• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Extracting only z rotation matrix from a rotation matrix

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1jenny_wui  Members

Posted 05 March 2014 - 07:53 PM

Hello,

I have a 3X3 rotation matrix. I have small 3D model. I only need to consider z rotation for that model, I don't need to consider xy movement or rotation. How can I extract the matrix that only consider z rotation of the model from the given rotation matrix?

### #2Álvaro  Members

Posted 05 March 2014 - 08:16 PM

There's no clear definition of what "the z rotation" means. Your best bet is to start with some sort of description of the rotation in terms of Euler angles and then to pick one of them. But there are many different possible setups, and I am not sure which one would map to whatever you think "the z rotation" means.

Edited by Álvaro, 05 March 2014 - 08:16 PM.

### #3steven katic  Members

Posted 05 March 2014 - 10:24 PM

yeah, exactly the first thing I thought Alvaro: what is jenny_wui's definition of z rotation.

However in the meantime:

searching the net on the title phrase such as "extract rotation from 3X3 opengl rotation matrix" will help.

This might help too: http://www.soi.city.ac.uk/~sbbh653/publications/euler.pdf

personally If I had a choice I would tend to store representations of rotations as variables to be placed into matrices as opposed to an angle extraction option.

### #4frob  Moderators

Posted 06 March 2014 - 12:01 AM

POPULAR

Your question has a lot of possible meanings, but it seems like you are asking how to find a basis function.

When you build a transformation matrix you start with an identity matrix and then add all your translations and rotations and shears to it.  You are probably familiar with it:

[ 1, 0, 0, 0 ]

[ 0, 1, 0, 0 ]

[ 0, 0, 1, 0 ]

[ 0, 0, 0, 1 ]

Now the weird part --- all of those values MEAN SOMETHING.  They aren't just magical numbers that mathematicians pulled from the ether.

There are actually sets of values usefully embedded in that matrix. You just need to know what they are.  I've labeled them A through F:

[ A, A, A, D ]

[ B, B, B, D ]

[ C, C, C, D ]

[ E, E, E, F ]

You might be using row-major or column-major matrix. The three values for "E" should match your translation, or the X,Y,Z coordinates of an un-rotated object.  If your coordinates are stored in the "D" vector then you'll need to do some mental work to flip the values along the diagonal.  The math still works, it is just stored in columns instead of rows.

If you only have the rotation part of it, parts A through C, everything still applies. You will still need to make sure you have are using row-major rather than column-major matrix types.

A, B, and C are called the "basis vectors". They define where the X axis, Y axis, and Z axis should point. D is the shear vector, it can add some tilt to the world. As mentioned already E should match the translation. F should be either 0 or 1; if it is 0 the matrix is a vector transformation (vectors have no position) meaning that operations should not multiply through the translation, they end up getting multiplied by zero and vanishing, if the value is 1 the matrix is a point transformation meaning that position should be considered.

So if we modify our identity matrix slightly, we get:

[ 1, 0, 0, 0 ]

[ 0, 0, 1, 0 ]

[ 0, 1, 0, 0 ]

[ 0, 0, 0, 1 ]

This means the Y and Z axis are swapped, or in other words, the transformation has rotated the object 90' to face a different axis. Instead of facing up into y, the transformation makes the model face out into z.  The second basis function "B" is the Y axis, and it is using the value that used to be in Z. The third basis function "C" is the Z axis, and it is using the value that used to be in Y.   So if the top of your model was two meters tall and it was located at [0, 2, 0], it is now laying on its side facing into the world at [0, 0, 2].

So what does that mean to you?

If you have a transformation matrix (or a 3x3 rotation matrix) and want to quickly know where Z is oriented, you can pull out the Z axis basis vector (marked as "C" above) and use that directly. Usually X is sideways ("A") Y is up ("B"), and Z is forward or backward ("C") making it easy to find object-relative directions.

Edited by frob, 06 March 2014 - 12:11 AM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #5Eck  Members

Posted 06 March 2014 - 09:06 AM

Wow frob. Thanks. I've been messing around with a ton of matrix operations lately and I understood the high level concepts. This post seems to explain away some of the magic and it's starting to make sense to me.

Very cool post.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

### #6Kryzon  Prime Members

Posted 06 March 2014 - 02:14 PM

If I'm not mistaken, you would extract only the Z rotation if you create a matrix with the inverse of all the other rotations (X or "pitch," and Y or "yaw") and leave the Z or "roll" row as 1.
Then when multiplying 'Original x Inverse,' you would obtain a matrix that has only the Z rotation present, with the other rotations set to identity (or "removed").
http://en.wikipedia.org/wiki/Transformation_matrix#Composing_and_inverting_transformations

A rotation matrix with only the Z rotation is a matrix with all the other rows set to identity, except the Z row.

Edited by Kryzon, 07 March 2014 - 04:20 PM.

### #7jenny_wui  Members

Posted 06 March 2014 - 08:13 PM

Hello, thanks a lot for the information. I would just like to double-check whether working ok. Please let me know in the following code, whether I've been able to transform "point" to "point_transformed_x" correctly using only the z basis vector/ If not please let me know how to do it the proper way.

Thank you very much.

/******************************************************/

Point3D point, point_transformed_x;

Point.x = 50.0;
Point.y = 70.0;
Point.z = 0.0;

// extracted Z basis vector for rotation matrix

double rotation_matrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
-0.0871186, -0.169623, 0.981651, 0,
0, 0, 0, 1};

Point_transformed_x = 50.0* rotation_matrix[0] + 70.0* rotation_matrix[1] + 0.0* rotation_matrix[2];

Point_transformed_y = 50.0* rotation_matrix[4] + 70.0* rotation_matrix[5] + 0.0* rotation_matrix[6];

Point_transformed_z = 50.0* rotation_matrix[8] + 70.0* rotation_matrix[9] + 0.0* rotation_matrix[10];

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.