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

you are located, no matter what the gravity, your duplicate will have
exactly the same mass as THE kilogram back in France. Problem solved.
Position
You might think the position of an object is an easy property to measure,
but where exactly do you measure its position from? For example, if you
wanted to specify your body’s position in space, from where would you
take the measurement? Would it be from your feet, your stomach, or your
head? This presents a problem because there would be a big discrepancy
between the position of your head and that of your feet.
Physicists solve this problem by taking the location of the center of
mass of the object as its position. The center of mass is the object’s balance
point. This would be the place where you could attach an imaginary piece
of string to the object and it would balance in any position. Another good
way of thinking about the center of mass is that it is the average location of
all the mass in a body.
Velocity
Velocity is a vector quantity (a quantity that has magnitude and direction)
that expresses the rate of change of distance over time. The standard unit of
measurement of velocity is meters per second, abbreviated to m/s. This can
be expressed mathematically as:
(1.75)
The Greek capital letter D, read as delta, is used in mathematics to denote a
change in quantity. Therefore, Dt in equation (1.75) represents a change in
time (a time interval) and Dx a change in distance (a displacement). D is
calculated as the after quantity minus the before quantity. Therefore if an
object’s position att=0is2(before) and att=1is5(after), Dx is5–2=
3. This can also result in negative values. For instance if an object’s posi
-
tion att=0is7(before) and att=1is3(after), Dx is3–7=–4.
Ü
NOTE Delta’s little brother, the lowercase letter delta, written as d, is used to
represent very small changes. You often see d used in calculus. Because d looks
similar to the letter d, to prevent confusion, mathematicians tend to avoid using
d to represent distance or displacement in their equations. Instead, a less
ambiguous symbol such as Dx is used.
Using equation (1.75), it’s easy to calculate the average velocity of an
object. Let’s say you want to work out the average velocity of a ball as it
rolls between two points. First calculate the displacement between the two
points, then divide by the amount of time it takes the ball to cover that
30 | Chapter 1
Physics
x
v
t
D
=
D
distance. For instance, if the distance between the points is 5 m and the
time taken for the ball to travel between points is 2 s, then the velocity is:
(1.76)
It’s also easy to calculate how far an object has traveled if we know its
average speed and the length of time it has been traveling. Let’s say you
are driving your car at 35 mph and you’d like to know how far you’ve
moved in the last half hour. Rearranging equation (1.75) gives:
(1.77)
Popping in the numbers gives:
(1.78)
Relating this to computer games, if you have a vehicle at position P at time
t traveling at constant velocity V, we can calculate its position at the next
update step (at time t+1) by:
(1.79)
Where VDt represents the displacement between update steps (from equa-
tion (1.77)).
Let’s make this crystal clear by showing you a code example. Following
is a listing for a
Vehicle class that encapsulates the motion of a vehicle
traveling with constant velocity.
class Vehicle
{
//a vector representing its position in space
vector m_vPosition;
//a vector representing its velocity
vector m_vVelocity;
public:
//called each frame to update the position of the vehicle
void Update(float TimeElapsedSinceLastUpdate)
{
m_vPosition += m_vVelocity * TimeElapsedSinceLastUpdate;
}
};
Note that if your game uses a fixed update rate for the physics, as do many
of the examples in this book, Dt will be constant and can be eliminated
from the equation. This results in the simplified
Update method as follows:
A Math and Physics Primer | 31
Physics
5
2.5 m/s
2
v ==
xvtD=D
1
PPV
tt
t
+
=+D
1
distance traveled 35 17.5 miles
2
=

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