# 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.

## 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 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 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,
Chris

##### Share on other sites
Many thanks, Il look into this and see what I can come up with.

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

http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm

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 :

X Z
| /
|/__Y

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

// Setup CamD3DXMATRIX Cam;D3DXMatrixIdentity(&Cam);D3DXMatrixLookAtLH((&Cam,&D3DXVECTOR3(0.0f,0.0f,0.0f),&D3DXVECTOR3(0.0f,0.0f,1.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f));// Move CamD3DXMATRIX Move;D3DXMatrixIdentity(&Move);D3DXMatrixTranslation(&Move,-512.0f,-64.0f,-512.0f));D3DXMatrixMultiply(&Cam,&Cam,&Move);// Change RotationD3DXMATRIX YawPitchRoll;D3DXMatrixIdentity(&YawPitchRoll);D3DXMatrixRotationYawPitchRoll(&YawPitchRoll,D3DXToRadian(Yaw),D3DXToRadian(Pitch),D3DXToRadian(Roll)));D3DXMatrixMultiply(&Cam,&Cam,&YawPitchRoll);// Update View StategpDevice->SetTransform(D3DTS_VIEW,&Cam)

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;        D3DXMatrixTranspose(&Transpose,pMatrix);	if (Transpose._32 > 0.998f)	{// Not Handled Yet		return;	}	if (Transpose._32 < -0.998f)	{// Not Handled Yet		return;	}	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.

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

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

http://www.gamedev.net/community/forums/topic.asp?topic_id=296900

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){	ASSERT(pVec);	D3DXVECTOR3	Scale;	D3DXQUATERNION	Rotation;	D3DXVECTOR3	Translation; // *************************** DECOMPOSE MATRIX	D3DXMatrixDecompose(&Scale,&Rotation,&Translation,this);// *************************** 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 ) );	}	else	{		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]

• ### What is your GameDev Story?

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

• 16
• 15
• 28
• 12
• 14
• ### Forum Statistics

• Total Topics
634805
• Total Posts
3019368
×