You’ll be using vector math frequently when designing the AI for your
games. Vectors are used everywhere from calculating which direction a
game agent should shoot its gun to expressing the inputs and outputs of an
artificial neural network. Vectors are your friend. You should get to know
You have learned that a point on the Cartesian plane can be expressed as
two numbers, just like this:
A 2D vector looks almost the same when written down:
However, although similar, a vector represents two qualities: direction and
magnitude. The right-hand side of Figure 1.16 shows the vector (9, 6) situ-
ated at the origin.
NOTE Vectors are typically denoted in bold typeface or as a letter with an
arrow above it like so: Åv. I’ll be using the bold notation throughout this book.
The bearing of the arrow shows the direction of the vector and the length of
the line represents the magnitude of the vector. Okay, so far so good. But
what does this mean? What use is it? Well, for starters, a vector can repre
sent the velocity of a vehicle. The magnitude of the vector represents the
speed of the vehicle and the direction represents the heading of the vehicle.
That’s quite a lot of information from just two numbers (x, y).
18 | Chapter 1
Figure 1.16. A point, P, and a vector, V
Vectors aren’t restricted to two dimensions either. They can be any size
at all. You would use a 3D vector, (x, y, z) for example, to represent the
velocity of a vehicle that moves in three dimensions, like a helicopter.
Let’s take a look at some of the things you can do with vectors.
Adding and Subtracting Vectors
Imagine you are a contestant in a TV reality game. You are standing in a
clearing in the jungle. Several other competitors stand beside you. You’re
all very nervous and excited because the winner gets to date Cameron
Diaz… and the losers have to watch. Sweat is dripping from your forehead,
your hands are clammy, and you cast nervous glances at the other competi
tors. The bronzed, anvil-chinned TV host steps forward and hands a gold-
trimmed envelope to each competitor. He steps back and orders you all to
rip open your envelopes. The first person to complete the instructions will
be the winner. You frantically tear away at the paper. Inside is a note. It
I’m waiting for you in a secret location. Please hurry, it’s very hot in
here. You can reach the location by following the vectors (–5, 5), (0,
–10), (13, 7), (–4, 3).
With a smile on your face you watch the rest of the competitors sprint off
in the direction of the first vector. You do a few calculations on the back of
the envelope and then set off in a completely different direction at a lei-
surely stroll. By the time the other competitors reach Cameron’s hideout,
sweating like old cheese and gasping for breath, they can hear your playful
giggles and the splash of cool shower water…
You beat the opposition because you knew how to add vectors together.
Figure 1.17 shows the route all the other competitors took by following the
vectors given in Cameron’s note.
A Math and Physics Primer | 19
Figure 1.17. The route of the opposition
You knew, however, that if you added all the vectors together you would
get a single vector as the result: one that takes you directly to the final des
tination. To add vectors together you simply add up all the x values to give
the result’s x component, and then do the same with the y values to get the
y component. Adding the four vectors in Cameron’s note together we get:
giving the vector (4, 5), exactly the same result as if we followed each vec
tor individually. See Figure 1.18.
Multiplying vectors is a cinch. You just multiply each component by the
value. For example, the vector v (4, 5) multiplied by 2 is (8, 10).
Calculating the Magnitude of a Vector
The magnitude of a vector is its length. In the previous example the magni
tude of the vector v (4, 5) is the distance from the start point to Cameron’s
20 | Chapter 1
(5) (0) (13) (4) 4
=- + + +- =
Figure 1.18. Your route