Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Relation between wheelcollider brake Torque and brake distance.

Discussion in 'Physics' started by David-C, Apr 30, 2021.

  1. David-C

    David-C

    Joined:
    Aug 23, 2014
    Posts:
    7
    Hello everyone, I'm very new to coding, so I will try to be as clear as possible.

    I have 2 AI cars, rolling in a straight line along the Z axis, I use motor torque to make them reach their speed.
    Car 1 is a little on the right of car 2 (on the X axis), so they will never collide even if car 2 start braking.
    Car 1 is behind car 2 and their velocity magnitude is 20.

    I want both cars to be side by side so car 1 is accelerating to reach a velocity.magnitude of 25.
    And here is the problem, I have failed at finding a way to calculate a relation with brake torque and the distance for car 1 to reach a velocity magnitude of 20 right when it is side by side with car 2.

    Thanks for reading and any help would be appreciated ^^.
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You need this:
    torque = force x distance (lever arm)
    force = mass x acceleration
    https://en.wikipedia.org/wiki/Equations_of_motion
    scroll down to "Constant translational acceleration in a straight line"

    Example
    Car 1 is moving at 25 m/s and you want it to slow to 20 m/s
    Car 2 is 50 m ahead of Car 1

    Use equation v^2 = u^2 + 2as (u is initial velocity, v is final velocity, a is acceleration and s is distance travelled)
    rearrange to get a = (v^2 - u^2) / 2s
    gives a = (20^2 - 25^2) / (2 x 50) = -2.25 m/s/s

    Assume Car 1 has a mass of 1200 kg, then given F = ma, F = 1200 x 2.25 = 2700 N

    So you need to apply a force of 2700 over 4 wheels = 2700 / 4 = 675 N per wheel

    If each wheel has a rolling radius of 0.3 m, then given torque = force x distance, torque = 675 x 0.3 = 202.5 Nm

    Hopefully, I have not made any mistakes with the numbers, but you get the idea.

    You will have to account for the gap changing due to the velocity difference between the cars, but that should not be too tricky once you get the basic idea working.

    Where you need to be careful is as your brake torque increases you will start to approach the slip limit of the wheel collider and eventually you will not be able to brake as hard as you would like because the wheel will slide, everything then gets more complex. A small error will occur because of how PhysX works, but it should be negligible.

    Edit:
    You need to apply this brake torque until you reach the desired speed (you can calculate the time this would take using the equations of motion I mentioned above, or just keep applying it until you reach the velocity you want)
     
    Last edited: May 2, 2021
    David-C likes this.
  3. David-C

    David-C

    Joined:
    Aug 23, 2014
    Posts:
    7
    Hello,
    I will try to work with all that ^^

    Thank you very much for the time you took to give me this reply, I greatly appreciate.
     
    AlTheSlacker likes this.
  4. David-C

    David-C

    Joined:
    Aug 23, 2014
    Posts:
    7
    I must have failed to understand something, I feel confused.
    I have set up the cars and distances to match the example you took. I also did the math and found the same results.
    So :
    Car 1 velocity is 25 and it's position Z = 50, at the same time car2 velocity is 20 at position z = 100.
    I then apply a brake torque of 202Nm on the 4 wheels. 4.45s later car 1 reach a velocity of 20 and it's position z = 150, at the same time car 2 position z = 189

    I wrote a little script to get the acceleration and it said my acceleration went from -1.2 to -1.16 over the 4.45s.
    Which do not match the a = (20^2 - 25^2) / (2 x 50) = -2.25 m/s.

    From what I have tried, I need to apply brake torque = 13.75 for both cars to be next to each other with a velocity magnitude of 20 at z = 505.

    I feel a bit lost
     
    Last edited: May 2, 2021
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I'm a bit tight for time now, I'll try and post something useful tomorrow. There are a few ways the first bit can go wrong...

    Most likely is that the wheels are slipping so you are not getting enough grip to give you the 2.25 m/s/s acceleration that you want. (you can check this by looking at WheelHit.forwardSlip data from WheelCollider.GetGroundHit). If you are using wheel friction curves for a typical real world car, then you should easily be able to achieve 2.25 m/s/s without slipping.

    Getting the cars to draw level is a little more awkward as you have to allow for the distance travelled by the first car and add that in to the calculations, but let's fix the basic braking first.
     
  6. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Try importing the attached package into a new, empty Unity session (I used 2020.3.3, but anything reasonably up-to-date should be OK). I have not attempted to make the code elegant, versatile or easy to maintain, but hopefully it is easy to follow...

    This demonstrates a car accelerating from stationary to 25 m/s and then braking to 20 m/s in the next 50 m. Here are some random notes/observations that you might find useful.

    The biggest problem you are likely to face is wheel slip. You will see I have added a very basic data log of what is happening to the car and there is a small amount of slip occurring at the start. In an effort to keep the code simple I have not corrected for this and you will see the car only reached 24 m/s instead of the target of 25 m/s. This could be resolved by applying the motor torque until the correct velocity is reached instead of purely relying on the time.

    You need to accelerate the car from stationary as I am not aware of any way to initiate a wheel collider as moving and in stable contact with the ground (wheel collider RPM is readonly - EDIT: I believe this had changed as of late 2022). You cannot just set the velocity of the car rigidbody as this will result in a large amount of noise and slip as the tyre tries to match ground speed.

    Another reason for instability at the start can be the suspension forces balancing out which can result in transient reductions in the contact force; these in turn allow slip to start. Try to ensure you don't see your car drop down / spring up in the first second of the simulation. Or just allow your car a couple of seconds to settle before you apply any torques to the wheels.

    The SubSteps class is to enable substeps in the physics engine for the wheel colliders. I believe it only needs to be added for a single collider to enable it for all colliders, but I have not tested this and just pasted the code in. This really helps to stabilize the wheel contact with the ground.

    Acceleration and braking would be smoother if the torques were ramped (lerped) in rather than stepped, this would reduce wheel slip (obviously, you would need to allow for this separately in your calculations).

    I've supplied a couple of functions based on the equations of motion that I linked to earlier, hopefully you can see enough here to figure out any others you may need.

    You may want to check out https://assetstore.unity.com/packages/tools/physics/vehicle-physics-pro-community-edition-153556 for a complete and free vehicle model (note, I have no affiliation with this and have not used it, but it comes very highly rated).

    I hope some of that helps.
     

    Attached Files:

    Last edited: Sep 5, 2022
    David-C likes this.
  7. David-C

    David-C

    Joined:
    Aug 23, 2014
    Posts:
    7
    The attached package works well, I will learn from your explanations and package then I'll correctly implement things in my project.

    Thank you so much !
     
    AlTheSlacker likes this.