Game Development Reference
In-Depth Information
need a four-dimensional vector to send in to a 4 × 4-transform matrix. If you
remember your high school math, you can ' t multiply a 4 × 4 matrix and a three-
dimensional vector. Only a four-dimensional vector will do.
The methods that are provided as a part of this class are
n Length : Finds the length of the vector.
n Normalize : Changes the vector to have the same direction but a length of 1.0f.
n Dot : Computes the dot product of the vector.
n Cross : Computes the cross product of the vector (only Vec3 does this!).
Matrix Mathematics
A 3D world is filled with objects that move around. It would seem like an impossible
task to set each vertex and surface normal of every polygon each time an object
moves. There
s a shortcut, it turns out, and it concerns matrices. Vertices and surface
normals for objects in your 3D world are stored in object space. As the object moves
and rotates, the only thing that changes is the object
'
s transform matrix. The original
vertices and normals remain exactly the same. The object
'
s transform matrix holds
information about its position in the world and its rotation about the X-, Y-, and
Z-axis.
Multiple instances of an object need not duplicate the geometry data. Each object
instance only needs a different transform matrix and a reference to the original
geometry. As each object moves, the only things that change are the values of each
transform matrix. A transform matrix for a 3D engine is represented by a 4 × 4 array
of floating-point numbers. This is enough information to store both the rotation and
position of an object. If you only want to store the rotation and not the position, a
3 × 3 array is just fine. This is one of the reasons you see both matrices represented
in DirectX and other renderers. I
'
ll use the 4 × 4 D3DXMATRIX in this chapter for all
of the examples because I want to use one data structure for rotation and translation.
The matrix elements are set in specific ways to perform translations and different
rotations. For each kind of matrix, I ' ll show you how to set the elements yourself or
how to call a DirectX function to initialize it.
A translation matrix moves vectors linearly. Assuming that you have a displacement
vector t , which describes the translation along each axis, you
'
'
ll initialize the transla-
tion matrix with the values shown below.
// Create a DirectX matrix that will translate vectors
// +3 units along X and
2 units along Z
 
 
Search WWH ::




Custom Search