Game Development Reference

In-Depth Information

* the given quaternion.

*/

void setOrientationAndPos(const Quaternion &q, const Vector3 &pos)

{

data[0] = 1 - (2*q.j*q.j + 2*q.k*q.k);

data[1] = 2*q.i*q.j + 2*q.k*q.r;

data[2] = 2*q.i*q.k - 2*q.j*q.r;

data[3] = pos.x;

data[4] = 2*q.i*q.j - 2*q.k*q.r;

data[5] = 1 - (2*q.i*q.i + 2*q.k*q.k);

data[6] = 2*q.j*q.k + 2*q.i*q.r;

data[7] = pos.y;

data[8] = 2*q.i*q.k + 2*q.j*q.r;

data[9] = 2*q.j*q.k - 2*q.i*q.r;

data[10] = 1 - (2*q.i*q.i + 2*q.j*q.j);

data[11] = pos.z;

}

};

9.4.5

T
RANSFORMING
V
ECTORS

In section 9.1.3 we looked at finding the position of part of an object, even when the

object had been moved and rotated. This is a conversion between object coordinates

(i.e., the position of the part relative to the origin of the object and its axes) and world

coordinates (its position relative to the global origin and direction of axes).

This conversion can be performed by multiplying the local coordinates by the

object's transform matrix. The transform matrix in turn can be generated from the

quaternion and position as we saw earlier. We end up with a 3

4 transform matrix.

Working out the world coordinates, given local coordinates and a transform matrix,

is a matter of simply multiplying the vector by the matrix:

×

Vector3 localToWorld(const Vector3 &local, const Matrix4 &transform)

{

return transform.transform(local);

}

The opposite transform, from world coordinates to local coordinates, involves

the same process but using the inverse of the transform matrix. The inverse does the

opposite of the original matrix: it converts world coordinates into local coordinates.