Fixed matrix4::getRotationDegrees()! (matrix to euler)

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Fixed matrix4::getRotationDegrees()! (matrix to euler)

Post by jox »

After AGES (no kiddin) of debugging I'm proud to present to you:

A (properly) working Matrix to Euler Angels conversion!! (matrix4::getRotationDegrees()) :D

This was a hard one because a) it took a while to find out that the bug is actually in that function b) the function was working "a little bit". Like sometimes you would get proper results, sometimes not. And c) because it's hard to tell if the result is ok, because the conversion from matrix to euler is not always unique.

To visualize I took 2 reference objects (each consisting of a red x, a green y and a blue z arrow according to the axes), placed them next to each other and rotated them arbitrarilly in all axes in a wild fashion (via the keyboard). The first directly with euler angels, the second with the result of getRotationDegrees(matrix.setRotationDegrees(theSameEulerAngels)). The old function would not always be in sync.

Here's an impression:

Image

Also I made everything double precision (f64) for better accuracy!

Now here is the fixed function with the changes pointed out:

matrix4.h:

Code: Select all

inline core::vector3df matrix4::getRotationDegrees() const
{
	const matrix4 &mat = *this;
	
	f64 Y = -asin(mat(2,0)); 
	f64 D = Y;
	f64 C = cos(Y); 
	Y *= GRAD_PI; // <- this was "180.000f/PI" before
	              //   (not part of the bug)

	f64 rotx, roty, X, Z;

	if (fabs(C)>0.0005f) // <- C not Y
	{ 
		rotx = mat(2,2) / C; 
		roty = mat(2,1)  / C; 
		X = atan2( roty, rotx ) * GRAD_PI; 
		rotx = mat(0,0) / C; 
		roty = mat(1,0) / C; 
		Z = atan2( roty, rotx ) * GRAD_PI; 
	} 
	else 
	{
		X  = 0.0f; 
		rotx = mat(1,1); // <- no minus here
		roty = -mat(0,1); // <- but here, and not (1,0)
		Z  = atan2( roty, rotx ) * (f32)GRAD_PI; 
	} 

	// fix values that get below zero
	// before it would set (!) values to 360
	// that where above 360:
	if (X < 0.00f) X += 360.00;
	if (Y < 0.00f) Y += 360.00; 
	if (Z < 0.00f) Z += 360.00; 

	return vector3df((f32)X,(f32)Y,(f32)Z);
}
However, I think now I will not use this funktion anymore... because a) it's better to use matrices or quaternions anyways and b) I get dizzy when I see it... ;) on the other hand it can be quite usefull sometimes...

What also helped is the "The Matrix and Quaternions FAQ". I recommend this one:

http://www.j3d.org/matrix_faq/matrfaq_latest.html

because it's the most up to date that I could find (Version 1.21 30th November 2003). It also has a version history.

Have fun rotating!
Jonas

ps: I still have other issues with matrix4.h, but more about that in a later post...

pps: Niko, your engine rocks! I really appreciate your hard work and it's just a pleasure for me to contribute things that make it better and less bogus! Thank you!
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Wow, cool. I bet it was a lot of work. :) Thanks for posting this!
Guest

Post by Guest »

You're welcome! I just found that there is still a tiny little flaw:

the 3 f's casting the 0.00's to f32 are not necessary, they even waste a tiny little bit of cpu. :wink:

better:

Code: Select all

   
   if (X < 0.00) X += 360.00; 
   if (Y < 0.00) Y += 360.00; 
   if (Z < 0.00) Z += 360.00; 
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Really funny. Finally I think I understand saigumi's posting (http://irrlicht.sourceforge.net/phpBB2/ ... php?t=2995). ;)
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Somehow the spammers like this thread. :)
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

They love it! :)
Post Reply