Game Development Reference
In-Depth Information
Clearly converting the local Z-axis direction vector into world coordinates, for an
object that has no rotation, should give us the Z-axis direction vector. Directions
should not change magnitude, and if there is no rotation, they should not change at
all.
In other words, direction vectors should be immune to any translational compo-
nent of the transformation matrix. We can do this only by multiplying the vector by a
3
3 matrix, which ensures that there is no translational component. Unfortunately
this will be inconvenient at several points, because we will have gone to the trouble of
building a 3
×
4 transform matrix, and it would be a waste to create another matrix
just for transforming directions. To solve this we can add two specialized methods
to the Matrix4 class to deal specifically with transforming vectors. One performs the
normal transformation (from local to world coordinates), and the other performs the
inverse (from world to local coordinates).
×
Excerpt from include/cyclone/core.h
/**
* Holds a transform matrix, consisting of a rotation matrix and
* a position. The matrix has 12 elements; it is assumed that the
* remaining four are (0,0,0,1), producing a homogenous matrix.
*/
class Matrix4
{
// ... Other Matrix4 code as before ...
/**
* Transform the given direction vector by this matrix.
*/
Vector3 transformDirection(const Vector3 &vector) const
{
return Vector3(
vector.x * data[0] +
vector.y * data[1] +
vector.z * data[2],
vector.x * data[4] +
vector.y * data[5] +
vector.z * data[6],
vector.x * data[8] +
vector.y * data[9] +
vector.z * data[10]
);
}