# Object Space Transformations Part 5: 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
### Object Space Transformations
Now that we know how to multiply matrices together, we can
implement the actual formulas used in our transformations. There are
three operations performed on a vector by a matrix transformation:
translation, rotation, and scaling.
Translation can best be described as linear change in position.
This change can be represented by a delta vector [dx, dy, dz], where
dx represents the change in the object's x position, dy represents the
change in its y position, and dz its z position.
If done correctly, object space translation allows objects to
translate forward/backward, left/right, and up/down, relative to the
current orientation of the object. Using our airplane as an example,
if the nose of the airplane is oriented along the object's local z
axis, then translating the airplane in the +z direction will make the
airplane move forward (the direction in which its nose is pointing)
regardless of the airplane's orientation.
Here is the translation matrix:
```
+= =+
| += =+ += =+ += =+ += += |
| | | | | | | | | |
| | 1 | | 0 | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | | 1 | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | | 0 | | 1 | | 0 | |
| += =+ += =+ += =+ | | |
| +===============+ | | |
| dy dx dz | 1 | |
| +===============+ += =+ |
+= =+
```
where [dx, dy, dz] is the displacement vector. After this operation,
the object will have moved in its own coordinate system, according to
the displacement (translation) vector.
The next operation that is performed by our matrix transformation
is rotation. Rotation can be described as circular motion about some
axis, in this case the axis is one of the object's local axes. Since
there are three axes in each object, we need to rotate around each of
them. Here are the matrices for rotation about each axis:
about the x axis:
```
+= =+
| += =+ += =+ += =+ += += |
| | | | | | | | | |
| | 1 | | 0 | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | |cx | |sx | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | |-sx| |cx | | 0 | |
| += =+ += =+ += =+ | | |
| +===============+ | | |
| 0 0 0 | 1 | |
| +===============+ += =+ |
+= =+
```
about the y axis:
```
+= =+
| += =+ += =+ += =+ += += |
| | | | | | | | | |
| |cy | | 0 | |-sy| | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | | 1 | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| |sy | | 0 | |cy | | 0 | |
| += =+ += =+ += =+ | | |
| +===============+ | | |
| 0 0 0 | 1 | |
| +===============+ += =+ |
+= =+
```
about the z axis:
```
+= =+
| += =+ += =+ += =+ += += |
| | | | | | | | | |
| |cz | |sz | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| |-sz| |cz | | 0 | | 0 | |
| | | | | | | | | |
| | | | | | | | | |
| | 0 | | 0 | | 1 | | 0 | |
| += =+ += =+ += =+ | | |
| +===============+ | | |
| 0 0 0 | 1 | |
| +===============+ += =+ |
+= =+
```
The cx, sx, cy, sy, cz, and sz variables are the values of the
cosines and sines of the angles of rotation about the x, y, and z
axes, respectively. Remeber that the angles used represent angular
displacement just as the values used in the translation step denote a
linear displacement. Correct transformation CANNOT be accomplished
with matrix multiplication if you use the cumulative angles of
rotation. I have been told that quaternions are able to perform this
operation correctly, however I know nothing of quaternions and how
they are implemented. The incremental angles used here represent
rotation from the current object orientation. In other words, by
rotating 1 degree about the z axis, you are telling your object
"Rotate 1 degree about your z axis, regardless of your current
orientation, and regardless of how you got to that orientation." If
you think about it a bit, you will realize that this is how the real
world operates. In object space, the series of rotations an object
undergoes to attain a certain orientation have no effect on the
object space results of any upcoming rotations.
Now that we know the matrix formulas for translation and rotation,
we can combine them to transform our objects. The formula for
transformations in object space is
```
[O] = [O] * [T] * [X] * [Y] * [Z]
```
where O is the object's matrix, T is the translation matrix, and X, Y,
and Z are the rotation matrices for their respective axes. Remember,
that order of matrix multiplication is very important!
The recursive assignment of O poses a question: What is the
original value of the object matrix? To eliminate any terrible errors
in transformation, the matrices which store an object's orientation
should always be initialized to identity. |