Game Development Reference
In-Depth Information
y
z
V2
V1
V3
x
VR = V1 + V2 + V3
Figure 3-9. Adding vectors together
In terms of code, the Add() function in the Vector3 class adds two vectors and returns the resultant
vector. Each component x, y, and z of the vectors is added together to form the new components of
the resultant vector (see Listing 3-4).
Listing 3-4. The Add Function Adds Two Vectors
static Vector3 Add(Vector3 vec1, Vector3 vec2)
{
Vector3 result = new Vector3(0,0,0);
result.x = vec1.x + vec2.x;
result.y = vec1.y + vec2.y;
result.z = vec1.z + vec2.z;
return result;
}
Vector Multiplication
You can also multiply a scalar value by a vector. For example, if you want to set an object's velocity,
which is a combination of direction and speed, you would find a vector that points to the desired
direction, normalize the vector so that the vector length would be 1, and then multiply that vector
by the speed. The final resultant vector, VR, would be pointing in the desired direction and have a
magnitude value of the speed (see Figure 3-10 ).
 
Search WWH ::




Custom Search