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

## 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
D3DXQUATERNION outQuat;

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

Thanks scratt

##### Share on other sites
I still don't understand if I need to use rotation matrix or how the best way to do this is.

##### Share on other sites
Talk to Xin Li you n00b.

##### 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 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);	  D3DXQUATERNION inverse;  D3DXQuaternionInverse(&inverse, &rotationQuat);  //q = q * rotationQuat;  D3DXQuaternionMultiply(&q, &q, &inverse);  D3DXQuaternionMultiply(&q, &rotationQuat, &q);

Thanks for the help,
Screamer

##### Share on other sites
oh i was doing rotation * quaternion * rotationInverse.
I should of been doing quaternion * rotation * quaternionInverse.
Thanks

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633345
• Total Posts
3011439
• ### Who's Online (See full list)

There are no registered users currently online

×