Game Development Reference
In-Depth Information
To travel from the treasure back to the ship, the pirates can follow the same
line but in the opposite direction. This is achieved by flipping the vector such
that all coordinate values are multiplied by 1. In this case, to get back to the
ship they follow the vector ( 3,7).
It might also be useful for the pirates to know how far the treasure is from the
ship. The length of a vector, v , called its magnitude and written | v |, is found
using Pythagoras' theorem shown in Equation (2.1) .
.
.
v vx vy
=
2
+
2
(2.1)
For the pirates, it means that their journey is a lengt h of 7.62 (kilometers, if the
units being used are kilometers), that is,
+− .
Sometimes it is necessary to scale a vector so that it has a length equal to 1.
The process of scaling the length is called normalizing , and the resultant
vector, which still points in the same direction, is called a unit vector . To find
the unit vector, each coordinate of the vector is divided by the vector's length.
In the case of the pirate's journey, this would equate to (3/7.62, 7/7.62) =
(0.39, 0.92). If the pirate takes 0.39 steps to the west and 0.92 steps to the
south, he will end up a distance of 1 from his starting position, right on the
original vector, as shown in Figure 2.2 . As can be seen, the vectors (3, 7)
and (0.39, 0.92) are parallel and the magnitude of (0.39, 0.92) is 1. The unit
3
2
( 7)
2
Fig 2.2 A normalized vector has
a length of 1.
( 0.39,0)
(0,-0.93)
leng th = 1
(3,-7)