# Get euler angles from directX view matrix

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

## Recommended Posts

Hello math forum. This is quite a math-noob question. I want to get the yaw, pitch and roll angles from directX view matrix. I have a piece of code that works fine for yaw and pitch but I don't understand it. Can someone explain me how you get angles from X-,Y-,Z-basis and/or tell me how to get roll angle? here is the code
	// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &matInvView._31;
D3DXVECTOR3* pYBasis = (D3DXVECTOR3*) &matInvView._21;
yaw = atan2f(pZBasis->x, pZBasis->z);
float fLen = sqrtf(pZBasis->z*pZBasis->z + pZBasis->x*pZBasis->x);
pitch = atan2f(pZBasis->y, fLen);



##### Share on other sites
Quote:
 Original post by AdrianoHello math forum. This is quite a math-noob question.I want to get the yaw, pitch and roll angles from directX view matrix.I have a piece of code that works fine for yaw and pitch but I don't understand it.Can someone explain me how you get angles from X-,Y-,Z-basis and/or tell me how to get roll angle?here is the code*** Source Snippet Removed ***thx for your help
What you have there is a Cartesian-to-spherical coordinate conversion (that, incidentally, will fail when the camera is looking straight up or down). Also, are you sure what you have works? If it really is the inverse view matrix, it doesn't seem like you should be able to get the basis vectors that way...

Anyway, the problem with extracting Euler angles is that there are many possible orders in which to do it (e.g. XYZ, YZX, ZXY, etc.). Presumably you want the same order that the DX 'yaw, pitch, roll' function uses for construction. Google will give you plenty of implementations of matrix-to-Euler-angle conversion, but they may or may not be of the form that you need to match DX.

One thing I'd ask is, are you sure you need Euler angles? Is there any particular reason that you need the orientation in that particular form?

##### Share on other sites
do you generally use D3DXMatrixRotationYawPitchRoll to construct the matrices, or some other implementation?

##### Share on other sites
I googled "matrix-to-Euler-angle conversion" and found a lot of results wich can be more or less interesting. I tryed searching other similar sentences before posting but I didn't find what I was searching. This happens if you aren't able to express yourself well in english :P
In almost each of them, however, are mentioned the limitations of this method.
I know you were about to suggest me to use quaternions to achieve the result and I might consider that if I fail with this euler-angles approach

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631705
• Total Posts
3001826
×