# How do I prevent raycast car wheels sliding sideways?

I've been writing a non-realistic car physics engine as a learning exercise, using this article as a reference.

I have a car that correctly collides with a terrain and applies the correct suspension forces for each wheel. My next problem is to prevent lateral movement of the wheels. For example, if I drop my car on a slope, it will slide sideways down the slope and then never stop sliding sideways (as there is no friction of any kind).

The linked article says that a "side force" must be applied, combined with the forward force, but it glosses over how that side force is calculated.

Given the collision info for each wheel (intersection normal, etc.) and the information about the car's rigid body, how can I calculate the appropriate side force to apply to the rigid body to prevent sideways motion?

That side force is the horizontal component of the road's normal with respect to the car's forward movement. Roads are banked like you are discussing to facilitate cornering at higher speeds without flying off the track from momentum pushing them to the outside corner. The banking pushes back in a direction the tires do not freely rotate and can hopefully resist; if the banking were steep enough, center of gravity high enough or speed slow enough, the car itself would act as a wheel and simply roll over.

Tires have a limited range of steering. Forces acting perpendicular to the longitudinal axis (front to rear) are resisted in-part because tires will not rotate that way. You can compute the force acting sideways against the road from gravity and forward velocity and then calculate the remaining force after the tire scrubs some of this friction off. If the remaining force is great enough to overcome the force pushing the car into the road, the car will slide/skid.

### The following illustrates this and is discussed here(with and without friction):

The force acting horizontally is of particular interest here. At rest (only force acting upon it is due to gravity), the car is not going to slide down a slope running perpendicular unless the coefficient of friction is exceptionally low (e.g. an icy road).

Andon's answer didn't quite give me solution, but it definitely put me on the right track!

Once I began thinking of the wheels being on their own plane (the normal of which being the vector sticking out from the centre - I'll refer to this as the wheel's "normal") I realized the side force is just the vector required to 'push' the car's velocity onto this plane. This vector is simply the wheel's "normal" multiplied by a scalar which is the dot product between the velocity and the "normal".

Here's the code that I settled on which works perfectly:

            // Now calculate the side force. Get the linear velocity
auto vel = car->body()->linear_velocity();
auto side_force = car->wheel_right_axis(i, closest_normal);

// Find the length of the vector necessary to nullify the horizontal movement
auto dot = kmVec3Dot(&vel, &side_force);

// Scale the right vector to that length
kmVec3Scale(&side_force, &side_force, -dot);


In the above code, "closest_normal" is the normal of the terrain the wheel collided with which is used as an 'up' vector when calculating the wheel's "normal".

• Yeah, I didn't really think that was going to give you the answer you needed, but it was much too long to put in a comment ;) – Andon M. Coleman Aug 31 '15 at 11:34