O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

//update for a simulation using a constant update step
void Vehicle::Update()
{
m_vPosition += m_vVelocity;
}
Remember though, that if you choose to eliminate Dt like this, the unit of
time you will be using in any calculations is no longer the second but rather
the time interval between update steps.
Acceleration
Acceleration is a vector quantity that expresses the rate of change of veloc
-
ity over time and is measured in meters per second per second, written as
m/s
2
. Acceleration can be expressed mathematically as:
(1.80)
This equation is stating that acceleration is equivalent to the change in
velocity of an object divided by the time interval during which the change
in velocity occurred.
For example, if a car starts from rest and accelerates at 2 m/s
2
, then
every second, 2 m/s is added to its velocity. See Table 1.1.
Table 1.1
Time(s) Velocity(m/s)
00
12
24
36
48
510
Plotting this data to a velocity versus time graph, we get Figure 1.26. If we
examine a time interval, say the interval between t=1 and t=4, we can
see that the gradient of the slope, given by , is equivalent to the accel
-
eration during that interval.
32 | Chapter 1
Physics
v
a
t
D
=
D
v
t
D
D
You learned earlier how the equation y = mx + c defines all straight lines in
the 2D Cartesian plane, where m is the gradient and c the intersection on
the y-axis. Because we can infer from Figure 1.26 that constant accelera-
tion is always plotted as a straight line, we can relate that equation to the
acceleration of the car. We know that the y-axis represents the velocity, v,
and that the x-axis represents time, t. We also know that the gradient m
relates to the acceleration. This gives the equation:
(1.81)
The constant u represents the velocity of the car at time t = 0, which can be
shown as the intersection of the line on the y-axis. For instance, if the car in
the example started off with a velocity of 3 m/s, then the graph would be
identical but offset upward by 3 as shown in Figure 1.27.
A Math and Physics Primer | 33
Physics
Figure 1.26. The velocity of the car plotted against time
vatu=+
Figure 1.27. The same car but traveling with an initial velocity of 3 m/s at time t =0
To test the equation let’s determine what the velocity of a car starting with
a velocity of 3 m/s and accelerating at 2 m/s
2
will be after 3 seconds.
Plugging in the numbers to equation (1.81) gives:
(1.82)
This is exactly what we can infer from the graph. See Figure 1.28.
Another interesting thing about a velocity-time graph is that the area under
the graph between two times is equivalent to the distance traveled by the
object during that time. Let’s look at a simple example first. Figure 1.29
shows the time versus velocity graph for a vehicle that spends 2 seconds at
4 m/s then stops.
34 | Chapter 1
Physics
Figure 1.28
233
9m/s
v
v
+
=
Figure 1.29
The area under the graph (the region shaded in gray) is given by height ´
width, which is equivalent to velocity ´ time, which as you can see gives
the result of 8 meters. This is the same result from using the equation
Dx = vDt.
Figure 1.30 shows the example from earlier where a vehicle accelerates
from rest with a constant acceleration of 2 m/s
2
. Let’s say we’d like to cal
-
culate the distance traveled between the times t = 1 and t =3.
We know that the distance traveled between t = 1 and t = 3 is the area
beneath the graph between those times. As is clearly shown in the figure,
this is the sum of the areas of rectangle A and triangle B.
The area of A is given by the time displacement, t, multiplied by the
starting velocity, u, written as:
(1.83)
The area of B, a triangle, is half the area of the rectangle described by the
sides of the triangle. The sides of the triangle are given by the time dis
-
placement, t, and the difference between the finish velocity and the start
velocity, v u. This can be written as:
(1.84)
Therefore, the total area under the graph between times t = 1 and t =3,
which is equivalent to the distance traveled, is the sum of these two terms,
given as:
(1.85)
A Math and Physics Primer | 35
Physics
Figure 1.30
()Area A t u=D ´
1
() ( )
2
Area B v u t=-D
1
()
2
xut vutD=D+ - D

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required