getEuler has unreachable code

jakeBie
Posts: 3
Joined: Wed May 30, 2007 12:36 am

getEuler has unreachable code

Post by jakeBie »

When converting a 3x3 matrix to its Euler angles, I came across the following code in btMatrix3x3.h:

Code: Select all

void getEuler(btScalar& yaw, btScalar& pitch, btScalar& roll) const
		{
			pitch = btScalar(btAsin(-m_el[2][0]));
			if (pitch < SIMD_2_PI)
			{
				if (pitch > SIMD_2_PI)
				{
					yaw = btScalar(btAtan2(m_el[1][0], m_el[0][0]));
					roll = btScalar(btAtan2(m_el[2][1], m_el[2][2]));
				}
				else 
				{
					yaw = btScalar(-btAtan2(-m_el[0][1], m_el[0][2]));
					roll = btScalar(0.0);
				}
			}
			else
			{
				yaw = btScalar(btAtan2(-m_el[0][1], m_el[0][2]));
				roll = btScalar(0.0);
			}
		}
The first if doesn't make sense- asin should never return a value higher than 2pi. The second if statement makes even less sense. How is it even possible to reach that code (only reachable if pitch < 2 pi and greater than 2 pi.

I am in need of this function and it seems to be returning incorrect values. Any insights?

[/code]
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

This was an un-used method and something must have gone wrong when porting the original code. This is the updated version, is checked into svn so should go into next version.

Can you check it?
Thanks for the feedback,
Erwin

Code: Select all

	void getEuler(btScalar& yaw, btScalar& pitch, btScalar& roll) const
		{
			
			if (btScalar(m_el[1].z()) < btScalar(1))
			{
				if (btScalar(m_el[1].z()) > -btScalar(1))
				{
					yaw = btScalar(btAtan2(m_el[1].x(), m_el[0].x()));
					pitch = btScalar(btAsin(-m_el[1].y()));
					roll = btScalar(btAtan2(m_el[2].y(), m_el[2].z()));
				}
				else 
				{
					yaw = btScalar(-btAtan2(-m_el[0].y(), m_el[0].z()));
					pitch = SIMD_HALF_PI;
					roll = btScalar(0.0);
				}
			}
			else
			{
				yaw = btScalar(btAtan2(-m_el[0].y(), m_el[0].z()));
				pitch = -SIMD_HALF_PI;
				roll = btScalar(0.0);
			}
		}
		
jakeBie
Posts: 3
Joined: Wed May 30, 2007 12:36 am

Post by jakeBie »

Yeah it looks like it's working.