Game Development Reference
In-Depth Information
Our Vector Class
In terms of our code for this topic, the vectors are represented in the Vector3 class (see Listing 3-1).
Listing 3-1. The Vector3 Class
class Vector3
{
public float x;
public float y;
public float z;
// Vector3 constructor
public Vector3(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
}
In the Vector3 class, the x, y, and z components of the vector are represented by floats. The constructor
accepts three float values that represent a 3D vector. For example:
Vector3 m_MyVector = new Vector3(1,2,3);
declares a new Vector3 class called m_MyVector , initialized with the values x = 1, y = 2, and z = 3.
The Vector Magnitude
The magnitude of the vector is the scalar value of a vector and is the length of the vector. Recall that
a scalar value is a numerical value that has no direction associated with it. A velocity of an object
can be represented by a vector and has the components of direction and speed. The speed is the
scalar component and is calculated by finding the magnitude of the vector. The magnitude of a
vector is found by squaring the x, y, and z components, adding them together, and then taking the
square root (see Figure 3-7 ).
Figure 3-7. Vector magnitude calculation
 
Search WWH ::




Custom Search