How to make a physically real,stable car with WheelColliders

How to make a physically real, stable car using WheelColliders

Demo:
http://www.edy.es/dev/vehicle-physics/demo

Press Enter to restart the level (i.e. if you flip the car or fall outside the terrain)
Press C to change the secondary camera.
Press B to change the stability mode among “Sport / Offroad” (default is “Sport”)
Press N to disable all stability features (keeps the plain rigidbody + 4 wheel colliders only). B to reenable.

It’s not only a typical road-car not meant to being too stable, but it also carries a heavy 500 Kg (1100 lbs) box! Can you keep it safely with you in all kind of terrains?

Notes:

  • Simple PhysX model: rigidbody + some box colliders roughly resembling the car’s shape + 4 standard WheelColliders
  • REAL center of mass, located around the front seats.
  • No angular drag, no arbitrary forces.
  • Default tyre friction curves, only stiffness is adjusted (forward: 0.82, sideways: 0.022)
  • No parameter is modified at runtime! (center of mass, rigidbody, drag or angular drag, suspension…)

This is the typical stability problem when using WheelColliders:

Q: Why my absolutely simple car (rigidbody + 4 wheel colliders) flips over so easily when steering?
A: Because that’s exactly how such car would behave in real life!!

So you have created a box, added a rigidbody and four wheel colliders. Maybe you’ve even used some real car’s data (mass, dimensions…). You have added a control script, tested it, and as soon as you gain a bit of speed and do a corner, the car rolls and flips over.

If you could build that in real life, it would do the same! PhysX is not perfect, but resembles the physical behavior of real objects in a fairly good manner.

Also note that the default WheelCollider’s friction curve parameters in Unity (1,20000,2,10000,1) define a tyre with almost infinite grip. So the rigidbody has no choice but rolling-over when steering even at low speed. You should first set the last parameter (Stiffness Factor) to 0.01-0.03 to have more realistic tyres, but even in that case the car will roll over when stering at certain (low) speed.

Q: But real cars don’t roll over so easily. Why?
A: Because real cars have STABILIZER BARS (aka anti-roll or anti-sway bars)

Sounds familiar?
Stabilizer bars “link” the two wheels of the same axle allowing a limited degree of freedom between them. When one of the wheels is pushed upwards, the stabilizer bar transfers a portion of that compression force to the other wheel, so its suspension compress as well. This limits the roll of the body’s car at that axle.

Q: I haven’t heard of such bars! Are really so important?
A: Absolutely. Stabilizer bars are an essential part of a vehicle’s suspension system in the same way as springs and dampers are.

Look under your car, observe the suspension behind the front wheels. You’ll be able to easily identify a rigid bar that travels from one wheel’s suspension to another. That’s it. The only exceptions are a very few car models that use dynamic systems (ej. computer-controlled hydraulic systems) to actively emulate the behavior of the anti-roll bars in turns.

IMHO, an AntiRoll script for WheelColliders should be at least mentioned in the documentation and tutorials as a basic requirement for physically real vehicles. Actually, anti-roll bars are barely mentioned in the last chapter of the Unity’s Car Tutorial (Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn), where an alternate car physics model is proposed.

Q: What about all other solutions proposed here? (lower center of mass, angular drag, custom friction models…)
A: The essential requirements for having a car that physically feels like real include the anti-roll bars.

Once you have the REAL absolutely simple car (rigidbody + four wheel colliders + 2 anti-roll bars) you can then apply all other techniques to get special behaviors on your car and/or tweaking its handling, i.e for arcade-style driving.

Even with the real absolutely simple car you’ll find a lot of options to tweak and play with, all of them affecting the handling and feel of the car: mass, REAL center of mass (typically moved towards the engine location), front-rear springs, front-rear dampers (must be different because the axle under the engine will support more weight), front-rear anti-roll bar stiffness…

No need for complicated custom friction/drag models at the beginning - leave them for final tweaks if necessary. You’ll see how much the handling can change by simply adjusting the difference between the stiffness of the front and rear stabilizer bars.

Q: What’s bad with lowering the center of mass?
A: Jumps, collisions, crashes or air-tricks will look weird because of the false center of mass.

Artificially lowering the center of mass is just a workaround that works under some circumstances, but fails at others. Having the REAL center of mass in the car, and making it stable by means of stabilizer bars, will make almost all situations behave like real.

Q: I’ve run your demo and I’ve made the car flip in “Sport” mode!
A: Look at that kind of car. If you do a close turn at high speed with that car in real, surely you’d roll over as well!

If you are (like me) used to watch those “educative” documentaries at Discovery Channel “Road Rampage”, “Destroyed in Seconds”, and so on, you’ll know how easily this kind of cars can roll over at relatively normal speeds. Also, you may have heard about “The moose test”, which is a heavy “S” turn test performed at 80 Km/h (50 mph). Many actual cars failed that test (Google for “the moose test” or see Moose test - Wikipedia)

Note that in the demo the elevation of the center of mass is around the middle of the car’s body! You must do a hard turn at around 60 Km/h (40 mph) for being able to flip the car in “Sport” mode. The anti-roll bars are doing an excellent work already.


Now the fun part…

Q: How to simulate stabilizer bars in Unity?
A: Easily :wink:

Anti-roll bars work by transferring some compression force from one spring to the opposite in the same axle. The amount of the transfered force depends on the difference in the suspension travel among the wheels.

So first task is to calculate the suspension travel on each wheel, as well as determine whether is grounded or not. We want the travel value to be between 0.0 (fully compressed) and 1.0 (fully extended):

groundedL = WheelL.GetGroundHit(hit))
if (groundedL)
    travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
else
    travelL = 1.0;

We multiply the travel diference by the AntiRoll value, which is the maximum force that the anti-roll bar can transfer among the springs (we could call this the Anti-roll bar’s stiffness). This yields the amount of force that will be transfered:

var antiRollForce = (travelL - travelR) * AntiRoll;

Finally, we must simply substract the force from one spring and add it to the other. We achieve this by adding opposite forces to the rigidbody at the positions of the WheelColliders:

if (groundedL)
    rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position);   
if (groundedR)
    rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);

Q: What’s a good AntiRoll value? Which units is it expressed in?
A: A good value is roughly the value of the wheel’s spring. Both are expressed in Newtons.

The spring value means the force the spring can give when fully compressed, and the AntiRoll value means the amount of force that can be transfered from one spring to another. Having the same values for Springs and AntiRoll in the same axle means that the anti-roll bar can transfer up the entire force of one spring to the other.

Q: Can I have the script for an anti-roll bar?
A: Sure, here is it.

As suggestion, I believe that an Anti-Roll script should be included in Unity’s Standard Assets.

Add two anti-roll scripts to your vehicle, one per axle (front - rear). Set the left-right wheels on each one and adjust the AntiRoll value. Remember to reset center of mass to the real position, or don’t touch it at all!

The full script: AntiRollBar.js

var WheelL : WheelCollider;
var WheelR : WheelCollider;
var AntiRoll = 5000.0;

function FixedUpdate ()
    {
    var hit : WheelHit;
    var travelL = 1.0;
    var travelR = 1.0;

    var groundedL = WheelL.GetGroundHit(hit);
    if (groundedL)
        travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;

    var groundedR = WheelR.GetGroundHit(hit);
    if (groundedR)
        travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;

    var antiRollForce = (travelL - travelR) * AntiRoll;

    if (groundedL)
        rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
               WheelL.transform.position);   
    if (groundedR)
        rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
               WheelR.transform.position);   
    }

Q: How can I set a real center of mass for my vehicle?
A: Use non-overlapping box colliders to roughly resemble your vehicle’s shape, then move the center towards the position of the engine.

Unity/PhysX calculates the center of mass based on the position and volume of the GameObject’s colliders. Note that overlapping colliders add more mass at the overlap position.

Typically, you would only need to move the center of mass a bit towards the position of the engine (front - rear). For instance, if your vehicle has front engine and its front is oriented in the Z+ direction, you should only need to move the (automatically calculated) center of mass 1 meter to the front:

function Start ()
    {
    rigidbody.centerOfMass += Vector3(0, 0, 1.0);
    }

Q: Does it really works so good? Could it really be so simple??
A: See it by yourself: Edy's Vehicle Physics demo - moved

You can have a good view of how it works by pressing C (change secondary camera), then pressing B while driving (alternate Sport / Offroad mode). Press N for completely disabling the stabilizer bars (B to reenable, or Enter to restart after flipping over).


Ending words…

As this is my first post here, please let me introduce myself. I’m a developer now working on dj software. Years ago, while I was a student two friends and me spent about two years trying to develop games in C++ from scratch (no engines at all), because games was what we wanted to do since kids. As you can imagine, very little to no result could be seen in the screen. I discovered Unity about four weeks ago (had heard of it before, but hadn’t look at it) and for me it was like seeing The Light. I could just put things on the screen, click play, and worked! I could then fine-tune the scene by looking at code examples and writing scripts in whatever language used by Unity (two weeks ago I realized I was coding in javascript). Magic! Now I’m about to achieve a personal goal of modeling physically real vehicles. This is what I really wanted to do since I was kid and self-learned to code in Basic with by Commodore 64. Overlander, Powerdrift, Hard Driving, and later 4D Sports Driving, Fatal Racing… now I can easily do it!! Even as hobbyist, I’m purchasing the Unity Pro license just because I love it!

This article is my first contribution to the GREAT Unity community. It’s great to have fun by modeling and playing with Unity’s vehicle physics.

Enjoy!
Edy

33 Likes

Thank you. That was a great read and a wonderful first post. :slight_smile:

1 Like

Thank you ! really !

Wonderful post. I really appreciate you sharing your workflow and insights with this. Seems a shame to have this info lost in a forum thread. Maybe it can go up on the Unify Wiki or a blog post on the Unity blog page? Full credit to you of course :wink:

Awesome! I was racking my brain trying to figure out a way to increase stability back when I went through the Car Tutorial and was making my own for practice mini-racing game and Monster truck off-roading.

This is a great solution to a problem that almost everyone seems to have when talking about vehicle physics. This definitely needs to be in the Wiki AND the blog! Great job! I will revisit that project and incorporate this into it as that was my biggest stumbling block.

What a fabulous contribution. Sir, I tip my hat to you.

I was vey happy yesterday when i find your topic, but today i’m disappointed. I tried to use your script to stabilizer my car, but it seems that nothing change (enabled or disabled) :frowning:

So i probably missed something (in spite of your very great explanation).

These screenshots show what i have :

Very basic shape :


I tried to use real dimension for the differents elements :
2m between rear wheels
1.8m between front wheels
3m between front wheels and rear wheels

I tried to play with differents values for the mass/center of mass/spring/damping … my vehicule flips to easily even at low speed.

Maybe its a problem whith the scripts.

public class BuggyController : MonoBehaviour
{

    public WheelCollider frontWheel1;
    public WheelCollider frontWheel2;
    public WheelCollider rearWheel1;
    public WheelCollider rearWheel2;

    public float steerMax = 20f;
    public float motorMax = 10f;
    public float brakeMax = 100f;

    private float steer = 0f;
    private float motor = 0f;
    private float brake = 0f;

    void Start()
    {
        //rigidbody.centerOfMass += new Vector3(0f, 0f, -1f);
    }

    void FixedUpdate()
    {
        steer = Mathf.Clamp(Input.GetAxis("Horizontal"), -1, 1);
        motor = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
        brake = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);

        rearWheel1.motorTorque = motorMax * motor;
        rearWheel2.motorTorque = motorMax * motor;
        rearWheel1.brakeTorque = brakeMax * brake;
        rearWheel2.brakeTorque = brakeMax * brake;
        frontWheel1.steerAngle = steerMax * steer;
        frontWheel2.steerAngle = steerMax * steer;
    }

}

And your script en C#

public class AntiRollBar : MonoBehaviour
{

    public WheelCollider WheelL;
    public WheelCollider WheelR;
    public float AntiRoll = 5000.0f;

    public void FixedUpdate()
    {
        WheelHit hit;
        float travelL = 1.0f;
        float travelR = 1.0f;

        bool groundedL = WheelL.GetGroundHit(out hit);
        if (groundedL)
            travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;

        bool groundedR = WheelR.GetGroundHit(out hit);
        if (groundedR)
            travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;

        float antiRollForce = (travelL - travelR) * AntiRoll;

        if (groundedL)
            rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
                   WheelL.transform.position);
        if (groundedR)
            rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
                   WheelR.transform.position);
    }

}

If somebody can see where i’m wrong…

Sure! Feel free to add the article to the blog and/or the Wiki. I’d be more than happy to appear in your blog! Take the version at the demo page as I’ve just updated the article. I believe this is a must-know for everybody modeling vehicle physics with Unity.

Feel free also to correct any grammar or misspelling errors when publishing it. I’ll then copy the corrections :stuck_out_tongue:

EDIT: The demo page will be the entry point for all my Unity articles and tutorials, so you can safely link it.

Right now I working on the friction curves, how they work exactly, and how to configure and use them properly. I’m almost done with that, so I expect to update the demo during this week. The goal is to create a fully customizable GTA4-style car using standard components. A new article will follow some time later :wink:

@ProfCWalker, Guru, MitchStan:

Thank you very much! Hope you have as much fun modelling vehicles as I’m having while (still) discovering Unity :slight_smile:

@blizhard:

Thank for your comments! There’s one obvious problem in your project, and other minor things you should consider. The big obvious thing:

Sideways Friction > Stiffness Factor: 1

Change that value to 0.02 - 0.01 in all wheels. Use that value to fine tune the “drifting” ability.
My next article will explain everything about the friction curves :wink:
EDIT: I’ve updated the post to include the stiffness values used.

Other things to look at:

  • The radius of the wheels are too large. Use something more realistic, about 0.5. Note that actually they are increasing the vehicle’s height a lot.

  • You have a very big collider at the top of your “car”. This makes the auto-calculated center of mass to be very high. You should move the center of mass to a more realistic position. Somewhere at the top of the lower colliders should be ok.

VERY cool - nice work thanks for sharing the info (and the code!) :smile:

@Edy : Thank you man ! it works !!

Hi there,
great physics you have there.
i’m making an open world game (like GTA) and started doing the car physics about 1 week ago…
i’m having some problems with the physics and i hope you or anyone could help
here is a small video of the problem:

at medium speeds, if i turn the car it will flip…
i would apreciate any help

i also used your script but it still flips:
here are the properties of the wheel colliders and other things:



(also i sent you a PM, hope you read it)

Edit:
i changed the stiffness factor of the sideways driction to 0.02, and now it turns better and doesn’t flip, but at high speeds it still flips…

Recently I’ve realized that the WheelCollider has a design problem on the sideways friction. For any stiffness value, there will be a speed at which the car will flip over.

So your chance is to set a speed limit to the vehicle, and then find a stiffness factor so low that the car doesn’t flip over at that maximum speed. You could also try dynamically setting the stiffness factor according to current speed.

I don’t think it’s really a design problem,or maybe just a bit, since in real cars the tire warms on slidings and loose traction.

Try it by yourself: Take a small 1:10 car which has replica of tires (must be enough realistic replica) and try to move it on the side pushing it to the ground. You’ll see the car will hardly slide.

But maybe I’m wrong.

It’s a design problem, trust me. I’ve researched deep in the WheelCollider’s insights and the sideways friction behaves in a way that it’s barely usable as-is. You’ll see it more clearly when I publish the information and documents.

That’s what I was saying, the wheel collider in Unity is given as a plastic toy’s wheel, you must simulate some kind of warming to get the desired effect. :stuck_out_tongue:

The new forum system doesn’t let me edit the original post (>10000 characters, odd). So here’s the update:

New in this version:

  • Highly improved everything.
  • 4 vehicles: V3 Pickup / GTA4 Pickup / Bus / Sport Coupe (select with PgDown / PgUp).
  • Dashboard with speed, gear, and driving aids (1-5 for driving aids)
  • New damage script with improved deformation and instant-repair option (shift R).
  • Vehicle interiors with mouse-look view (F1, mouse look where available).
  • Rear mirrors in the Bus driver view (PgDown until Bus, then F1).
  • Better traction simulation at low speeds (burnouts without TC).
  • Smoke effects on burnouts and driftings.
  • WheelCollider friction curves are now perfectly predicted, allowing exact fine-tunning of the tire behavior.
  • Graphic tire telemetry (B, then shift-B)
  • Integrated help (H).

I like Edy’s vehicle library better than other vehicle libraries. Is it possible to get source code and content so that I can create my driving/racing games?

I’d really like to see some source code for this demo, I just can’t get my cars to behave the same!

I would like to purchase an asset bundle for this. Can you make it available at a (hopefully not expensive) price? Its just the right feel.