1. Matrix Rotation
The final effect I’m going to describe is rotation. Rotation
is the act of turning an object around a certain axis. This allows
objects to change their orientation within space. For instance, if you
wanted to rotate a planet, you would create a rotation matrix and apply
it to the planet.
Rotation matrices take a little more explanation
because the matrix needs to change based on the axis you’re trying to
rotate around.
Rotate around the X Axis
When rotating around any axis, it will require
you to convert the angle of rotation into both a sine and cosine value.
These values are then plugged into the matrix to cause the proper amount
of rotation.
When rotating around the X axis, the sine and cosine values are placed into the matrix in the following manner.
float RotateXMatrix [4][4] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cosAngle, sinAngle, 0.0f,
0.0f, -sinAngle, cosAngle, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
The Direct3D function D3DXMatrixRotationX will create a rotation matrix along the X axis.
D3DXMATRIX * D3DXMatrixRotationX(
D3DXMATRIX * pOut,
FLOAT Angle
);
Rotate around the Y Axis
Rotating around the Y axis (see Figure 1)
requires only that the sine and cosine values be moved to different
positions within the matrix. In this instance, the values are moved to
affect the X and Z axes.
float RotateYMatrix [4][4] = {
cosAngle, 0.0f, -sinAngle, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
sinAngle, 0.0f, cosAngle, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
The D3DXMatrixRotationY function will generate a rotation matrix around the Y axis.
D3DXMATRIX * D3DXMatrixRotationY(
D3DXMATRIX * pOut,
FLOAT Angle
);
Rotate around the Z Axis
The
final rotation matrix allows for rotation around the Z axis. In this
instance, the sin and cosine values affect the X and Y axes.
float RotateZMatrix [4][4] = {
cosAngle, sinAngle, 0.0f, 0.0f,
-sinAngle, cosAngle, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
The D3DXMatrixRotationZ function will generate a rotation matrix around the Z axis.
D3DXMATRIX * D3DXMatrixRotationZ(
D3DXMATRIX * pOut,
FLOAT Angle
);
2. Multiply Matrices
The
last thing you need to learn about matrices is how to combine them.
Most of the time, you won’t need to only translate an object, you’ll
need to scale it or maybe rotate it all in one go. Multiple matrices can
be multiplied, or concatenated, together to create a single matrix that
is capable of containing the previously single calculations. This means
that instead of needing a matrix for rotation as well as one for
translation, these two can be combined into one.
Because the D3DXMatrix type overrides the multiplication operator, the act of multiplying two D3DXMatrix objects together is easy. The following single line of code demonstrates this.
D3DXMatrix finalMatrix = rotationMatrix * translationMatrix;
A new matrix called finalMatrix is
created, which now contains both the rotation and translation. This new
matrix can then be applied to any object going through the pipeline. One
thing to mention though is to watch out for the order in which you
multiply matrices. Using the previous line of code, objects will be
rotated first and then translated away from that position. For example,
rotating the object 90 degrees around the Y axis and then translating it
four units along the X axis would cause the object to be rotated in
place at the origin and then moved four units to the right.
If the translation came first, the object would be
translated four units to the right of the origin first and then rotated
90 degrees. Think what would happen if you extended your hand to your
right and then turned your body 90 degrees—where would your hand end up?
Not exactly in the same place as before; you need to make sure that the
order in which you multiply the matrices is the order you really want
them applied.
If you don’t like using the multiplication operator to combine matrices, you can use the Direct3D function D3DXMatrixMultiply instead.
D3DXMATRIX * D3DXMatrixMultiply(
D3DXMATRIX * pOut,
CONST D3DXMATRIX *pM1,
CONST D3DXMATRIX *pM2
);
The D3DXMatrixMultiply
function takes three parameters. The first parameter is a pointer to
the output matrix to be created. The second and third parameters are the
matrices you want to multiply. Using this function may make your code
slightly easier to follow.
Note
Matrix multiplication is
not cumulative. Multiplying Matrix A by Matrix B does not result in the
same output matrix as multiplying Matrix B by Matrix A. The order in
which matrices are multiplied is important.