Game Development Reference
In-Depth Information
In code, the magnitude of a vector is calculated by the Length() function in the Vector3 class, as shown
in Listing 3-2.
Listing 3-2. Length or Magnitude Function
float Length()
{
return FloatMath.sqrt(x*x + y*y + z*z);
}
Vector Normalization
Normalization in terms of vectors means that the length or magnitude of the vector is changed
to 1, while maintaining the vector's direction. Normalization is a good way to set vector quantities,
such as velocity and force. First, you would find a vector in the desired direction, then you would
normalize it to change its length to 1. Finally, you would multiply the vector by the magnitude you
want to assign to it, such as speed or the amount of force. In order to normalize a vector, you divide
each of the vector's components by the length of the vector (see Figure 3-8 ).
Figure 3-8. Normalizing a vector
In code, the Normalize() function in the Vector3 class performs normalization (see Listing 3-3).
Listing 3-3. Normalize() Function
void Normalize()
{
float l = Length();
x = x/l;
y = y/l;
z = z/l;
}
Vector Addition
Vectors can be added together to produce a resultant vector that is the combination of the effects
of all the individual vectors combined. You can add vectors graphically by putting them head to
tail. The resultant vector, VR, is the one drawn from the tail of the starting vector to the head of the
preceding vector (see Figure 3-9 ).
 
Search WWH ::




Custom Search