4.2. Points and Vectors 147
z Perpendicular. (a ⋅ b) = 0 (i.e., the angle between them is 90 degrees).
z Same direction. (a ⋅ b) > 0 (i.e., the angle between them is less than 90
degrees).
z Opposite directions. (a ⋅ b) < 0 (i.e., the angle between them is greater than
90 degrees).
Some Other Applications of the Dot Product
Dot products can be used for all sorts of things in game programming. For ex-
ample, let’s say we want to fi nd out whether an enemy is in front of the player
character or behind him. We can fi nd a vector from the player’s position P to
the enemy’s position E by simple vector subtraction (v = E – P). Let’s assume
we have a vector f pointing in the direction that the player is facing. (As we’ll
see in Section 4.3.10.3, the vector f can be extracted directly from the player’s
model-to-world matrix .) The dot product d = v ⋅ f can be used to test whether
the enemy is in front of or behind the player—it will be positive when the
enemy is in front and negative when the enemy is behind.
(a · b) = ab
(a · b) = –ab
(a · b) = 0
(a · b) > 0
(a · b) < 0
a b a b a b
a
b
b
a
Figure 4.11. Some common dot product tests.
Figure 4.12. The dot product can be used to fi nd the height of a point above or below a
plane.