Game Development Reference
In-Depth Information
malized, so we only need to normalize P . Remember, to normalize a vector, its compon-
ents are divided by its magnitude.
Consequently, the normal of P (NP) is:
Np = P / |P| // Equation (2)
The dot product can now be used to determine the angle:
cos(θ) = Np.H // Equation (3)
So:
θ = cos -1 (Np.H) // Equation (4)
To clarify the process, let's do the whole thing again, but with some numbers. Let's say,
the troll is situated at the origin T (0, 0) and has a heading of H (1, 0). The princess is
standing at the point P (4, 5). How many radians does the troll have to turn to face the
princess? We know that we can use Equation (4) to calculate the angle, but first we need
to determine the vector, TP, between the troll and the princess and normalize it. To obtain
TP, we subtract T from P, resulting in the vector (4,5). To normalize TP, we divide it by its
magnitude, resulting in NTP (0.62,0.78). Finally, we plug the numbers into Equation (4),
which is illustrated as follows:
θ = cos -1 (Ntp.H)
θ = cos -1 ((0.62 *1) + (0.78 * 0))
θ = cos -1 (0.62)
θ = 0.902 radians
Search WWH ::




Custom Search