Sign in to follow this  
DXnut

Quaternion rotations

Recommended Posts

Hi, I am trying to use quaternions so I can keep track of the rotation on the XYZ axes. I am doing the same thing I have done with matrices (when I don't need to keep track of the rotation on each axis independently).
//I start by making it the identity:

D3DXQuaternionIdentity(&m_KeyFrame.RotateY.Value);

//Then, as the mouse moves to rotate it on the Y-axis MoveXYZ is a calculation of the mouse movement (it's like 0.06 after a small movement)

D3DXQuaternionRotationAxis(&DQ, &D3DXVECTOR3(0.0f, 1.0f, 0.0f), MoveXYZ.y);
D3DXQuaternionNormalize(&DQ, &DQ);
D3DXQuaternionMultiply(&m_KeyFrame.RotateY.Value, &m_KeyFrame.RotateY.Value, &DQ);
D3DXQuaternionNormalize(&m_KeyFrame.RotateY.Value, &m_KeyFrame.RotateY.Value);

//Then I update the frame's matrix:

D3DXMATRIXA16 R;
D3DXMatrixRotationQuaternion(&R, &DQ);
m_pFrame->TransformationMatrix = R * m_pFrame->TransformationMatrix;
(also tried "*= R" here)

This causes major problems trying to render the basic cylinder mesh I skinned here. It basically destroys the TransformationMatrix, and I end up seeing deranged results in the debugger window for it. I found that if I do this it works (where I saved the TransformationMatrix in a new matrix that doesn't get changed): D3DXMATRIXA16 R; D3DXMatrixRotationQuaternion(&R, &m_KeyFrame.RotateY.Value); m_pFrame->TransformationMatrix = R * m_pFrame->OriginalTrans; However, I can't add on new keyframes after this, since they won't be added on to the changes made previously to the TransformationMatrix. What am I doing wrong??? Thanks.

Share this post


Link to post
Share on other sites
Stupid me. [embarrass] I was calling the routine to update the matrix from one place after processing all 3 possible axis rotations. I moved the call into each one, and it works:

D3DXQUATERNION DQ;
if (MoveXYZ.x != 0.0f)
{
D3DXQuaternionRotationAxis(&DQ, &D3DXVECTOR3(1.0f, 0.0f, 0.0f), MoveXYZ.x);
D3DXQuaternionNormalize(&DQ, &DQ);
D3DXQuaternionMultiply(&m_KeyFrame.RotateX.Value, &m_KeyFrame.RotateX.Value, &DQ);
D3DXQuaternionNormalize(&m_KeyFrame.RotateX.Value, &m_KeyFrame.RotateX.Value);
UpdateFrameRotation(DQ);
}
if (MoveXYZ.y != 0.0f)
{
D3DXQuaternionRotationAxis(&DQ, &D3DXVECTOR3(0.0f, 1.0f, 0.0f), MoveXYZ.y);
D3DXQuaternionNormalize(&DQ, &DQ);
D3DXQuaternionMultiply(&m_KeyFrame.RotateY.Value, &m_KeyFrame.RotateY.Value, &DQ);
D3DXQuaternionNormalize(&m_KeyFrame.RotateY.Value, &m_KeyFrame.RotateY.Value);
UpdateFrameRotation(DQ);
}
if (MoveXYZ.z != 0.0f)
{
D3DXQuaternionRotationAxis(&DQ, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), MoveXYZ.z);
D3DXQuaternionNormalize(&DQ, &DQ);
D3DXQuaternionMultiply(&m_KeyFrame.RotateZ.Value, &m_KeyFrame.RotateZ.Value, &DQ);
D3DXQuaternionNormalize(&m_KeyFrame.RotateZ.Value, &m_KeyFrame.RotateZ.Value);
UpdateFrameRotation(DQ);
}

void AnimationCreateDialogsCTL::UpdateFrameRotation(D3DXQUATERNION& DQ)
{
D3DXMATRIXA16 R;
D3DXMatrixRotationQuaternion(&R, &DQ);
m_pFrame->TransformationMatrix = R * m_pFrame->TransformationMatrix;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this