# quaternion to axis-angle and back

This topic is 4889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok! i was playing ard with the conversions and i wonder if converting from axis angle to quaternion and back will get back the original axis angle. below is the small source which i am working on, but i couldnt seem to see what was wrong. I jus dun get back the original values. would be grateful if someone help to look at it.
D3DXQUATERNION quat;
D3DXQuaternionIdentity(&quat);

D3DXVECTOR3 rot_axis(0.5f,1.2f,0.3f);//hardcoded
float angle = D3DXVec3Length(&rot_axis);
float halfangle = angle/2.0f;
D3DXVec3Normalize(&rot_axis,&rot_axis);

float sine_halfangle = sin(halfangle);
quat.x = sine_halfangle * rot_axis.x;
quat.y = sine_halfangle * rot_axis.y;
quat.z = sine_halfangle * rot_axis.z;
quat.w = cos(halfangle);
D3DXQuaternionNormalize(&quat,&quat);

//try to convert it back
float angle2;
D3DXVECTOR3 axis2;
D3DXQuaternionToAxisAngle(&quat,&axis2,&angle2);


thx! RC

##### Share on other sites
you could get back -angle and -axis. (opposite axis and opposite angle). That's exactly same turn. Also, if your angle is bigger than pi, you'll get back different angle, as angle (pi+something ) is the same as angle -pi-something.

##### Share on other sites
Your code seems correct from over here. Except, I dont understand why the angle of the vector is equal to its Magnitude or length.

##### Share on other sites
hi!
i thot the angle calculated is known as the 'angular velocity'
i took from this webpage:

Martin Baker's Angular Velocity webpage

is there something wrong when using it?
thx!
RC

##### Share on other sites
Angular velocity can be expressed in axis-angle notation and quaternion notation (and matrix notation and euler notation and that other weird magnitude-based notation) just like angle, but that doesn't mean that you take a quaternion representing an angle and turn it into an axis/angle representing angular velocity. It still represents an angle.

Angular velocity specifies velocity of rotation. If my bike wheel is revolving at one rotation per second, its angular velocity is 2π radians per second.

##### Share on other sites
You need to check for if the length of the vector is 0.

##### Share on other sites
Quote:
 Original post by JohnBoltonYou need to check for if the length of the vector is 0.

That was something I checked first, the || rot_axis || = 1.334 or something, so regardless of the [or lack of] physical relevance of the way the angle is set, the angle given seems fine.

Anyway I plugged your code into my engine real quick and looked at the output. It worked. What was wrong was one of those silly mistakes that catches us all. The angle returned by D3DXQuaternionToAxisAngle() was equal to that of rot_axis implying that they were parallel to each other, rot_axis || axis2. The only thing wrong was that D3DXQuaternionToAxisAngle() did not return a unit vector. Renormalizing axis2 will make it equal to rot_axis.

This should fix it:
D3DXQUATERNION quat;D3DXQuaternionIdentity(&quat);D3DXVECTOR3 rot_axis(0.5f,1.2f,0.3f);//hardcodedfloat angle = D3DXVec3Length(&rot_axis);float halfangle = angle/2.0f;D3DXVec3Normalize(&rot_axis,&rot_axis);float sine_halfangle = sin(halfangle);quat.x = sine_halfangle * rot_axis.x;quat.y = sine_halfangle * rot_axis.y;quat.z = sine_halfangle * rot_axis.z;quat.w = cos(halfangle);D3DXQuaternionNormalize(&quat,&quat);//try to convert it backfloat angle2;D3DXVECTOR3 axis2;D3DXQuaternionToAxisAngle(&quat,&axis2,&angle2);D3DXVec3Normalize(&axis2,&axis2); //Now they are equal in all ways

hi!
thx so much!

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 17
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632191
• Total Posts
3004662

×