Advertisement

Something is wrong with my getEulerAngles-routine...

Started by September 03, 2002 11:44 PM
1 comment, last by Floating 22 years, 5 months ago
I defined my 3D-transformations like this: T=Translation(x,y,z)*XRotation(alpha)*YRotation(beta)*ZRotation(gamma). So my T matrix contains: Transformed x-vector: (T[0][0],T[1][0],T[2][0]) Transformed y-vector: (T[0][1],T[1][1],T[2][1]) Transformed z-vector: (T[0][2],T[1][2],T[2][2]) Transformed origin position: (T[0][3],T[1][3],T[2][3]) I implemented my own getEulerAngles routine where I want my angles to be: alfa and gamma angles are between -pi and +pi beta angle is between -pi/2 and +pi/2 My routine looks like this:

void tt::getEulerAngles(float m[4][4],float &a,float &b,float &g)
{
	b=(float)asin(m[0][2]);
	a=(float)atan2(-m[1][2],m[2][2]);
	g=(float)atan2(-m[0][1],m[0][0]);
}
 
But in some very rare cases I get strange results. Then I found the following routine on this site:

void tt::getEulerAngles(float m[4][4],float &a,float &b,float &g)
{
	b=(float)asin(m[0][2]);
	float tmp=m[0][2];
	if (tmp<0.0f)
		tmp=-tmp;
	if (tmp<0.999995f)
	{	// No gimbal lock
		a=(float)atan2(-m[1][2],m[2][2]);
		g=(float)atan2(-m[0][1],m[0][0]);
	}
	else
	{	// Gimbal lock has occured
		a=0.0f;
		g=(float)atan2(-m[1][0],m[1][1]);
	}
}
 
But this gives me also some strange results in very rare cases. So my question: What do I do wrong or how should that code look like?? Thanks
Please search the forum archives. You should find some information there on extracting Euler angles from matrices.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Advertisement
Thanks grhodes_at_work

I finally found the problem... in the second routine, I put a minus sign which should not be there... typing error?

It should be:
...	else	{	// Gimbal lock has occured		a=0.0f;		g=(float)atan2(m[1][0],m[1][1]);	} 

This topic is closed to new replies.

Advertisement