Game Development Reference
In-Depth Information
vectors for north, south, east, and west as they would be overlaid on a map
of the earth 1 are (0,1), (0, 1), ( 1,0), and (1,0).
Two further important calculations can be performed with vectors. These are
the dot product and the cross product . The use of these in computer graphics
and games programming will become overwhelmingly clear later. Among
other things, the dot product can be used to calculate the angle between two
vectors and the cross product can be used to determine the direction.
The dot product is calculated by taking two vectors, v and w, and multiplying
their respective coordinates together and then adding them. The dot product
results in a single value. It can be calculated using Equation (2.2) .
v wv wv w
.
=×+×
(2.2)
x
x
y
y
Given the vectors v = (1,0) and w = (3, 7), the direction the pirate is facing
and the direction to the treasure (shown in Figure 2.3 ), the dot product will
be 1 × 3 + 0 × 7 = 3.
But what does this mean? The most useful application of the dot product
is working out the angle between two vectors. In a moment we will work
out the actual value of the angle, but for now by just knowing the value
of the dot product you can determine how the vectors sit in relation to
each other. If the dot product is greater than zero, the vectors are less
than 90° apart; if the dot product equals zero, then they are at right angles
(perpendicular); and if the dot product is less than zero, then they are more
than 90 ° apart.
To find out the exact angle the pirate must turn to face the treasure, the
arccosine of the dot product of the unit vectors is calculated. The unit vector
for (3, 7) is (0.39, 0.92) as already established and (1,0) is already a unit vector.
This result for the angle between the vectors is therefore:
= arcos((1,0).(0.39,0.92))
= arcos((1 × 0.39) + (0 × 0.92))
= arcos(0.39)
= 67°
You can always check the result of your calculation by looking at a plot of
the vectors and measuring them with a protractor. In this case, by taking a
visual estimate, the angle is larger than 45 and less than 90; therefore, the
calculation appears to be correct.
Now imagine the pirate is told to turn 67° and walk for 7.62 kilometers
to get to the treasure. Which way does she turn? The image in Figure 2.3
shows that a decision needs to be made as whether to turn to the right
or the left.
1 This is only for a land map. For 3D coordinates, there are no such equivalents.

Custom Search