quaternion to axis-angle and back

Started by
6 comments, last by RenderCache 19 years ago
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
Advertisement
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.
Your code seems correct from over here. Except, I dont understand why the angle of the vector is equal to its Magnitude or length.
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
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.
You need to check for if the length of the vector is 0.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
You 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!

This topic is closed to new replies.

Advertisement