::Matrix Anatomy::

September 21st, 2010 by hamish download the zooToolBox

So 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:


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:

  1. Your vectors need to be orthogonal to one another
  2. 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.

  • http://morganloomis.com Morgan Loomis

    That’s great Hamish, thanks, it’s helping already! I’m looking forward to learning about converting local to world, and mirroring rotation. It’d be great if we could see some python or MEL examples of how to deal with matrices, if you think that’s appropriate. Thanks again dude!

  • http://3devartist.com Michael Trainor

    Thanks! Always appreciated. Can’t wait for more.

  • http://www.lichiman.com/ Miguel

    Thanks Hamish, the explanation was very clear!
    I would really appreciate the aim constraint tutorial in the future. I saw the Adam Mechtley’s python plugins for shoulder and hip constraints and they’re great, but I can’t understand some parts of how it works when I look at the python code.
    Thanks again.

  • hamish

    If you’re interested in code – take a look at my vectors module here:

    the module isn’t “official” yet, as it still needs a bit of house keeping, but I use this on a daily basis for matrix operations. Obviously maya has their own matrix and vector classes, but I write code that has to work inside maya and outside, so I can’t rely on their implementations for everything.

    But I will try to include some code examples using these classes in subsequent posts.

    Thanks for the feedback!

  • http://none Meto

    Thank you so much Hamish. I am looking forward to see the next tutorial.

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

  • http://www.evadanimation.com Evad

    Sometimes the best tutorials are written by those who just struggled through it. The problem I’ve found when trying to learn 3d math is that all the information is written people who have a high level understanding of it, so it is not written in a way that makes sense to the beginner. This is good so far. I just started reading, can’t wait to get to the rest of the posts. Thanks for this.

  • http://www.direenworks.com James

    It is really cool that you took the time to clearly and concisely write this out. Thank you!

  • hamish

    thanks guys! Glad its useful to folks.

    Evad: I took a peek at your character tools – they look remarkably similar to the tools I’ve been developing. Which I guess is hardly surprising as it seems like a pretty obvious idea to me, but great to see similar thinking in action! nice work!

  • David

    Finally, a really cool tutorial for beginners! I looked for many tutors about matrices but this was absolute clear! Thx.

  • Cyberika

    Thank you for such explicit explanation! I agree with the post below. Most of tutorials are done by math experts but the laguage of explanation is very hard to understand wen you are not mathematician