   Quaternion orientation
  I'm trying to represent the orientation of an object using quaternions.  The problem I'm having is that when I want to rotate about the objects local Y-axis (for example) it seems to be rotating about the world Y-axis.  Heres a section of my code:qRotation = D3DXQUATERNION(0, sin(theta/2), 0, cos(theta/2));D3DXQuaternionInverse(&qRotationInv, &qRotation);m_qOrientation = qRotation*m_qOrientation*qRotationInv;I convert the m_qOrientation quaternion to a matrix using D3DXMatrixRotationQuaternion and add translation before sending it to SetTransform as the World transformation.I know a little about quaternions but I've never used them in practise.  I know the proper way to rotate a vector v about a quaternion q is q*V*q-1, where V=[0,v], but I'm not sure about combining quaternions for use in converting to a rotation matrix.Any help much appreciated.edit - Coder: Fixed code tags.[Edited by - Coder on July 27, 2004 11:28:22 AM]
  I have not worked with Quaternions, but here is a roundabout way to acheive what you are trying to do.Convert the quaternion representing the orientation of your object to an equivalent matrix (I belive D3DX has methods for that). The second row of that matrix is the y-vector of your object. Now compose a rotation matrix using D3DXRotationAxis method using this y-vector as your rotation axis. Now if you must stay in quaternions, you can conver tthis rotation amtrix back to quaternion using D3DX facilities.;  This is taken directly from one of my other responses regarding quaternion orientation:OK a quick recap on quaternions. In DirectX quaternions have the structuretypedef struct D3DXQUATERNION {  FLOAT x;  FLOAT y;  FLOAT z;  FLOAT w;} D3DXQUATERNION;The x,y,z components represent the angle of rotation and the w component is the scalar. The w component is the cosine of the angle of rotation divided by two. The x,y,z vector component should be normalised and be scaled to the sine of the angle divided by two.As a quick example, suppose you want to create a quaternion that represents a 20 degree rotation around the X axis! You would do it like this// Create a quaternionD3DXQUATERNION quatOrientation;// Create a vector that represents the rotation axisD3DXVECTOR3 Axis = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );// Normalise the vectorD3DXVec3Normalize( &Axis, &Axis );// Scale x,y,z components to the sine of the angle divided by two.Axis *= (float)sin(D3DXToRadian(20) / 2.0f);// Store this in the quaternionquatOrientation.x = Axis.x;quatOrientation.y = Axis.y;quatOrientation.z = Axis.z;// Calculate the scalar componentquatOrientation.w = (float)cos(D3DXToRadian(20) / 2.0f);// Make it a unit quaternionD3DXQuaternionNormalize( &quatOrientation, &quatOrientation );That is how you store your rotation in a quaternion. The problem is that DirectX does not allow you to do quaternion transformations so you need to change it to a rotation matrix with the method D3DXMatrixRotationQuaternion() when you are setting the world position for rendering. Also, because quaternions are right-handed and you will likely be using the left-handed system, you must transpose the returned matrix with D3DXMatrixTranspose().   ;  Daedalus, I actually read that post before I started this thread.  What I seem to be seeing is that basically you have to recreate the quaternion from scratch every time you want to transform it, you cant just keep one quaternion that represents an object's orientation and transform it when you want to yaw/pitch/roll.  I was pretty sure that wasnt the case, but all the posts, tutorials and articles I've read so far just give functions for creating a quaternion from yaw/pitch/roll or axis-angle and not giving any information on transforming an existing quaternion about an object's local axis.Anyone any more information?  Am I right in assuming you simply cant transform a quaternion about local axis? - I'm pretty sure I'm not.Help!   ;Quote:Original post by DangerDaveDaedalus, I actually read that post before I started this thread.  What I seem to be seeing is that basically you have to recreate the quaternion from scratch every time you want to transform it, you cant just keep one quaternion that represents an object's orientation and transform it when you want to yaw/pitch/roll.  I was pretty sure that wasnt the case, but all the posts, tutorials and articles I've read so far just give functions for creating a quaternion from yaw/pitch/roll or axis-angle and not giving any information on transforming an existing quaternion about an object's local axis.Anyone any more information?  Am I right in assuming you simply cant transform a quaternion about local axis? - I'm pretty sure I'm not.Help!Hmmm, I usually just keep track of the yaw, pitch and roll in floats. You can build a quaternion quickly from this with the D3DXQuaternionRotationYawPitchRoll() method and that way you just change the float values to take in to account relative transformations. Not sure how you do relative transformations directly with a quaternion...   ;  Hmmm... to rotate around my objects (camera in my case) local y axis, I do :D3DXQUATERNION qtTemp;D3DXQuaternionRotationYawPitchRoll(&qtTemp, fYRot, 0.0f, 0.0f);m_qtOrientation *= qtTemp;fYRot is the y rotation in radians.m_qtOrientation is the objects position.Not sure if this is the same for you.Steele.   ;  The mistake is multiplying by the inverse. If you want to accumulate rotation, you just multiply the current orientation by the new rotation:m_qOrientation = m_qOrientation * qRotation;qVq-1 rotates the vector V, q0q1Vq1-1q0-1 rotates it twice.   