Game Development Reference

In-Depth Information

If two vectors are parallel, then their cross product will be 0. This is useful when you

need to determine whether or not two vectors are indeed parallel.

The cross-product operation is distributive; however, it is not commutative:

u
×
v ≠ v
×
u

u
×
v = −
(
v
×
u
)

s (
u
×
v
) = (s)(
u
) ×
v
=
u
× (s)(
v
)

u
× (
v + p
) = (
u
×
v
) + (
u
×
p
)

Here's the code that takes the cross product of vectors
u
and
v
:

inline Vector operator^(Vector u, Vector v)

{

return Vector( u.y*v.z - u.z*v.y,

-u.x*v.z + u.z*v.x,

u.x*v.y - u.y*v.x );

}

Vector cross products are handy when you need to find normal (perpendicular) vectors.

For example, when performing collision detection you often need to find the vector

normal to the face of a polygon. You can construct two vectors in the plane of the polygon

using the polygon's vertices and then take the cross product of these two vectors to get

normal vector.

Vector Dot Product: The * Operator

This operator takes the vector dot product between the vectors
u
and
v
, according to

the formula:

u
•
v
= (u
x
* v
x
) + (u
y
* v
y
) + (u
z
* v
z
)

The dot product represents the projection of the vector
u
onto the vector
v
, as illustrated

in
Figure A-8
.

Figure A-8. Vector dot product

In this figure,
P
is the result of the dot product, and it is a scalar. You can also calculate

the dot product if you the know the angle between the vectors: