Jump to content
  • Advertisement
Sign in to follow this  

Quaternion rotation help

This topic is 3585 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

I'm trying to implement a rotation of a quaternion off of this website.(and many others). Anyhow it isn't working for me. http://www.cprogramming.com/tutorial/3d/quaternions.html I'm trying to use the directx libraries to do most of the work for me. Here is my code, any help getting this to work would be much appreciated.
  D3DXVECTOR3 tempAxis, newAxis;
  D3DXMATRIX rotationMatrix;
  D3DXQUATERNION local_rotation, total;

  total.w = 1;
  total.x = 0;
  total.y = 0;
  total.z = 0;

  D3DXQuaternionToAxisAngle(&q, &tempAxis, &tempAngle);

  local_rotation.w  = cosf( tempAngle/2);
  local_rotation.x = tempAxis.x * sinf( tempAngle/2 );
  local_rotation.y = tempAxis.y * sinf( tempAngle/2 );
  local_rotation.z = tempAxis.z * sinf( tempAngle/2 );

  total = local_rotation * total;

  rotationMatrix._11 = 1 - 2 * total.y * total.y - 2 * total.z * total.z;
  rotationMatrix._12 = 2*total.x*total.y-2*total.w*total.z;
  rotationMatrix._13 = 2*total.x*total.z+2*total.w*total.y;
  rotationMatrix._14 = 0;

  rotationMatrix._21  = 2*total.x*total.y+2*total.w*total.z;	
  rotationMatrix._22  = 1 - 2 * total.x * total.x - 2 * total.z * total.z;
  rotationMatrix._23  = 2*total.y*total.z-2*total.w*total.x;
  rotationMatrix._24  = 0;

  rotationMatrix._31  = 2*total.x*total.z-2*total.w*total.y;
  rotationMatrix._32  = 2*total.y*total.z-2*total.w*total.x;
  rotationMatrix._33  = 1 -2 * total.x * total.x - 2 * total.y * total.y;
  rotationMatrix._34  = 0;

  rotationMatrix._41  = 0;
  rotationMatrix._42  = 0;
  rotationMatrix._43  = 0;
  rotationMatrix._44  = 1;

  D3DXQuaternionRotationMatrix(&rotationQuat, &rotationMatrix);*/
  D3DXQuaternionMultiply(&total, &rotationQuat, &total);

  //then we need to slerp in between those two quaternions
  D3DXQuaternionSlerp(&outQuat, &preQuat, &total, GAMEPLAY->timeStep_);
  D3DXQuaternionNormalize(&objBody->orient_, &outQuat);

What i'm not so clear on is how I rotate on an arbitrary axis. I tried adding to tempAngle but that doesn't seem to do anything since at the start the axis which is returned from D3DXQuaternionToAxisAngle(&q, &tempAxis, &tempAngle); is {0,0,0} and angle {0} I want to be able to rotate my object on the object's local coordinate system. Thanks, Screamer

Share this post

Link to post
Share on other sites
You need to implement / find / create functions to GetViewAxis(), GetRightAxis(), GetUpAxis(), and then use those to rotate the object around it's own coordinate system.

Most of the maths tutorials on Quaternions cover that.

Post back if you can't find one, and I'll try to dig one up.

Share this post

Link to post
Share on other sites
I suggest you read a bit more on how the quaternion is represented. For instance here. Ie what the elements x,y,z represent, and what the w element is.

Reading your code, it's obvious that tempAxis becomes {0,0,0}, and you should read up on quaternions until it's obvious to you too :)

Share this post

Link to post
Share on other sites
So now I'm trying this code and from what I've read it should work but the inverse seems to cancel out the first rotation.

float yaw = D3DX_PI/4.f;
D3DXQuaternionRotationYawPitchRoll(&rotationQuat, 0, yaw, 0);

D3DXQuaternionInverse(&inverse, &rotationQuat);
//q = q * rotationQuat;
D3DXQuaternionMultiply(&q, &q, &inverse);
D3DXQuaternionMultiply(&q, &rotationQuat, &q);

Thanks for the help,

Share this post

Link to post
Share on other sites
oh i was doing rotation * quaternion * rotationInverse.
I should of been doing quaternion * rotation * quaternionInverse.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!