Search Unity

Wheel colliders... Are they broken?

Discussion in 'Physics' started by petey, Apr 10, 2017.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi there,

    I've been messing around with wheel colliders for a while now and this is about as good as I can get them -

    (This particular attempt was based on the Unity tute - https://docs.unity3d.com/Manual/WheelColliderTutorial.html)

    So just wondering, are they worth spending time on, or are they just broken?
    I guess I'm getting some serious alarm bells if they are that painful to get just barely working, they'll probably be an absolute nightmare in production. But I feel as though a lot of the assets that are out there are overkill.

    Anyway, any help would be great!

    Thanks,
    Pete
     
  2. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    No, they're not broken. I actually end up using them for things other than wheels. I'm not sure what's wrong with your setup here, I've made things that look more or less exactly like that and they work fine.
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey cool, I just went back and did the tute word for word again, and it worked! Thanks Uzi, I must have been doing something wrong. They actually seem quite stable now too!? So what other things do you use them for?
    P.
     
  4. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    I was thinking specifically about my Space Taxi clone. The landing legs here are actually wheel colliders.



    I'm measuring the compression of the wheel colliders and manually setting the frame on an animation of them compressing. Trying to do that with actual physics with hinge joints and stuff went hilariously bad, it was just easier to use a wheel collider. And it works very well.

    I've also used it for something that needed to hover. I think setting the wheel collider to 0 friction let it slide around and react in a semi-realistic way. In both of these cases I'm basically using the collider as a spring though, which can easily be emulated by using a raycast and an AddForce, because at the end of the day that's what the wheel collider is. It doesn't actually collide with anything, it just raycasts and applies a force.

    WheelColliders do work, but they're not perfect. For example, compression the suspension spring takes 0 energy. Imagine going over an obstacle like this forwards.



    It will work as expected until you stop on the obstacle. You'll roll backwards down the slope of one of the bumps but then as soon as the wheel collider is above the next bump it'll suddenly say "hey, I need to compress the spring, the ground is closer now!" And then it'll keep rolling backwards, it will actually accelerate backwards if you stop on an obstacle like this. So wheel colliders are far from perfect and you need to understand their limitations.
     
    Kurt-Dekker likes this.
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey thanks for that Uzi, I ended up removing the friction and adding my own forces and it seems to be working pretty cool.
    Thats in interesting example, I'd never really thought of that before but it's totally what would happen :p I get that they are pretty limited but I reckon that suits what I'm working on, I don't want to go too crazy.
    Hey your taxi game looks really cool btw ;) !

    P.
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    It's fun to see that WheelColliders work good except when you're trying to build actual cars with them :D.

    As for my experience, WheelColliders usefulness is inversely proportional to the degree of realism you're trying to achieve. The more realistic cars you need, the more issues and show stoppers you'll find when using them directly. The Vehicle Tools example shows the most the WheelCollider can do without further scripting. Additional scripting and tricks are needed beyond that point.
     
  7. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hehe, well I'm actually not sure of the usefulness even when you aren't aiming for realism :)
    I ran into a few showstoppers trying to get something arcadey happening.
    I wanted to try and get the vehicle to be able to do wheelies and since the wheecolider is just a single raycast, it kinda breaks.
    Also I couldn't get the friction working, it seems it love flipping the vehicle no matter what I do.

    Thanks for that link though, I'll check it out!
     
  8. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Flipping how? If you mean when you're turning the flips over, it's because the center of gravity is too high. The center of gravity of a cube car is too high by default, the center of gravity of a car is usually pretty low. The heaviest parts of the car are the engine and lower chassis, so bring the center of gravity down into the lower third of the car and it'll handle much better. There are other things you can do here as well, many cars have sway bars that help keep the car level for example. Simulating these can go a long way to making a car that handles well.

    If it's hitting the ground and then just bouncing all over the place, that's something that'll happen if you throw some wheel colliders on a default cube. The wheel colliders are set up by default to work on a car that weighs around 1,000 units. If you have a car that weighs 1 unit, the suspension will be so bouncy it can often throw it back into the air. Even if it doesn't do that, hitting a tiny bump will send the car flying and the suspension won't want to compress at all.

    Just remember that the wheel collider isn't really a collider. It does a raycast, finds the ground and applies forces to simulate a suspension and wheel. If you're falling and the wheel collider can't see the ground, but then suddenly next frame the suspension is compressed by 10%, then it will want to apply a spring force proportional to its compression, or 10% of 35,000 or whatever the suspension spring is set to. If your car weighs 1 unit like the default cube, it will want to apply a force of 3,500 and yes, that will send the car flying.

    As for wheelies, that should actually work. If you're trying to do that while accelerating then you could try moving the center of gravity backwards while accelerating or something. Given enough torque and weight transfer you might be able to get the car to do a wheelie. But you know, this is just a game, fake it by adding a force at the front of the car to lift it up a bit.
     
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    To be more exact, the spring force is proportional to the compression length, not the compression ratio.

    Example: if suspension distance is 0.2 and the suspension is 10% compressed, then the suspension distance results 0.2 x 0.1 = 0.02. For a suspension spring of 35,000 this case yields a force of 35,000 x 0.02 = 700 N.
     
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey thanks guys,
    I really appreciate the feedback here, I just got a bit tied up with other stuff! I'll definitely have a mess with the COG box and post the results.

    This is what I'm talking about with wheelies though -

    Popping the wheelie is okay but you get up there and the wheel collider starts to get on a crazy angle. I think at that point it's even trying to lift the vehicle off the ground rather than push it forward more.

    P.
     
  11. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    I think with a wheelie that extreme you've gone beyond the bounds of what the wheel collider was designed to do. There's only so much a single raycast can do.

    Maybe someone has written (Edy, perhaps?) a better wheel collider that uses multiple raycasts or a sphere overlap or something to better simulate a wheel. Barring that, you may want to go a half-scripted route just to get it to act correctly while doing a wheelie, but revert back to the wheel colliders when it's oriented correctly. Don't worry about it being right, just get it to work. If it looks and feels right and does what you need it to, then it is right.
     
  12. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    My workaround for that is using a collider that covers the top half of the rear wheel. This collider acts as limit preventing the wheel to reach those angles on wheelies:

    upload_2017-4-26_10-15-50.png
     
  13. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    What can I say, I am extreme... :cool:
    Hehe, yeah I think I'll come up with something to seperate the actual wheelie from the rest of the rig.
    The game idea wasn't meant to be that realistic anyway, so going down a full simualted path may have opened up a whole bunch of other challenges anyway.
    Thanks for all the help!
    P.