Game Development Reference
In-Depth Information
How it works
Here's an example of some of the vector methods you've just learned about working togeth-
er. Let's say you have a game agent, Eric the Troll, who stands at position T (the origin) and
facing in the direction given by the normalized vector H (for heading). He can smell a help-
less princess at position P and would very much like to throw his club at her, to tenderize
her a little, before he rips her to pieces. To do this, he needs to know how many radians he
must rotate to face her. The following figure shows the situation:
You've discovered that you can calculate the angle between two vectors using the dot
product. However, in this problem you only have one vector to start with,
H
. Therefore, we
need to determine a vector—the vector
TP
that points directly at the princess. This is calcu-
lated by subtracting point
T
from point
P
. Because
T
is at the origin (0, 0), in this example,
P-T= P. However, the answer P-T is a vector, so let's show this by typing it in bold and
calling it
P
.
We know that the cosine of the angle the troll needs to turn to face the princess is equival-
ent to the dot product of
H
and
P
, provided both vectors are normalized.
H
is already nor-