How to Read a 3×3 Matrix
Matrices are simple but like anything they take a bit of getting used to. I decided to write this article to help newcomers understand a bit more about them.
It is a summary of an explanation I’ve frequently used to develop intuition surrounding matrices. I’ll be assuming very little prior knowledge apart from that you are familiar with 3D applications (it’s not strictly necessary, but the analogies will make more sense). By the end, I’m hoping that you can look at a matrix and have a fair idea what it represents. It’s not an approach I’ve often seen, but try following it and see if it makes sense to you.
A 3×3 matrix is used by a 3D application to describe the orientation and a scale of an object in space. (Note that if you want to include the object’s position, then you need a 4×4 matrix, but we don’t want to go there yet.)
It is made up of 9 numbers arranged like this:
This particular 3×3 matrix is known as the “identity” matrix. We have zero rotation and a scale of 1. It doesn’t do anything to a point in space, no rotation or scale, so it’s a good place to start.
Okay, so this is the important bit. Instead of looking at this matrix as a bunch of numbers, let’s think about it as representing the axis icon in the corner of your XSI/Houdini/Maya viewport.
The first line of the matrix represents the X axis, the second line the Y axis, and the final line the Z axis. This makes more sense when we think of the end of each line of the matrix being a point in space. This means that each axis looks like this.
So the first line is a point in space that is 1 unit along the X axis, the second line is a point at 1 unit along the Y axis, and similarly for the final line 1 unit along Z.
So now, if we want to figure out what a particular matrix looks like, just imagine doing the transform to the axis icon and see what happens.
What would a 2x uniform scale do to the icon? It’d move each point to double it’s original position along each axis. Okay, well that’s easy to write:
The video clip below shows what happens to the axis icon when we scale it uniformly by a factor of 2. I’ve annotated the end of each axis with it’s position. In the top left you can see the three point positions in order, shown to appear as a 3×3 matrix.
Starting with the identity matrix again, what would a 90 degree rotation around the Y axis give us? Well, that depends which way we rotate it. Looking down on the Y axis, an anti-clockwise rotation would move the X axis to point along the negative Z axis, and the Z axis to point along the positive X axis.
So for each point, just visualise where it ends up and write down its position.
Yep, it’s that simple. Check out the short clip below to see it in action.
If you happen to know a bit of scripting, you should be able to obtain the matrix of an object in your 3D scene and check for yourself that this really works.
Reading the Matrix
So why don’t we try to take a matrix and work backwards to see if we can figure out what it represents.
Compare the first line to the first line of the identity matrix at the top of this article. It’s exactly the same, so we know the X axis is unchanged.
Looking at the second row, the Y axis is reversed and now pointing down. The third row shows that the Z axis is reversed too. How would you do this in your 3D software if you were going to use the transform tools?
Actually, there are two ways. You could either negatively scale the Y and Z axes with the scale tool, as shown in the following clip:
Or you could rotate it around the X axis by 180 degrees, shown below:
So are we able to say which one? No, the matrix only tells us about the destination, not the journey.
A word of warning, sometimes you’ll see matrices written in a different way. In this article, I have used the “row major” form where you can read the X, Y, and Z horizontally. Sometimes you’ll also see them written in “column major” format where you can read the axis vectors vertically instead. This can often lead to confusion when using the above technique.
If you research this further, it is usually stated (Wikipedia) that this difference in format only refers to the internal storage order of the numbers in computer memory, but I have come across situations where it has been written on the page in different ways too.
Using What You Know
Here’s just one quick example of how you can use this knowledge:
Q. Imagine we have a scene with a boat bobbing around on a rough sea. How do we find out how upright this boat is? Another way of thinking about this is how aligned is the boat’s y-axis is with the world’s y-axis?
A. If we have the boat’s 3×3 matrix, we know from the discussion above which row represents the y-axis of the boat (the middle row). With a bit of thought, we can figure out that if the boat is fully upright, this middle row of the matrix should read [ 0 1 0 ]. Taking this further, we know that all we care about is the height of this point (who cares if it’s offset to the side in x or z right?), so really we’re only interested in the y coordinate of this point, i.e. the middle value.
This value gives us the perfect measure of how upright the boat is. A value of 1 indicates it is perfectly upright, and -1 means it is completely upside-down.
Now that you’re hopefully familiar with the basic anatomy of a 3×3 matrix, the next step is to have a look at how it can be used to transform geometry. This isn’t a big leap; you’ve already seen visually how it works for the three points that lie on the axes.
After that, you’ll be more than ready to look at 4×4 matrices which incorporate translation as well, but this is all material for future articles.