Rotation matrix and rotation vector

Discussion about everything. New games, 3d math, development tips...
Post Reply
madinitaly
Posts: 92
Joined: Sat Nov 29, 2003 8:30 pm
Contact:

Rotation matrix and rotation vector

Post by madinitaly »

First of all, I must admit my narrowess in math questions. Now...
Let's say I have three vectors specifying the rotation for an object, defining a rotation matrix R:
[ R1,1 R1,2 R1,3 0 ]
[ R2,1 R2,2 R2,3 0 ]
[ R3,1 R3,2 R3,3 0 ]
[ 0 0 0 1 ]

And I also have the (relative) position of that object in the form of another "XYZ" vector, so that they make the translation matrix T:
[ 1 0 0 X ]
[ 0 1 0 Y ]
[ 0 0 1 Z ]
[ 0 0 0 1 ]

I can now combine the two and obtain the translation matrix Tr:
[ R1,1 R1,2 R1,3 X ]
[ R2,1 R2,2 R2,3 Y ]
[ R3,1 R3,2 R3,3 Z ]
[ 0 0 0 1 ]

Now, how can I obtain a simple XYZ vector containing angles of rotation around the respective axes?
Pratically, I think it's the inverse operation of irr::core::matrix4::setRotationDegrees (vector3df& rotation)


P.S.: I may be wrong or even delirious.
madinitaly
Posts: 92
Joined: Sat Nov 29, 2003 8:30 pm
Contact:

Post by madinitaly »

Uhm I actually didn't explain it well (flu and my bad english are a lethal mix).
What I need is to find the angles to rotate an object in order to face the direction specified by the rotation matrix R.
All I know is how to do the inverse: obtaining the rotation matrix from three angles. Inverting the process is not so trivial :cry:
Chev
Posts: 37
Joined: Wed Nov 19, 2003 6:15 am

Post by Chev »

I had the same problem, I wrote this function to do as you described. Maybe if niko reads this he can add a cleaner version of this to the standard matrix4 class. It really helps those of us that do some of our own matrix operations. I am a beginner in c++ so you might want to change some of this. The tabs are a bit off after pasting into the bb.

Most of this code I found on http://skal.planet-d.net/demo/matrixfaq.htm#Q2 then adapted it to Irrlicht.

Code: Select all

vector3df getRotationDegrees(matrix4 matrix)
{
	matrix4 mat=matrix;
	vector3df rotation;
	f32 D,C,Y,rotx,roty,X,Z;

	Y = D = -asinf( mat(2,0));
    C     =  cosf( Y );
    Y    *= 180.000f/PI;

    if ( fabs( Y ) > 0.0005f )
      {
      rotx      =  mat(2,2) / C;
	  roty      = mat(2,1)  / C;
      X  = atan2f( roty, rotx ) * 180.0000f/PI;
      rotx      =  mat(0,0) / C;
      roty      = mat(1,0) / C;
      Z  = atan2f( roty, rotx ) * 180.0000f/PI;
      }
    else
      {
      X  = 0.00000f;

      rotx      = -mat(1,1);
      roty      = mat(1,0);

      Z  = atan2f( roty, rotx ) * 180.0000f/PI;
      }

	if (fabs(X)>=360.00f)X=0.00f;
	if (fabs(Y)>=360.00f)Y=0.00f;
	if (fabs(Z)>=360.00f)Z=0.00f;

	rotation=vector3df(X,Y,Z);
	return rotation;
}
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Surely useful. I will try it out and then maybe built it in. Thanks :)
madinitaly
Posts: 92
Joined: Sat Nov 29, 2003 8:30 pm
Contact:

Post by madinitaly »

Nice! I'll check my formulas and compare them with your code (at a first look, it seems perfect, anyway :wink: )
Thank you!
Post Reply