# How do I calculate vehicle rotation when two car wheels are on the ground to simulate gravity?

I'm writing some code to simulate a vehicle (tank or car) driving over a heightmap terrain. I'm not using a full physics engine, and just simply checking the height of the terrain at the 4 bottom corners of the vehicle bounding box for collision. The vehicle has a quaternion to represent rotation, and a 3D position.

I have orientation working if there are at least 3 wheels touching the ground (I can define a plane and determine the orientation from the normal), but what I'd like to do is react pseudo-realistically rotate the car if two wheels are touching the ground.

For example, if the rear two wheels hit the ground, I'd like to calculate a rotation to make the front of the vehicle drop until 3+ wheels touch the ground.

Obviously if I have two wheels intersection points, I have an axis, but I'm not sure how to manipulate the rotation and position to rotate around that axis when it's not central to the body.

• Should it be possible to flip the car? – PSquall May 9 '19 at 12:45
• No! In fact I was planning to ask an additional question to restrict the rotation within 45 degrees of vertical. – Kazade May 9 '19 at 13:26

I'd be inclined to simulate each corner of the vehicle as a point mass on a spring suspension.

Each integration step, you accumulate several vectors that nudge these points around:

• gravity pulling it down
• the suspension spring pushing it away from the terrain, according to its current proximity along your vehicle's vertical axis
• dampening from the suspension to keep it from bouncing / oscillating too much
• the vehicle's momentum / grip from the wheel pushing it forward

Left alone, these influences could cause the corners to all wander off in their own directions, so as a final step you enforce the constraint that all four corners form a planar rectangle with your vehicle's dimensions.

One simple way to do this is with Verlet integration, where you just calculate adjusted positions for each corner and that will implicitly adjust the velocity without the need to compute restoring impulses & torques.

You can compute the average of the four corners to get the vehicle center, the average of the front corners minus the rear to get its average forward direction, and the average of the right corners minus the left, minus the projection onto the forward to get the average right direction. With that basis you can set the corner points back into a perfectly rigid planar rectangle.

That rectangle will naturally try to settle into positions where each suspension spring is similarly depressed, finding its level on uneven terrain. When only two wheels are close enough to provide a push on the suspension springs, the momentum and mass of the remaining two corners will cause it to rotate smoothly along the axis between the supporting wheels until a third wheel touches down.