# Introduction Part 1: 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
### Introduction
It has been entirely too long since I wrote my last doc, I just
had to make another one :) This is a topic which I have spent a great
deal of time teaching lately, so I decided to make a doc that would
expedite the process for me.
If you are looking for advice from a certified expert, you will
not find it in this doc. I am merely an individual who has spent the
past year studying realtime rendering, and who is willing to share
what he has learned. However, you should rest assured that I do not
write docs about things I have never implemented in working code, and
I never code anything I don't fully understand. In other words, I
think I know what I am talking about here :)
All of the techniques in this file can be implemented regardless
of the programming language you use, from assembler to C to Visual
Basic. I will, however, be giving any pseudocode examples in C,
because it seems to be the universal language of coders right now.
For the sake of simplicity, all code examples will use floating point
math. Floating point is the wave of the future, as a matter of fact
its faster than fixed point integer math on the newest RISC machines
(PowerPC, DEC Alpha). Unfortunately, Intel users have been given
hideously slow floating point processors in the past and are less than
confident in the ability of their machines; but things will get better
very shortly.
### Math Prerequisites
Sadly, matrix math is something that is all but ignored in most
high school curricula in the United States. But hey, the best stuff
is skipped in high school, everybody knows that :) You should not be
afraid of matrix math just because your high school math teacher does
not teach it. Now that I think about it, matrix math is probably
skipped in high school simply because there is nothing taught in high
school that applies its principles.
The only matrix math I was taught in high school was for solving
systems of equations. Basically, we were taught that a matrix is a
simple and powerful way to represent a complex system of equations.
This is a great explanation, although extremely simplistic. Keep this
in mind throughout the course of this doc.
The math of matrices is very simple. Nothing higher than first
year algebra is used, although an understanding of trigenometry will
make your life much easier when it comes to understanding the meaning
of all those numbers in a matrix. If you have had a course in Linear
Algebra at a university, this doc will probably be old news to you.
### Importance of Correct Transformations
When I first began coding vector graphics, I had a great
understanding of trigenometry and anything but fond memories of matrix
math as it was presented in my high school courses. To me, matrix
math seemed to be an unneccessarily complex way to solve a simple
algebraic problem, like trying to swat a fly with an elephant gun.
Indeed, many algebraic problems can be solved without the use of
matrix math.
However, vector graphics pose a system of equations that are
anything but simple to solve algebraically. Consider the following:
in a typical vector world, there are many objects. Each of these
objects moves in its own space (object space), relative to its own set
of coordinate axes. There are several cameras, each of which moves in
its own space (camera space or eyespace) according to its own set of
coordinate axes. At some point, the objects must be transformed from
object space to eyespace so they can be displayed as the eye sees
them. Apart from this, there is the issue of object hierarchies. In
a realistic vector environment, object hierarchies must be handled
correctly. These issues present formidable challenges without the use
of matrix math. However, by using matrix math we can deal with all
of these issues in a simple and speedy manner.
Granted, I am assuming that everyone wants to make simulation
quality vector code. This type of code requires correct
transformations. Speaking from a demo scene point of view, very few
demos implement correct transformations simply because they are not
required for the application. The stereotypical vector scene in a
demo is a childish attempt to display speed and pretty rendering on a
single object. Most demo coders do not care if their objects are
moving correctly, they just want them to move around a bit. This
attitude is fine, assuming you never want to do anything with your
code besides show a spinning cube or toroid.
Any impressive vector application (game, simulator, etc.) requires
correct transformations. Consider the following example. An airplane
is oriented such that its nose is pointing in the positive z
direction, its right wing is pointing in the positive x direction, its
cockpit is pointing in the positive y direction. The airplane's local
x, y, and z axes are aligned with the world x, y, and z axes. If this
airplane were rotated 90 degrees about its y axis, its nose would be
pointing toward the world -x axis, its right wing pointing toward
the world +z, and its cockpit remaining in the world +y direction.
Most transformations, whether correct or incorrect, would accomplish
this. Here is the 'acid test' to see whether your transformations are
correct. From this new position, rotate the airplane about its z
axis. If your transformations are correct, the airplane will rotate
about its own z axis (it will roll). If your transformation is
incorrect, the airplane will rotate about the world z axis (its nose
will pitch up or down).
This rather silly example poses quite a serious question. If your
transformations are not correct, how will you control object movement
in a vector world? How will you guarantee your airplane will roll
when you tell it to instead of pitching? The same problem arises with
incorrect camera rotation. The consequences of such incorrectness in
a flight simulator or game would make the game unplayable.
The solution to this problem is simple; the use of 4x4 matrix transformations. |