Skip to content
Siorki edited this page Nov 11, 2013 · 4 revisions

The car behavior is not the standard "flat out" driving that prevail in arcade games. The vehicle obeys gravity on inclines, careens (too ?) easily when going through a curve at high speed, and needs counterbraking skills to stay aligned with the road.

The physics implementation is derived from Marco Monster's excellent model (kept online by Adam Sawicki). Longitudinal forces control the acceleration and deceleration of the car, and lateral forces cause it to turn or skid.

The following tweaks and simplifications amend the original model :

##Straight line motion Notions like engine torque, gear ratio and longitudinal slip ratio are entirely ignored. The traction or braking forces are directly obtained by multiplying the throttle by a conversion factor. The factor, and thus the magnitude of the force, is identical for both acceleration and braking. Acceleration in reverse is capped at 40% of its value in forward motion. Drag force and weight transfer are simulated. In an early version of the game, a complete model was implemented, from the engine torque to the traction force. It was costly in lines of code, and did not add much to the simulation, thus it ended up being scrapped.

##Drivetrain The traction force can be applied to any combination of front or rear drivetrain, meaning that the equations can simulate FWD, RWD or 4WD cars. This possibility was added with an intent to feature extra cars, that could be acquired after winning a few races. Unfortunately, the size limit was hit before the car salesman was hired (remember the one from Supercars ?) Drivetrain

Gravity

The car speeds up travelling downhill and is slowed down moving uphill. This natural effect is achieved by adding extra forces, namely weight and the ground reaction, to the equations. Weight is always oriented downwards, and the reaction force goes the exact opposite way as long as the ground is level. In the latter case, both of them cancel up and can be ignored. Potholes, uneven surfaces, missing bridges and anything that could send the car flying into thin air is not simulated, nor are shock absorbers. Forces

In slopes, things get a bit more interesting. The reaction force is still orthogonal to the ground, but no longer points straight up (since the ground is at an angle), nor does it compensate perfectly weight. The resulting force accelerates the car downhill, adding or subtracting to your speed as long as you are travelling parallel to the road. If the car is at an angle, or worse, perpendicular to the slope, the lateral component of the force will negate some of the tire grip and may send the car spinning.

Grip

A commonly used, and pretty realistic - assuming you choose the right set of constants - model of tire behavior was developed by professor Pacejka at Delft University. From the slip angle of a tire, and the weight applied to the wheel, it produces the friction force exercised on the ground, which counteracts the car inertia and lets you steer to follow curves. The force goes through a maximum at a few degrees, then decreases slightly (how much actually depends on the constants set earlier) towards an asymptotic value, which is never reached since the slip angle is capped at PI/2, higher values are considered as travelling in reverse.

Early tests showed that the model could be simplified, which actually made the car easier to drive. The implementation has the force proportional to the slip angle, capped to a maximum value. In other words, past a given angle (0.4 radian ~ 23°), the cornering force remains constant instead of diminishing : even when careening wildly, the player stands a better chance to counterbrake and regain control of the car.

Steering

The steering angle is an input parameter of the model. Anything is supported, including ludicrous values : you could theoretically turn the wheel completely to one side while travelling at highway speeds (disclaimer : do not try this on a real car unless you know exactly what you are doing). Several options were available, I went for the one that stuck most to the arcade touch I wanted to instill to the game.

Thus, when both steering keys are released, the wheel returns to the neutral position, tires aligned with the body (but not necessarily with the direction of travel !). When going either left or right, the steering angle is a decreasing function of speed, pretty much the same way you would be operating a real car : turn the wheel completely at low speed to manoeuver, move it only slightly at high speed. The transition upon pressing or releasing a steering key is smoothed. The steering angle can be observed by looking at the front tires of any car in the game view.

Convergence at low speed

The physics model performs very well in most conditions, although it tends to be unstable when the car comes to a halt. Friction force, and thus acceleration, get their sign reversed at each step of simulation, which produces jagged, yet low amplitude, motion. The differential equations that describe the motion have terms that are linked to the car speed, and, to simplify, you end up solving for x in ax=b, where both a and b are getting closer and closer to zero. In formal mathematics, this is not much of an issue, as the function can be extended over a discontinuity point as long as it does not diverge in its vicinity. However, we are performing numerical solving, and the accumulation of rounding errors offsets the result enough to be visible : a car that was brought to a complete stop resumed is motion in form of a slow drift.

The friction force between the tire and the road is computed from the angle between the car motion and the tire orientation. When the speed is nearing zero, variations in the speed vector have an increasing impact on the angle : rounding errors are magnified, and the force is translated to an acceleration that alters the speed. At values close to a complete stop, this feedback loop will cause the speed to flip-flop continuously around zero, the acceleration induced by the friction being larger than is required to gradually reduce it.

The recommended solution is to add an extra degree (first derivative) to the differential equation. However, this has a heavy cost in terms of lines of code, which I wanted to avoid, as I only addressed model stability late in the development process, at a time the code size was already nearing the limit. After a few trial and errors, I went for something simpler and quite intuitive (which does not mean mathematically correct !). If there are no forces that tend to accelerate the car, which excludes acceleration and weight on an incline, leaving only braking, drag and friction, and the speed is below a preset threshold, then no force nor torque is applied, and speed and angular speed are quickly reduced to zero.

The same set of equations apply on both player and CPU cars, using only throttle, brake and steering values as input.