Game Development Reference
In-Depth Information
P = u v = | u| | v| cos θ
Here's the code that takes the dot product of u and v :
// Vector dot product
inline float operator*(Vector u, Vector v)
{
return (u.x*v.x + u.y*v.y + u.z*v.z);
}
Vector dot products are handy when you need to find the magnitude of a vector pro‐
jected onto another one. Going back to collision detection as an example, you often
have to determine the closest distance from a point, which may be a polygon vertex on
one body (body 1) to a polygon face on another body (body 2). If you construct a vector
from the face under consideration on body 2, using any of its vertices, to the point under
consideration from body 1, then you can find the closest distance of that point from the
plane of body 2's face by taking the dot product of that point with the normal vector to
the plane. (If the normal vector is not of unit length, then you'll have to divide the result
by the magnitude of the normal vector.)
Scalar Multiplication: The * Operator
This operator multiplies the vector u by the scalar s on a component-by-component
basis. There are two versions of this overloaded operator depending on the order in
which the vector and scalar are encountered:
inline Vector operator*(float s, Vector u)
{
return Vector(u.x*s, u.y*s, u.z*s);
}
inline Vector operator*(Vector u, float s)
{
return Vector(u.x*s, u.y*s, u.z*s);
}
Scalar Division: The / Operator
This operator divides the vector u by the scalar s on a component-by-component basis:
inline Vector operator/(Vector u, float s)
{
return Vector(u.x/s, u.y/s, u.z/s);
}