Last week I decided to take another look at the wheel physics implementation, in particular the tire friction model. First let’s quickly recap how the wheel physics works in GearBlocks.
Wheel physics recap
Every update I find the contact point on the ground directly below the wheel and position a configurable joint there. This joint is then connected to the wheel at the closest point on its outer edge to the contact point on the ground. The joint has a linear limit set up to prevent the wheel going below the ground, which provides the wheel to ground collision response.
For friction between the tire and ground, I set up the configurable joint’s velocity drive to constrain the wheel’s velocity at the contact point to zero, with the maximum force on the drive being set to the friction force. To calculate the friction force, I use a method inspired by the Coulomb damping model, where the friction force equals the product of a friction coefficient and the normal force at the contact point (i.e the force preventing the wheel from sinking into the ground).
Previous tire friction hack
To find the normal force I need to know what force the configurable joint’s linear limit is applying to keep the wheel above the ground. However, it used to be that in Unity there was no way to access this (even though it was available in PhysX), so I had to estimate the normal force by taking the total mass of the construction, dividing it by the number of wheels, and multiplying it by the acceleration due to gravity. Basically a total hack, because it assumed the vehicle’s weight was always distributed exactly evenly over each wheel.
Improved tire friction
Well the good news is it turns out that fairly recently the joint force was made available in Unity (via Joint.currentForce), so I’ve now switched the implementation over to use this to find a proper normal force. This means that in a vehicle, weight distribution now affects tire grip, and because the normal force is now being calculated dynamically, weight transfer also affects grip in the way you’d expect, which is pretty cool. This all sounds great, and is a definite step in the right direction, but there’s a problem.
By using Coulomb damping I’m effectively assuming that tires are rigid (i.e. non-elastic) which of course they’re not. In reality a tire’s grip changes depending on how much it is sliding across the ground (tires actually develop peak grip when sliding slightly). Not only that, the longitudinal (forward and back) and lateral (side to side) grip of a tire behaves slightly differently. So most driving simulations instead use some form of “friction curves”, equations that you plug slip amounts into and get friction forces out. The industry standard is the Pacejka tire models, sometimes known as “magic formulas”, these are empirical models that have been made to fit real world measured data, the equations themselves don’t have any basis in real physics as far as I can tell.
Sounds simple enough, so why not use the magic formulas? Well, after looking into this for a bit, I can see several problems:-
- The equations for the Pacejka curves themselves are complicated and have a ton of tuning parameters, something I’d prefer not to have to deal with. Probably overkill for what I need anyway.
- The slip values used to lookup into the curves are actually slip ratios. The upshot being that at low velocities numerical instability becomes an issue (and at zero velocity you’ve got a divide by zero – the slip ratio is undefined!)
- Because you need to evaluate longitudinal and lateral slip separately, there’s the question of how to combine the resulting separate friction forces. You can’t just add them together because a tire can only develop so much grip at any one time, the more longitudinal grip you “use up”, the less lateral grip is available, and vice versa. This effect is sometimes known as the tire’s “traction circle”.
I’m sure there are ways around all of these problems. The Pacejka equations could be substituted with something simpler for example, and I’ve seen various ideas out there that attempt to properly combine longitudinal and lateral slip. The slip ratio numerical instability issue I’m less sure about at the moment, apparently a lot of driving sims switch to another simpler friction model at low velocities to get around it, seems a bit hacky though.
Another related issue is that I’m not even differentiating friction levels between different surfaces (e.g. tarmac vs. dirt) yet, so perhaps a realistic tire friction model isn’t worth it at this point? Anyway, something to keep thinking about and revisit again in the future.