Game Development Reference

In-Depth Information

β

v =(1,0)

(Unit Vectors)

α

(1,0)

(-1,0)

(0,1)

(0,-1)

length=7.62

w =(3,-7)

Fig 2.3
A pirate facing (1,0) and the

vector to the treasure.

In computer graphics, a positive value for an angle always indicates an

anticlockwise turn. An anticlockwise turn in this case would have the pirate facing

away from the treasure. When calculating the angle between vectors using the

dot product, the angle is always positive. Therefore, you need another method to

determine the turn direction. This is where the cross product comes into play.

The cross product of two vectors results in another vector. The resulting vector

is perpendicular (at 90°) to both the initial vectors. This sounds odd working in

2D as a vector at right angles to two vectors in 2D would come right out of the

page. For this reason, the cross product is only defined for 3D. The formula to

work out the cross product is a little obtuse and requires further knowledge of

vector mathematics, but we will try to make it as painless as possible.

The cross product of two vectors,
v
and
w
, denoted
v
×
w
is shown in

Equation (2.3)
:

v w vw vw

× =

(

−

)(1,0,0)

+

(

vw vw

−

)(0,1,0)

y

z

z

y

z

x

x

z

(2.3)

+

(

vw vw

−

)(0,0,1)

x

y

y

x

The equation is defined in terms of standard 3D unit vectors. These vectors are

three unit length vectors orientated in the directions of the
x
,
y
, and
z
axes. If

you examine
Equation (2.3)
you will notice that there are three parts added

together. The first part determines the value of the
x
coordinate of the vector,

as the unit vector (1,0,0) only has a value for the
x
coordinate. The same occurs

in the other two parts for the
y
and
z
coordinates.