Game Development Community

Quaternion To Angle Axis bug - LOGGED

by Nathan Bowhay - ESAL · in Torque 3D Professional · 03/03/2011 (6:31 pm) · 3 replies

I am not positive if this is a bug, although I am pretty sure it is. We get a position and rotation sent from another piece of software. The rotation in Quaternions this is converted to angle axis of course so we can position and rotate an object according to the data. We do this in script and currently we are using this code:
%qx = getWord(%qaut, 0);
   %qy = getWord(%qaut, 1);
   %qz = getWord(%qaut, 2);
   %mag = getWord(%qaut, 3);
   //Calculate the angle axis using the quaternion rotation
   %scale = mSqrt(mPow(%qx,2)+mPow(%qy,2)+mPow(%qz,2));
   
   if(%scale == 0)
   {
      %theta = 0;
      
      %xA = 0;
      %yA = 0;
      %zA = 1;
   }
   else
   {
      %theta = 2 * mAcos(%mag);
      
      %xA = %qx/%scale;
      %yA = %qy/%scale;
      %zA = %qz/%scale;
   }
   %angleAxis = %xA SPC %yA SPC %zA SPC %theta;

This code has been working and the object looks fine. I recently had to do some other angle conversions and realized there was a QuatToAngleAxis function in engine, I decided to use it, but the object seems to be flipping and flopping around all over the place when it is pretty much not rotating (very small rotation values).

I decided to look at how the conversion was being done and here is the code (once you follow it):
AngAxisF & AngAxisF::set( const QuatF & q )
{
   angle = 2.0f * mAcos( q.w );
   F32 sinHalfAngle = mSqrt(q.x * q.x + q.y * q.y + q.z * q.z);
   if (sinHalfAngle != 0.0f)
      axis.set( q.x / sinHalfAngle, q.y / sinHalfAngle, q.z / sinHalfAngle );
   else
      axis.set(1.0f,0.0f,0.0f);
   return *this;
}

The only differences I notice is that x is getting set to 1 instead of z when the sinHalfAngle is 0. The other thing is that angle could potentially be some value other than 0 when sinHalfAngle is zero.

Now I am not a math expert, pretty rusty on this stuff lately, but I do know what happens visually and it isn't correct.

This is a simple enough fix, if it is really a bug. If not let me know why it isn't. I do know there aren't complete standards on terms used in rotation as well as axis orientation.

#1
03/04/2011 (12:20 am)
Why don't you set the position and rotation from quaternion direct? We use the quaternions to create the rotation matrix and set the position afterwards. Our problem is often, that coordinate system is rotated to our system.
#2
03/07/2011 (11:12 am)
Just because I am doing it all in script and using setTransform. I suppose I could create another console function for this, but not much point when I can just convert. I still want to know why though this conversion in Torque is off.
#3
03/15/2011 (3:56 pm)
Greetings!

Logged as THREED-1467. Thanks!

- Dave