5. Chapter 1
Objective
The objective of this project was to physically model the experience of driving a car. In order
to accomplish this, a physics simulation will be used, along with realistic graphical rendering
of the car and the environment. The basic idea of the project is to implement an OpenGL
3D car driving simulation in C++. We used the SDL libraries, along with GLEW and the
linear algebra library, Armadillo. A robust rigid body dynamics simulation was developed
as part of the project. The emphasis was to have as realistic physics as possible, and to use
a variety of graphical rendering techniques commonly found in commercial games. Because
this was a large-scale collaborative project, we used SVN as a version control system. The
project can be subdivided into three main subsystems: the rigid body dynamics engine, the
car physics model, and the graphics rendering engine.
Figure 1.1: An early version showing rigid body dynamics and car driving
4
6. Chapter 2
Prior Work
Various existing games and simulations inspired our effort throughout this project:
2.1 Flight Simulation
The flight simulation that I wrote several years ago provided much background knowledge
for implementing this project. Like the car driving simulation, the flight simulation fea-
tures a rigid body dynamics engine for physics. It also gave me much-needed experience in
mathematically modelling realistic phenomena using differential equations and forces. The
flight simulation project lacked one of the main challenges of the car driving simulation –
resting contact forces. I managed to use a really crude implementation of the impulse-based
method for resolving the contact forces while the plane was on the runway since the terrain
was approximately planar locally.
Figure 2.1: Prior Work: Flight Simulation Project
5
7. 2.2 Racer
The open source game racer found at
http://www.racer.nl/
was another inspiration for this project. Like this project, it aims to model the experience of
driving a car as realistically as possible. An interesting thing to note is that like this project,
Racer, uses the Pacejka tire model to model the longitudinal and lateral forces on each tire
while the car is on the ground. Moreover, Racer implements many of the same graphical
effects as this project, including atmospheric scattering. A proof-of-concept screenshot from
Racer follows. Racer focuses more on the simulation front than trying to be an arcade racing
game. Also, a key observation is that Racer uses a flexible configuration file system that
stores simulation parameter values for each car. This idea will prove to be useful for tweaking
values in the simulation, as the number of coefficients needed to model a car to any level of
detail is quite large.
Figure 2.2: A proof-of-concept screenshot from Racer
2.3 Commercial Games
Perhaps the most impressive commercial car driving/racing game is the newly-released Gran
Turismo 5. This newest release emphasized having photo-realistic graphics rendering ca-
pabilities. Although it is far beyond the scope of this project, it serves as inspiration, and it
provides a formidable goal to compete with.
6
8. Figure 2.3: A screenshot showcasing the highly realistic commerical game, Gran Turismo 5
7
9. Chapter 3
Rigid Body Dynamics Simulation
3.1 Introduction
The rigid body dynamics simulation component of this project provided support for New-
tonian physics simulation. The physics engine was implemented based on Kenny Erleben’s
“Velocity-Based Shock Propagation for Multibody Dynamics Animation” paper. It uses a
projected, relaxed Gauss-Seidel solver to analytically determine contact forces:
b = J(ut + ∆tM−1 fext )
A = JM−1 JT
λ = ncp(A, b)
ut+1 = ut + M−1 JT λ + ∆tM−1 fext
st+1 = st + ∆tSut+1
A screenshot of a rigid body test demo with a box resting on the ground follows:
3.2 Numerical Integration
The Euler method of numerical integration is used to timestep the simulation by computing
updated velocities and positions for each body. It is represented in matrix-form as discussed
in the Erleben paper. We needed a stable integrator to solve the dynamics equations. Prof.
James suggested using a reverse Euler method. This worked fine for our needs. The Time-
Corrected Verlet method was also researched, along with the fourth-order Runge-Kutta
method.
xn+1 = xn + f (tn , vn )∆t
vn+1 = vn + g(tn , xn )∆t
8
10. Figure 3.1: Rigid body dynamics test simulation
3.3 Resting Contact
3.3.1 Analytical Method
Contact forces were computed analytically using a relaxed Gauss-Seidel solver. To improve
simulation stability, I also manually resolved penetration at each time step.
Projected Relaxed Gauss-Seidel Solver
The projected Gauss-Seidel solver was implemented using the optimizations suggested in the
Erleben paper.
i−1 n−1
− j=0 Li,j λk+1 −
j j=i+1 Ui,j λk − bi
j
λk+1
i =
Di,i
The projection step enforces the friction cone and the linear complementarity problem
constraints:
λk+1 = min(max(λloi , λk+1 , λhii )
i i
We added a relaxation term ω = 0.25 to the iteration step given in the paper.
9
11. 3.4 Collision Detection and Response
3.4.1 Collision Detection
Collision detection right now is limited to simple box-plane collision testing. A future plan
is to utilize the SOLID collision detection library so that the car can collide with arbitrary
“polygon soup” meshes such as the concrete barrier on the racetrack.
3.4.2 Newton’s Law of Restitution
Newton’s Law of Restitution is used to compute the collision response impulses:
AB
−(1 + e)v1 · n
j=
n·n 1
MA
+ 1
MB
+ I−1 (rAP × n) × rAP + I−1 (rBP × n) × rBP · n
A B
It is represented in Matrix form as part of the b vector used in the rigid body system
of equations discussed in the Erleben paper. This is an elegant way to add bouncing as a
collision response to the simulation.
3.4.3 Tangential Coulomb Friction
Coulomb friction was also incorporate in the rigid body solver using extra constraints on λ
as discussed in the Ereleben paper:
Ff ≤ µFn
The Coulomb friction turned out to be convenient for modelling the rolling resistance
forces that affect the car’s tires. We later switched from using the built-in Coulomb friction
to just applying it ourselves as a separate longitudinal resistance force.
10
12. Chapter 4
Car Driving Model
The force-based car physics model can be divided into longitudinal forces and lateral forces.
By computing the resultant force vector acting on the car (with some degree of physical
accuracy), the car can be simulated within the rigid body dynamics simulation engine. At
the heart of the simulation is the Pacejka tire model, which determines both the lateral and
longitudinal forces on the car as functions of slip ratio and slip angle (soon to be defined)
respectively. A limiting factor was the availability of empirical coefficients and data. For
example, the Pacejka coefficients are jealously guarded by car manufacturers, so finding good
data required a scavenger hunt on the Internet.
4.1 Longitudinal Forces
Longitudinal forces were implemented using the model discussed in Short et al.’s “Simulation
of Vehicle Longitudinal Dynamics” paper. This paper described force-based models for the
various longitudinal forces acting on a car, primarily derived from empirical data:
4.1.1 Resistance
Resistance forces oppose the motion of the car.
Fluid Dynamic Drag
Fluid dynamic drag is computed using the v 2 drag equation (an equation that is all-too-
familiar to flight simulation developers):
1 v
FD = − ρACd (v · v)
2 ||v||
The drag coefficient Cd = 0.4257 for a Corvette C5 sports car was used in this simulation.
Rolling Resistance
The rolling resistance is the frictional force opposing the motion of the wheels spinning on
the road:
11
13. Figure 4.1: Longitudinal forces acting on the car
F = Crr Nf
where Crr = 12.8 was the parameter value used for the simulation.
4.1.2 Brake Force
The brake force model used in this simulation is a simple linear model discussed in Monster’s
article “Car Physics for Games”:
Fbraking = −u ∗ Cbraking
4.1.3 Engine Force
The engine is treated as a black box that provides torque for the purposes of simplifying this
simulation without losing too much accuracy.
Torque Curves
Engine torque is provided as a function of engine RPM:
Short performed quadratic regression in his paper to come up with an analytical equation:
TM ax = 528.7 + 0.152R − 0.0000217R2
He suggested that using higher-order polynomials may provide improved fit; however, for
our needs, this quadratic model should suffice.
12
14. Figure 4.2: Engine torque curve
Gearbox Model
For a realistic car simulation, it is essential to model the transmission/gear box of the car
itself.
Gear and Difference Ratios Gear ratios were provided in Marco Monster’s paper for
the aforementioned Corvette sports car:
Gear Constant Ratio
First gear g1 2.66
Second gear g2 1.78
Third gear g3 1.30
Fourth gear g4 1.0
Fifth gear g5 0.74
Sixth gear g6 0.50
Reverse gR 2.90
Differential ratio xd 3.42
In order to compute the drive torque from the engine torque, the engine torque is scaled
by the gear ratio, the differential, and the transmission efficiency (0.7 for our simulation),
13
15. Figure 4.3: Car Transmission
resulting in different torque curves for each gear:
Figure 4.4: Drive Torque Curves
Automatic Shifter Logic
An automatic transmission is implemented from a shifter map discussed in Short’s paper:
Based on the throttle position and the current RPM, the gear for the car can be deter-
mined.
14
16. Figure 4.5: Gear shift map
4.1.4 Pacejka Tire Model
The Pacejka model is an empirically derived model for computing the longitudinal and lateral
forces acting on each car tire. The lateral forces are important for steering and skidding. The
Pacejka curves are functions of slip angle/ratio and wheel load that compute these forces.
Each Pacejka model fits an equation of the form:
y(x) = D sin[C tan−1 Bx − E(Bx − tan−1 (Bx))]
Coefficients
Coefficients were borrowed from Brian Beckman’s The Physics of Racing series of articles
since empirical tire coefficient data is very hard to find elsewhere. Note that the aligning
moment coefficients allow feedback forces that the driver feels at the steering wheel to be
computed. The coefficients used in this simulation are reproduced in the following table:
15
17. Lateral Coefficients Longitudinal Coefficients Aligning Moment Coefficients
a0 = 1.5 b0 = 1.65 c0 = 2.2
a1 = −40 b1 = 0 c1 = −3.9
a2 = 1600 b2 = 1688 c2 = −3.9
a3 = 2600 b3 = 0 c3 = −1.26
a4 = 8.7 b4 = 229 c4 = −8.2
a5 = 0.014 b5 = 0 c5 = 0.025
a6 = −0.24 b6 = 0 c6 = 0
a7 = 1.0 b7 = 0 c7 = 0.044
a8 = −0.03 b8 = −10 c8 = −0.58
a9 = −0.0013 b9 = 0 c9 = 0.18
a10 = −0.15 b10 = 0 c10 = 0.043
a111 = −8.5 b11 = 0 c11 = 0.048
a112 = −0.29 b12 = 1.65 c12 = −0.0035
a12 = 17.8 - c13 = −0.18
a3 = −2.4 - c14 = 0.14
- - c15 = −1.029
- - c16 = 0.27
- - c17 = −1.1
4.2 Lateral Force
A graph of the lateral force Pacejka model follows:
Slip Angle
A quantity known as slip angle determines the lateral forces. It is defined as the angle
between the velocity vector of the car and its orientation:
Mathematically, its definition is:
vy
β = tan−1
vx
4.3 Longitudinal Force
A graph of the longitudinal force Pacejka model follows:
Slip Ratio
Similarly, a quantity known as slip ratio determines the longitudinal forces. The slip ratio
is the amount of slip between the tire and the road surface. This slipping creates a friction
force that drives the car forward. Mathematically, it is defined as:
ωw Rw − vlong
σ=
|vlong |
16
18. Figure 4.6: Lateral Pacejka function
Figure 4.7: Slip angle
Elliptical Force Constraint
It is important to note that these forces have to be constrained so that reside within the
friction cone of the car tire. Racer implements this constraint quite succinctly as:
2
Fx
Fy = Fy0 ∗ 1−
F x0
17
19. Figure 4.8: Longitudinal Pacejka function
where Fx0 is the maximum lateral force possible. The downside is that this model always
favors the longitudinal forces. From these Pacejka forces, along with the longitudinal vehicle
dynamics model, a realistic force-based model for a car driving simulation was implemented.
18
20. Chapter 5
Graphical Improvements
5.1 Normal Mapping
Normal mapping allows for more realistic rendering of bumpy surfaces within the game. We
modulate the normals using a texture map that stores each normal for each pixel in a compact
RGB format. The texture map stores the normals in a tangent space with respect to the
textures, necessitating an extra coordinate system transformation. Normal mapping allows
the lighting calculations to take in account the surface details, as shown in the following
figures:
Figure 5.1: The normal map for the concrete barrier
19
21. Figure 5.2: The diffuse texture map for the concrete barrier
Figure 5.3: The resulting scene rendered with bump mapping
5.2 Dynamic Cube Environment Mapping
A crucial element of photorealistic rendering is accurately reproducing the reflected light on
shiny metal surfaces. This effect is particularly necessary for rendering a car. We dynamically
20
22. compute the reflections by rendering off-screen into six axis-aligned cube faces from the
location of the car. We then fetch samples from these textures using a reflection vector
computed within a GLSL shader in order to render the reflections on the car. The result
is that the reflections are not static – they dynamically reflect the car’s surroundings, and
they take in account the changing sky color.
Figure 5.4: The car model rendered using dynamic environment mapping
5.3 High Dynamic Range Lighting
High dynamic range lighting is a graphical effect found in many commercial games, where
textures are stored in floating point format, and rendering is performed off-screen into two
floating point textures – a low dynamic range texture and a high dynamic range texture.
The main advantage of high dynamic range lighting is that the contrast ratio of the image
can be much higher since light values are no longer clamped to reside within the [0, 1] range.
The high dynamic range texture is blurred using a Gaussian filter for a so-called bloom effect
around the bright areas, and then the two textures are composited to form the final image.
Moreover, a tonemapping operator scales the RGB light intensities to within the [0, 1] range
used by a conventional computer monitor. A graphical demonstration of some of the effects
possible with high dynamic range lighting follows.
5.4 Motion Blur
Motion blurring was implemented as well by Mark using a shader that runs on the GPU:
21
23. Figure 5.5: The car model rendered using high dynamic range lighting
Figure 5.6: A close-up of the high dynamic range lighting
22
24. Figure 5.7: Motion blurring
5.5 Atmospheric Scattering
An atmospheric scattering model was implemented to analytically compute the sky color
with beautiful results from Sean O’Neil’s “Accurate Atmospheric Scattering” article in GPU
Gems 2. Atmospheric scattering calculations allow us to have a full day-night cycle with
colorful sunrises and sunsets. A brief overview of the mathematics used follows (one can
consult the article for more details):
Figure 5.8: A diagram illustrating atmospheric scattering
Here is the main integral that needs to be evaluated per-vertex on the three RGB wave-
lengths:
Pb
−h
Iv (λ) = Is (λ) × K(λ) × F (θ, g) × (exp × exp(−t(P Pc , λ) − t(P Pa , λ)))ds
Pa H0
t is a function of optical depth, which describes the average atmospheric density across
a ray of light:
23
25. Pb
−h
t(Pa Pb , λ) = 4π × K(λ) × exp ds
Pa H0
The Henyey-Greenstein phase function (responsible for the light and dark areas in the
sky towards its zenith) in polar coordinates is also needed where g is a tunable parameter
that affects the eccentricity of the elliptical shape. Rayleigh scattering, which predominates
the sky, is a special case where g = 0:
3 × (1 − g 2 ) 1 + cos2 θ
F (θ, g) = ×
2 × (2 + g 2 ) (1 + g 2 − 2 × g × cosθ) 3
2
The integrals are evaluated using Riemann sums on the GPU in GLSL shaders.
24
26. Chapter 6
Human Computer Interaction
This project allowed for some interesting experimentation in human-computer interaction.
Figure 6.1: HCI hardware setup diagram
25
27. 6.1 Keyboard and Mouse Controls
The keyboard controls are as follows:
Functionality Key
Camera Pan Forward [W]
Camera Pan Backward [S]
Camera Pan Left [A]
Camera Pan Right [D]
Mouse Look Toggle [TAB]
Car Accelerate [Up Arrow]
Car Brake [Down Arrow]
Car Steer Left [Left Arrow]
Car Steer Right [Right Arrow]
Car Gear Down [,]
Car Gear Up [.]
Car Horn [H]
6.2 Heads-Up Display
The heads-up display provides the driver with the same kinds of instrumentation as an
actual car. A rear-view mirror is simulated using an off-screen FBO render buffer, along with
speedometer and tachometer gauges that use OpenGL’s orthographic rendering functionality.
6.3 Steering Wheel Controller
A Logitech USB steering wheel controller was interfaced and calibrated using the configura-
tion system as a hardware input device for controlling the car. This provides for a much more
realistic driving experience than merely using the traditional keyboard and mouse controls.
6.4 Haptic Force Feedback
6.4.1 Pacejka Aligning Moment (Mz )
One of the more interesting features of the steering wheel controller is its ability to provide
haptic feedback forces to the user. The Pacejka model allows these feedback forces to be
determined. Using the Pacejka aligning moment (Mz ) equation allows the feedback force
that the driver feels at the wheel to be computed:
y(x) = D sin[C tan−1 Bx − E(Bx − tan−1 (Bx))]
By applying this force using the hardware force feedback functionality of the steering
wheel, haptic feedback forces can be applied to the user. This is the same model that the
Racer uses to determine its feedback forces. The parameter values are B, C, D, and E are
26
28. Figure 6.2: Logitech USB steering wheel controller
empirically determined coefficients. Unfortunately, actually telling the haptic hardware to
apply these forces to the steering wheel required a switch to SDL 1.3 beta, which could not
be completed in time.
6.5 Sound Effects
Sound effects are used for the car horn, the engine whine (which varies in timbre with
respect to the engine RPM), and the skidding (once the slip angle goes beyond a certain
cut-off value).
27
29. Chapter 7
Future Plans
7.1 Height Map Terrain Engine
A height map terrain engine using the ROAM (Real-time Optimally Adapting Meshes)
algorithm for level-of-detail to allow for non-flat driving surfaces.
Figure 7.1: ROAM-ing terrain (Real-time Optimally Adapting Meshes)
7.2 Suspension/Weight Distribution Model
Work on suspension/weight distribution by modelling mass-spring-dashpot system for each
tire.
7.3 Wheel-Body Joint Constraints
Support for advanced joint constraints in the rigid body solver’s Jacobian matrix so that the
tires can be modelled as separate bodies as demonstrated by Russell Smith’s Open Dynamics
28
30. Figure 7.2: A canonical mass-spring-damper system
Engine:
Figure 7.3: A “Hinge-2” joint from the Open Dynamics Engine
7.4 Improved Collision Detection
Box-triangle mesh collision detection using the SOLID collision detection library.
7.5 Physics Stability
Additional tweaking to improve the stability of the physics simulation, particularly when
running at low framerates.
7.6 Simulation Accuracy
Finding more accurate coefficient values and implementing more advanced car physics models
to improve the simulation accuracy.
29
31. 7.7 Racing Game
Turning the car driving simulation into a full-fledged racing game.
30
32. Chapter 8
Conclusion
The results of this project was a drivable simulation with realistic graphical rendering,
physics, and sound effects. The steering wheel controller provides a realistic and inter-
esting way of controlling the simulation. Moreover, the graphical rendering capabilities are
quite on-par with the Racer simulation.
Figure 8.1: Final result
High-definition videos, along with full source code, is provided with this project submis-
sion.
31
33. Chapter 9
References
Several references have been consulted:
• Robert Bridson Nonconvex Rigid Bodies with Stacking
http://www.graphics.stanford.edu/papers/rigid_bodies-sig03/
• Doug James CS 5643: Physically Based Animation for Computer Graphics
http://www.cs.cornell.edu/courses/cs5643/2010sp/
• David Baraff Fast Contact Force Computation for Nonpenetrating Rigid Bodies
http://www.cs.cmu.edu/~baraff/papers/sig94.pdf
• Jonathan Dummer A Simple Time-Corrected Verlet Integration Method
http://www.gamedev.net/reference/programming/features/verlet/
• Marco Monster Car Physics for Games
http://home.planet.nl/~monstrous/tutcar.html
• Brian Beckman The Physics of Racing
http://phors.locost7.info/contents.htm
• Pacejka’s Magic Formula
http://www.racer.nl/reference/pacejka.htm
• Mark Duchaineau, et al. ROAMing Terrain: Real-time Optimally Adapting Meshes
http://www.llnl.gov/graphics/ROAM/roam.pdf
32