Game Development Reference
In-Depth Information
to achieve a 3D-like effect, by having an image face the camera at all times. If done with a complex
background image, such as a tree, the viewer may not notice that it is the same image when viewed
from different angles (see Figure 3-13 ).
Figure 3-13. Dot product formula
You can also find the angle between two vectors using the dot product. The angle between two
vectors is the angle whose cosine is given by the dot product of vector A and vector B, divided by
the magnitude of vector A, multiplied by the magnitude of vector B (see Figure 3-14 ).
Figure 3-14. Finding angle from dot product
You can simplify the above equation by normalizing both vectors, then taking the dot product.
The denominator becomes 1, and the angle is the arc cosine of the dot product of vector A
and vector B.
You can get the dot product directly from the vectors by multiplying each component in vector A by
the corresponding component in vector B and adding the results together. Such as
Dot Product = (Ax * Bx) + (Ay * By) + (Az * Bz)
See Listing 3-7 for the Java code to do this.
Listing 3-7. DotProduct Function
float DotProduct(Vector3 vec)
{
return (x * vec.x) + (y * vec.y) + (z * vec.z);
}
Vector Cross Product
The cross product of two vectors A and B is a third vector that is perpendicular to both A and B
(see Figure 3-15 ). Cross products can be used in applications such as billboarding, where you need
to find a rotation axis and know the vector that represents the front of the image and the vector that
points toward the object you want to turn toward. The cross product is calculated in code in Listing 3-8.
 
Search WWH ::




Custom Search