Game Development Reference
In-Depth Information
Dot product tests are used in back face culling. This is a technique to see if some
polygon is facing away from the camera. By default, polygons are not double-
sided; they only have one side indicated by the normal. If the polygons face away
from the camera, they can't be seen. This means the graphics hardware doesn't
need to be told about them. The dot product can be used to filter out all the
polygons that are facing away from the camera.
The Cross-Product Operation
The last vector operation to be covered is the cross-product. Unlike the previous
operations, this operation only works on vectors with three or more elements.
This means there is no cross-product operation for a simple X,Y vector. The
calculation is more complicated than previous operations but the results are
quite intuitive. The cross-product takes two vectors and returns a vector per-
pendicular to the passed in vectors. If you have a table with one side from [0, 0, 0]
to [0, 0, 1] and another side from [0, 0, 0] to [1, 0, 0], then the resulting dot
product vector will be a vector that is pointing upwards from the table's surface
[0, 1, 0]. The operation can be seen in Figure 8.16.
Here is the formula for calculating the cross-product.
A ¼ B C
2
3
2
3
2
3
A x
A y
A z
B x
B y
B z
C x
C y
C z
4
5 B
4
5 C
4
5
¼
¼
¼
A
A x ¼ B y C z B z C y
A y ¼
B z C x
B x C z
A z ¼
B x C y
B y C x
The formula looks rather intimidating; fortunately, once it's converted to code it
can pretty much be ignored. When this formula is used, you won't be thinking
about the particular mechanics of the code, just that you want a vector that
points outwards from two cross-product vectors.
public Vector CrossProduct(Vector v)
{
double nx = Y * v.Z - Z * v.Y;
 
Search WWH ::




Custom Search