Game Development Reference
In-Depth Information
There are three sections: the modifiers, the accessors and transforms, and finally the
initializers. The modifiers simply set position; if you want to set rotations, there ' s
another way I
ll show you in a moment. The accessor GetPosition() returns the
position component of the 4 × 4 matrix. The Xform() methods transform a Vec3 or
Vec4 object into the space and position of the matrix. Don
'
'
t worry yet because I
'
ll
show you an example of how to use this in a moment.
The initializer methods, those starting with Build , take various parameters you
might have on hand to build a rotation or transform matrix. If you want one that
encodes both rotation and transformation, just build two of them and multiply
them. Multiplying matrices is the same thing as concatenating them.
There
s also a static member, g_Identity , which forms a matrix that you use to set
an object at the origin with no scaling or rotation.
Here ' s a quick example in C++ that does the following things:
'
n Builds two matrices, one for rotation and one for translation.
n Concatenates these matrices in one Mat4 × 4 to encode both movements.
Remember that rotation always comes first and then translation.
n Determines which direction in the 3D world is considered
by the new
orientation and position. This direction is sometimes referred to as a frame or
reference.
forward
Mat4x4 rot;
rot.BuildYawPitchRoll(D3DX_PI / 2.0f, -D3DX_PI / 4.0f, 0);
Mat4x4 trans;
trans.BuildTranslation(1.0f, 2.0f, 3.0f);
// don
'
t mess up the order! Multiplying Mat4x4s isn
'
t like ordinary numbers
.
Mat4x4 result = rotOnly * trans;
Vec4 fwd(0.0f, 0.0f, 1.0f);
// forward is defined as positive Z
Vec4 fwdWorld = result.Xform(fwd);
There you have it. The fwdWorld vector points in the forward direction of the trans-
form matrix. This is important because of two reasons. First, all of the code in this
chapter will continue using these math classes, and this is exactly how you
d tell a
missile what direction to move if you fired it from an object that was using the
concatenated matrix.
I hope you
'
sa
critical concept you need to understand before we talk about quaternions. If you
'
ve followed these bits about rotating things around an axis because it
'
Search WWH ::




Custom Search