Saturday, December 8, 2007

Visualizing Transformation Matrices

When working in 3D, you have to learn to love, or just tolerate, transformation matrices. You'll see them & use them all over the place. Whenever you find yourself face to face with a series of 16 floats, it's really handy to be able to build a mental picture of what that matrix represents.

A typical transformation matrix looks like this:
Not very helpful. But by partitioning the matrix into groups, you can get a feel for what's going on. First let's take a look at the last column.
Most of the time you'll be dealing with object transforms so this group should be [0, 0, 0, 1]. If it's not, you're either looking at a projection matrix, or things have gone very very wrong.
The green group represents the translation. [m=x, n=y, o=z]. This is where the pivot point of your object will be. If you transform zero, [0 0 0 0], by this matrix, the result will end up at this position in world space.
This blue grouping is a bit trickier because it represents both orientation and scale. But you can further subdivide it further to get a better feel for whats going on.
Each of these three vectors represent where the principle 3 axes will end up AFTER being transformed. Try it, take the vector [1 0 0 1] and multiply it this matrix. You'll end up with [a b c 0].

By looking at these vectors you will be able to tell which way the object will be oriented in world space.

For example: if you have a rocket-ship that's pointed along the x-axis and z is up.
And you have a rotation matrix that looks something like this:
You can tell by looking at the first row which way the rocket ship will be facing in world coordinates, along the z-axis, straight up. Similarly by looking at the third row, you can tell what direction the top of the rocket will be facing, along the y axis.

By looking a the last row of the matrix [0 0 9 0], you can see the rocket is 9 units off of the ground. So the rocket-ship is pointing upward and is off the ground, just like it's taking off from it's launch pad.


Now what about scale? Well typically the length of the red [a b c] vector is one. But what happens if it's not? It will compress or stretch vertices along the x-axis. Similarly, the length of the green [e f g] vector will scale along the object's y-axis. If you want a long thin rocket, just make the magnitude of the [a b c] vector larger.

No comments: