Game Development Reference
In-Depth Information
multiply it by this computed distance, we will get our extended .
Figure 3.7(d)
shows this extended vector, which can then be used to calculate the value of .
Now that we have the value of , we can substitute back into the equation from
Figure 3.6(b)
and perform some simple algebra to get the final solution for :
When you're solving vector problems, it is useful to make sure you ultimately are
performing valid vector operations. For example, if a solution to a vector problem
suggested that avector shouldbeadded toascalar,it wouldhave tomean the solu-
tion is wrong. This is much like checking to make sure the units line up in physics.
So let's look at our solution to the vector reflection problem. In order to do vector
subtraction with , the result of must be a vector. If we look at this ex-
pressionmoreclosely,wefirstscalar multiply by2,whichresults inavector.We
then scalar multiply this vector by the scalar result of a dot product. So, ultimately,
we are subtracting two vectors, which certainly is valid.
This vector reflection problem is a great example of how to use the vector opera-
tions we've covered thus far. And it also turns out that it's an extraordinarily com-
mon interview question posed to potential game programmers.
Cross Product
The
cross product
between two vectors results in a third vector. Given two vec-
tors, there is only a single plane that contains both vectors. The cross product finds
a vector that is perpendicular to this plane, as in
Figure 3.8(a)
,
which is known as
a
normal
to the plane.
Because it returns a normal to a plane, the cross product only works with 3D vec-
tors. This means that in order to take a cross product between two 2D vectors, they
first must be converted to 3D vectors via the addition of a z-component with a
value of 0.