Jump to content
  • Advertisement
Sign in to follow this  

Quanternions Angles

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im currently trying to write a flight sim, and have setup my terrain so that im flying across the x/z axis with the y axis being the height. I dont store the current location, angles etc of the plane, but use the view matrix to handle a flying camera. However, Im having some real trouble trying to work out the current pitch/yaw/roll to shjow on my flight panel. I've managed to use the D3DXMatrixDecompose() function to obtain not only the position but also the angles, But the angles look funny, I would expect them to go from -1.0f to 1.0f as the view spins over one axis but the D3DXQUATERNION returned from the decompose holds some values that seems to be all over the place. Can someone tell me if Ive missed something, is there something else I need to do with this quanternion before I can get the x/y/z (roll pitch yaw) values correctly. Regards

Share this post

Link to post
Share on other sites
The components of a quaternion do not correspond to yaw, pitch, and roll. Search for algorithms that convert from a 3x3 matrix or a quaternion to Euler angles.

Share this post

Link to post
Share on other sites
Here is a link to MSDN which has a description of what the Quaternion is composed of. Basically it's a certain amount of rotation about a given axis, so that's what the 4 components represent.

I didn't see any functions in the D3DX library to convert back to yaw, pitch, roll, but there is a function D3DXMatrixRotationQuaternion which creates a matrix from the quaternion. I'm not entirely sure how easy it is to go backwards, but I'm sure you could find something on Google about it.

Good luck,

Share this post

Link to post
Share on other sites
Im still having no joy with this. I have searched the web and found many articles, especially this one.


However, It only uses RHV and Im using LHV with DirectX. I believe the matrix rows and columns are inversed as well. The orientation is :

| /

So im hoping that the calculation in the linked articles works, but it still doesnt. Basically im using this code:

// Setup Cam

// Move Cam

// Change Rotation
D3DXMATRIX YawPitchRoll;

// Update View State

I can obtain the position no problems, but the angles well, there seems to be alot of documentation, mostly RHV OpenGL, but very little on DX. Im using this atm.

VOID GetAngles(DXMATRIX* pMatrix,D3DXVECTOR3* pVec)
D3DXMATRIX Transpose;

if (Transpose._32 > 0.998f)
// Not Handled Yet
if (Transpose._32 < -0.998f)
// Not Handled Yet
pVec->y = D3DXToDegree(atan2(-Transpose._42,Transpose._22));
pVec->z = D3DXToDegree(atan2(-Transpose._34,Transpose._33));
pVec->x = D3DXToDegree(asin ( Transpose._32));

This doesnt work either. Im sort of running out of ideas how to do something that seems to be pretty easy on paper, but is turning out to be very hard. Can anyone enlighten me on this please. Btw, I have tried obtaining a Quaternion and although I can get this fine, I still have the same problem with knowing the correct cos/sin matches.

Thanks very much in advance

[Edited by - MatthewEva on December 7, 2005 9:38:58 AM]

Share this post

Link to post
Share on other sites
I seem to have found a solution here:


Although it doesnt mention matrices, it does work on the Quanternion and seems to work in my code.

I have made the function now: (where DXMatrix is my overloaded class inherited from D3DXMATRIX)

VOID DXMatrix::GetRotation(D3DXVECTOR3* pVec)

D3DXVECTOR3 Translation;

// *************************** DECOMPOSE MATRIX


// *************************** CALCULATE COMPONENTS

float tx = 2.0f * Rotation.x;
float ty = 2.0f * Rotation.y;
float tz = 2.0f * Rotation.z;
float txw = tx * Rotation.w;
float tyw = ty * Rotation.w;
float tzw = tz * Rotation.w;
float txx = tx * Rotation.x;
float tyx = ty * Rotation.x;
float tzx = tz * Rotation.x;
float tyy = ty * Rotation.y;
float tzy = tz * Rotation.y;
float tzz = tz * Rotation.z;

float s = tzy - txw;
if( s > 1.0f )
s = 1.0f;
if( s < -1.0f )
s = -1.0f;

// *************************** CALCULATE PITCH

pVec->x = asin(-s );

// *************************** CALCULATE YAW/ROLL

if ((Rotation.x > -D3DXToRadian(89)) || (Rotation.x < D3DXToRadian(89)))
pVec->y = atan2( tzx + tyw, 1.0f - ( txx + tyy ) );
pVec->z = atan2( tyx + tzw, 1.0f - ( txx + tzz ) );
pVec->y = 0.0f;
pVec->z = atan2( tzx - tyw, 1.0f -( tyy + tzz ) );

// *************************** CHANGE TO DEGREE

pVec->x = D3DXToDegree(pVec->x);
pVec->y = D3DXToDegree(pVec->y);
pVec->z = D3DXToDegree(pVec->z);

Thanks for the previous help, you got me started in the right direction.

[Edited by - MatthewEva on December 7, 2005 9:07:14 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!