Camera Transformations Part 6: Matrix Transformation Tutorial
Table of Contents
 Introduction
 Math Prerequisites
 Importance of Correct Transformations
 What a Matrix Represents
 Matrix Multiplication
 Transforming a Vector by a Matrix
 Object Space Transformations
 Camera Transformations
 Inverse Transformations
 Hierarchical Transformations
 Precision
 Conclusion
Camera Transformations
Good camera code is a key to a good vector engine. If the camera
does not move correctly in a firstperson type simulation, any hope of
realism is lost. Fortunately, camera code is very easy to implement
using matrices.
Each camera should contain one matrix which defines its current
orientation. Like the object matrix, the camera matrix (I call it
cmatrix) should be initialized to identity to avoid hideous errors.
The camera matrix is built in exactly the same way as the object
matrix, using CT, CX, CY and CZ. These translation and
rotation matrices are made in exactly the same way as their
object space counterparts, except for the obvious fact that
they are made using camera rotation and translation vectors.
Depending on your implementation, the components of the displacement
vectors may need to be negated. Sometimes all of the [dx, dy, dz] and
[xangle, yangle, zangle] need to be negated, sometimes some of them.
Once again, it depends on your implementation. If you find your
camera is moving forward instead of backward, right instead of left,
up instead of down, etc. you need to negate a vector component
somewhere. Once you get that straightened out, build the camera
matrix like this
[C] = [C] * [CT] * [CX] * [CY] * [CZ]
Once you have made the camera matrix, you can transform your object
matrices to camera space with a simple matrix multiplication
[E] = [O] * [C]
where [E] is the eyespace matrix, [O] is the object space matrix for
the object, and [C] is the camera space matrix for the camera you are
using (which we calculated above).
If you are an incredibly astute individual, you will have realized
that I have left a step out of this process. Where did I add the
world coordinates of the object's origin? The translation in the
object matrix is a displacement which is added to the origin of
the object, but what of the original value of the object's origin?
The reason I left this step out until now is because it is easiest
to perform this operation in the middle of transforming an object to
eyespace. Something to remember is that the object origin is
expressed in terms of world space coordinates. If we want our
transformations to be correct, we have to add world space coordinates
to world space coordinates, adding them to camera or object space
coordinates would give us incorrect results. The only time we have
world space coordinates in this process is after object transformation
is complete. At this time, we can add the coordinates of the object's
origin to the origin vector found in the transformation matrix. Here
is some example code illustrating this process. omatrix contains the
object space transformation matrix, cmatrix contains the camera
transformation matrix. ematrix will be the result of omatrix * cmatrix.
(initialize tmatrix, xmatrix, ymatrix, zmatrix...)
...
matmult(omatrix, tmatrix); // translate the object
matmult(omatrix, zmatrix); // rotate about z
matmult(omatrix, xmatrix); // rotate about x
matmult(omatrix, ymatrix); // rotate about y
initmatrix(ematrix); // initialize eye space matrix to identity
matmult(ematrix, omatrix); // copy the object space matrix to eye space
ematrix[3][0] += origin>lx; // add origin vector
ematrix[3][1] += origin>ly;
ematrix[3][2] += origin>lz;
matmult(ematrix, cmatrix); // convert world space to eye space
