# OpenGL from matrix to radians

## 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
[quote name='Drakex' timestamp='1121850834' post='3165384']
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]
[/quote]

cheers , this solved a problem that i had

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627681
• Total Posts
2978611
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 13
• 12
• 10
• 12
• 22