# How to extract a 3d rotation from a matrix?

Tanks!

And by the way… how can i get the current Matrix.

Tanks again!

Haaa HA!

okay, first you gotta call

``````float Matrix[16];
glGetFloatfv( GL_MODELVIEW_MATRIX,&Matrix );
``````

that’ll get you your 4x4 matrix, set up like this (in hex offsets)
0 1 2 3
4 5 6 7
8 9 A B
C D E F

soo, that means that 5 is also (1,1), k? 6 is (1,2), alright?

to factor as a product of RxRyRz, heres what you do

it come from the fact that a rotation matrix
composed of an Rx x Ry x Rz rotations has the form

given,
sx = sin x
sy = sin y
sz = sin z
cx = cos x
cy = cos y
cz = cos z
the matrix is
[ cycz -cysz sy 0 ]
[ czsxsy + cxsz cxcz - sxsysz -cysx 0 ]
[ -cx
czsy + sxsz czsx + cxsysz cxcy 0 ]
[ 0 0 0 1 ]

sure, the matrix is different if you use RxRzRy, etc, but it’s all the same idea.

basically, you just look in there for ways of canceling out products, and using inverse trig functions.

like for instance, Ry = ArcSin( sy );, right?
well, given that, here’s the code:

``````Ry = ArcSin( Matrix[2] );
if( Ry < Pi/2 )
{
if( Ry > -Pi/2 )
{
Rx = ArcTan( -Matrix[6]/Matrix[10] );
Rz = ArcTan( -Matrix[1]/Matrix[0 ] );
}
else
{
Rx = -ArcTan( Matrix[6]/Matrix[10] );
RZ = 0;
}
}
else
{
Rx = ArcTan( Matrix[6]/Matrix[10] );
Rz = 0;
}
``````

okie dokie?

it’s some pretty neat math stuff.

remember, this only works if the matrix is only an RxRyRz matrix. it won’t work if there’s scaling, i don’t think, but translations are ok.

if you know what the scaling is, you can also just divide it out.

hope that helps

-Succinct

[This message has been edited by Succinct (edited 11-30-2000).]

It helps me a lot, and i’ll be able to to what i want now

Tanks again!

Well… all your stuff is working! I thought it was my solution to correct my problem. But it seems not. (it was a good formatio though )

Well… here is my probleme just in case that someone will say “Hey! i know what it is!”

My real problem is that i have a 3d objet, that i draw on a opengl view. This object get transformed with the following matrix transformation: glRotated and glTranslate.

I have a point, in that mesh, that i must calculate mannualy. So i though i just get the rotation of my mesh, and rotate my point with that rotation to get the position, but it is not working. It work only if i rotate only on one axis. And it also work when i rotate it over the XY, and XZ. But XYZ and YZ rotation is not working.

So i though it was something in the Matrix that i do not understand, so i asked how to get the rotation of a matrix, and use this rotation to rotate my point, but it do the same thing.

I am desesperate… Tanks in advance

Best Regard!

although i don’t understand the exact meaning
of “I have a point, in that mesh, that i must calculate mannualy…”, what i suppose is that u want to find the position of a specific point in the modelview space after the transformation, if u have its position before…
If that is the case, then the answer is simple: u just have to multiple the modelview matrix with (x y z 1) where x,y,z are the coords of the point before the transform.

But if you want to calculate the position of the point after rendering - in 2D coords, then you must multiple the result with projection matrix, and the to apply the viewport transform (see glViewport for more info). Another solution is to use feedback mode in OpenGL (see glRenderMode()) - but this is too complicated for just one point

[This message has been edited by martin_marinov (edited 12-01-2000).]