Game Development Reference
In-Depth Information
Figure 3-23. Model matrix
The important thing to understand about matrix multiplication is that the order of multiplication
matters. That is, matrix multiplication is not commutative. Thus, AB does not equal BA.
For example, if you want to rotate an object around an axis then translate it, you need to have the
rotation matrix on the right-hand side and the translation matrix on the left-hand side. In code, this
would look like the following:
// Rotates object around Axis then translates it
// public static void multiplyMM (float[] result, int resultOffset,
// float[] lhs, int lhsOffset,
// float[] rhs, int rhsOffset)
// Matrix A Matrix B
Matrix.multiplyMM(TempMatrix, 0, m_PositionMatrix, 0, m_RotationMatrix, 0);
The multiplyMM() function multiplies two matrices A and B. In terms of effects, the matrix B is
applied first then matrix A is applied. Thus, the above code first rotates the object around its rotation
axis and then translates it to a new position.
Thus, the ModelMatrix in Figure 3-23 is set up to first scale an object, then rotate the object around
its axis of rotation, and then translate the object.
Hands-on Example: Manipulating Objects in 3D Space
In this hands-on example, we will concentrate on manipulating a 3D object's position, rotation, and
scaling to demonstrate the concepts of vectors and matrices covered in this chapter.
This example uses some vector functions like Negate() , presented earlier. Please make sure to
add these functions, as well as other functions you wish to experiment with, into the Vector3 class
from Chapter 2. You can also find the code for this example in the Source Code/Download area of
apress.com .
Building a 3D Object's Model Matrix
The Orientation class holds data for a 3D object's position, rotation, and scaling. It also calculates
the object's model matrix, which contains the object's position, rotation, and scaling information
(refer to the preceding Figure 3-23 ).
 
Search WWH ::




Custom Search