## ::Matrix Anatomy::

September 21st, 2010 by hamish download the zooToolBoxSo I’m working off the assumption that you already know what vectors are and how to do basic arithmetic with them. Matrices are a little less well known, although its also pretty easy to get information on basic matrix arithmetic so I won’t go into that here either – although if you really want me to cover this please speak up in the comments! If enough folks want to see some tutorials on basic matrix operations I’ll write some of them too.

What I will go into is what matrices are and why and how to use them. Well, I’ll try anyway.

So matrices are a pretty general concept. They’re used anywhere that needs to programmatically (sp?) represent 3d transformations. The cases we care about are their applications in maya or whatever other app you’re into. Some applications use “row major” others use “column major” for their transformations. One is basically the transpose of the other. Maya is row major, so keep that in mind if whatever application you’re using isn’t (I know Blender is column major). It doesn’t make much of a difference to anything I’m talking about here except that if you’re using a column major app, whenever I say **row**, read it as **column**.

Anyway, so basis vectors seem like a good place to start. Basis vectors are basically a transform’s x, y and z axes after its transformation has been applied. So if you rotate an object by 90 degrees around the y axis, then that object’s basis vectors would be:

x=<0,0,-1> y=<0,1,0> z=<1,0,0>

Try it out – rotate an object in maya 90 degrees in Y, then look at the object’s rotation axes. You’ll see the X axis facing down the negative Z of the world and the Z axis pointing in the direction of the world X axis.

So these are the basis vectors for the transform in world space. Now if you have basis vectors for the transform, you have enough information to figure out the rotation. Ie from the above 3 vectors, you could figure out that the rotation was 90 degrees around Y. I’ll go into converting a matrix into rotations in another post – for now I want to do a bit of “matrix anatomy”.

So transformation matrices are quite cool because they quite literally have an anatomy. Understanding this anatomy enormously helps clear away the confusion surrounding matrices. Lets take the rotation we talked about above (ie r=0, 90, 0) and turn it into a matrix:

| 0, 0, -1, 0 | | 0, 1, 0, 0 | | 1, 0, 0, 0 | | 0, 0, 0, 1 |

This, literally, is the matrix for an object sitting at origin rotated 90 degrees around the Y axis. Now how cool is that? As you can see, the top 3×3 part of the matrix are just the basis vectors we wrote above. And this is generally true for all transformation matrices. The top left 3×3 part of the matrix contains the basis vectors for the transform. This 3×3 part of the matrix is the rotation matrix because it completely describes the rotation. It can be converted to either euler angles, or a quaternion should you need either of them.

Thats the first interesting piece of matrix anatomy, and it will take you a long way. It also means that you can pretty easily construct your own rotation matrices by doing a little bit of vector math. Vectors are far easier to understand than rotations, and when you come to grips with the fact that rotations are indeed just a set of vectors arranged in such a way, then you’re home free.

Now for a few important points. For the matrix to be a proper rotation matrix you need to make sure of a few things:

- Your vectors need to be orthogonal to one another
- They need to be normalized

If you don’t know what that means do some searching on the web on how to normalize a vector, and how to determine whether two vectors are orthogonal to one another (hint: their dot product is zero!). So as long as each basis vector is orthogonal to each of the other basis vectors AND each basis vector is normalized, you have yourself a rotation matrix.

Now this post is getting kinda long, but I wanted to cover one more piece of matrix anatomy because its super easy. The position of a transform is stored quite literally in the first 3 items of the last row. So just say we wanted to have our transform in the above example positioned at <1, 2, 3> in xyz. Our transformation matrix would look like this:

| 0, 0, -1, 0 | | 0, 1, 0, 0 | | 1, 0, 0, 0 | | 1, 2, 3, 1 |

Easy peasy eh? Now scale can make things a little more confusing – and I’ll get into that in following posts. But if you’re not dealing with scale you can go really far with the above bits of knowledge.

Now please leave feedback on the post – let me know what makes sense and what needs clarification and I’ll try to do that in follow up posts. In the next post I’ll get into a bit more matrix anatomy (there really isn’t much left!) and cover some basic matrix arithmetic which will hopefully clear up why we use matrices in 3d, and then probably one on how to do things like converting from local space to world space, or converting transforms from one space to another. Other posts I’ll probably do include dealing with scale, converting a rotation matrix to euler angles or a quaternion and maybe even some application type tutorials such as how to write an aim constraint, or how to mirror a rotation.

This is post 1 in the matrix series.

This post is public domain

This entry was posted on Tuesday, September 21st, 2010 at 19:58 and is filed under main, tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Pingback: Miguel González Viñé | LichiMan | BLOG