Game Development Reference
In-Depth Information
Vector Addition, Subtraction, and Multiplication
The vector addition operation is very simple; each member of the first vector is
added to the respective member of the second vector. Here is the code to per-
form vector addition.
public Vector Add(Vector r)
{
return new Vector(X + r.X, Y + r.Y, Z + r.Z);
}
public static Vector operator+(Vector v1, Vector v2)
{
return v1.Add(v2);
}
When the binary addition operator + is overloaded, it automatically overloads
+= . The same is true for *= and /= .
Figure 8.4 shows the result of adding two vectors together. Vectors are often
added together when trying to get a certain offset in 3D space. For instance, say
you wanted to put a 3D model of a halo above a player's head. The player origin
is directly in between the feet of the character. You have a vector that represents
an offset from the player's feet to the center of the player's head [0, 1.75, 0].
Figure 8.4
Vectors addition.
 
Search WWH ::




Custom Search