Gimbal Lock with Quaternions! Eeek!

Started by
0 comments, last by SGreth 20 years, 9 months ago
I''m obviously doing something wrong to get gimbal lock while using quaternions! Any help would be *greatly* appreciated. void CModelReference::RotateX(float fDegrees) { //Cook up a temp quaternion. D3DXQUATERNION qTemp; D3DXVECTOR3 vVector; vVector.x = 1; vVector.y = 0; vVector.z = 0; D3DXQuaternionRotationAxis(&qTemp, &vVector, HMATH_DEGTORAD(fDegrees)); m_qOrientation *= qTemp; RotateUpdate(); } void CModelReference::RotateY(float fDegrees) { //Cook up a temp quaternion. D3DXQUATERNION qTemp; D3DXVECTOR3 vVector; vVector.x = 0; vVector.y = 1; vVector.z = 0; D3DXQuaternionRotationAxis(&qTemp, &vVector, HMATH_DEGTORAD(fDegrees)); m_qOrientation *= qTemp; RotateUpdate(); } void CModelReference::RotateZ(float fDegrees) { //Cook up a temp quaternion. D3DXQUATERNION qTemp; D3DXVECTOR3 vVector; vVector.x = 0; vVector.y = 0; vVector.z = 1; D3DXQuaternionRotationAxis(&qTemp, &vVector, HMATH_DEGTORAD(fDegrees)); m_qOrientation *= qTemp; RotateUpdate(); } void CModelReference::RotateUpdate() { D3DXQUATERNION qX, qY, qZ, qResult; D3DXVECTOR3 vAxisAngle; D3DXVECTOR3 vResult; D3DXMATRIX Matrix; //Convert the quaternion into a matrix. D3DXMatrixRotationQuaternion(&Matrix, &m_qOrientation); //Compute the forward vector. vAxisAngle.x = 0; vAxisAngle.y = 0; vAxisAngle.z = 1; D3DXVec3TransformCoord(&vResult, &vAxisAngle, &Matrix); D3DXVec3Normalize(&m_vForward, &vResult); //Compute the up vector. vAxisAngle.x = 0; vAxisAngle.y = 1; vAxisAngle.z = 0; D3DXVec3TransformCoord(&vResult, &vAxisAngle, &Matrix); D3DXVec3Normalize(&m_vUp, &vResult); //Compute the right vector. vAxisAngle.x = 1; vAxisAngle.y = 0; vAxisAngle.z = 0; D3DXVec3TransformCoord(&vResult, &vAxisAngle, &Matrix); D3DXVec3Normalize(&m_vRight, &vResult); }
"The difference between insanity and genius is measured only by success."~Bruce Feirstein
Advertisement
Got it *phew*

Was multiplying the quaternions in the wrong order

*Wrong*
m_qOrientation *= qTemp;

*Right*
m_qOrientation = qTemp * m_qOrientation;

I''ve seen a couple of demos that do it the first way, but the second works for me, so I''m going with it! Don''t fix working code

~S''Greth
"The difference between insanity and genius is measured only by success."~Bruce Feirstein

This topic is closed to new replies.

Advertisement