# OpenGL from matrix to radians

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a 4x4 matrix with the orientation and location of an object. It's a ms directx style matrix (I think it's the opposits of opengl if I remember correctly) Anyway, how do I convert my orrientation into radians (or degrees) along any of the 3 axis? For my specific instance I want to determine how far I'm rotated around the Y axis, so I can convert that to a compass heading. Thanks

##### Share on other sites
Well you can get the local X, Y and Z axes and the world position of the local origin from the matrix using:
D3DXMATRIX m = GenerateYourMatrix();D3DXVECTOR3 X(m._11,m._12,m._13);D3DXVECTOR3 Y(m._21,m._22,m._23);D3DXVECTOR3 Z(m._31,m._32,m._33);D3DXVECTOR3 Pos(m._41,m._42,m._43);

This relies on there being no scaling.

But generating this back to Euler angles is a bit tricky. I recommend you look here although I don't know which way rounf their matrices are and I don't have time to check. But if you look at the previous point that may help you...

##### Share on other sites
Yes, I actually had that doc, and created the GetEular function. However, I see some wierd behavior. I suspect I'm missing another step in using eular angles?

It seems to flip flop every 90 degrees, so if I try and rotate around the y axis, it seems to flip as I cross the X or Z axis. I kind of understand what it's doing, but I don't know the math to compinsate so I get an ever incrementing value from 0-360 (or 0-3.14 (that's rads right?)) without flip flop? I'm trying to display my heading or compass reading.

Do I somehow need to combine the x and z eular angles to get a consistent reading as I rotate around the Y, or should the y eular angle be sufficient?

Thanks

##### Share on other sites
Here's the function I use, translated to C++. I hope I've got it right (I never really used references in C++).

void eulerAnglesFromMatrix(D3DXMATRIX& mat, float& x, float& y, float& z){	if(mat.m[2][1]>1)		mat.m[2][1]=1;	if(mat.m[2][1]<-1)		mat.m[2][1]=-1;	x=asin(mat.m[2][1]);	const double epsilon=0.01;	if(x+epsilon<(3.1415926535/2))	{		if(x-epsilon>(-3.1415926535/2))		{			z=atan2(-mat.m[0][1],mat.m[1][1]);			y=atan2(-mat.m[2][0],mat.m[2][2]);		}		else		{			z=-atan2(mat.m[0][2],mat.m[0][0]);			y=0;		}	}	else	{		z=atan2(mat.m[0][2],mat.m[0][0]);		y=0;	}	x=-x;	y=-y;	z=-z;}

It works well. I had the flip-flop problem with some other methods as well. The epsilon in this method is to counter a strange problem when the object is pointing in one of the cardinal directions, and it is kind of indeterminate as to which solution to use, so the epsilon skews the result to one side or the other. Lastly, the checks on mat.m[2][1] at the beginning are because of floating point inaccuracies - sometimes that element can be something like 1.0000001, when it really should be 1.0, and atan2() will then return a NAN.

[Edited by - Drakex on July 21, 2005 11:13:14 AM]

##### Share on other sites
DrakeX,

That worked perfectly. I wish I understood the math, and why the document in http://skal.planet-d.net/demo/matrixfaq.htm#Q37 doesn't work, considering it seems to be a very popular document, but thanks.

Also, you're returning more of a radian value than a eular angle, right? It seems to be +/- pi/2, right? I actually wanted radians, so it works for me.

##### Share on other sites
I know, that document seems to be very popular, and it's never worked correctly for me. I think it's because of rotation order - different rotation orders require different ways of getting the angles out of the matrix. We both seem to be using YXZ :)

The function I posted does return radians, yes. If you're wondering about the name of the function, it does return degrees in my project, but I just removed that conversion for you.

##### Share on other sites

Here's the function I use, translated to C++. I hope I've got it right (I never really used references in C++).

void eulerAnglesFromMatrix(D3DXMATRIX& mat, float& x, float& y, float& z)
{
if(mat.m[2][1]>1)
mat.m[2][1]=1;
if(mat.m[2][1]<-1)
mat.m[2][1]=-1;
x=asin(mat.m[2][1]);
const double epsilon=0.01;
if(x+epsilon<(3.1415926535/2))
{
if(x-epsilon>(-3.1415926535/2))
{
z=atan2(-mat.m[0][1],mat.m[1][1]);
y=atan2(-mat.m[2][0],mat.m[2][2]);
}
else
{
z=-atan2(mat.m[0][2],mat.m[0][0]);
y=0;
}
}
else
{
z=atan2(mat.m[0][2],mat.m[0][0]);
y=0;
}

x=-x;
y=-y;
z=-z;
}

It works well. I had the flip-flop problem with some other methods as well. The epsilon in this method is to counter a strange problem when the object is pointing in one of the cardinal directions, and it is kind of indeterminate as to which solution to use, so the epsilon skews the result to one side or the other. Lastly, the checks on mat.m[2][1] at the beginning are because of floating point inaccuracies - sometimes that element can be something like 1.0000001, when it really should be 1.0, and atan2() will then return a NAN.

[Edited by - Drakex on July 21, 2005 11:13:14 AM]

cheers , this solved a problem that i had

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 14
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015484
×