Game Development Reference

In-Depth Information

Vector3 worldToLocal(const Vector3 &world, const Matrix4 &transform)

{

Matrix4 inverseTransform;

inverseTransform.setInverse(transform);

return inverseTransform.transform(world);

}

If the transform matrix is made up of only a rotation and a translation (as it

should be for our needs), we can do this in one step.

We split the 3

4 matrix into two components: the translation vector (i.e., the

fourth column of the matrix) and the 3

×

3 rotation matrix. First we perform the

inverse translation by simply subtracting the translation vector. Then we make use of

the fact that the inverse of a 3

×

×

3 rotation matrix is simply its transpose, and multiply

by the transpose.

This can be done in one method that looks like this:

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 vector by the transformational inverse

* of this matrix.

*/

Vector3 transformInverse(const Vector3 &vector) const

{

Vector3 tmp = vector;

tmp.x -= data[3];

tmp.y -= data[7];

tmp.z -= data[11];

return Vector3(

tmp.x * data[0] +

tmp.y * data[4] +

tmp.z * data[8],

tmp.x * data[1] +