btTransform getRotation Bug?

q-turn
Posts: 3
Joined: Mon Feb 25, 2008 12:43 pm

btTransform getRotation Bug?

Post by q-turn »

I'm doing a 360 degree rotation about the z-axis which works fine, but reading back the result from the btTransform does not make sense? I wrote a small example to demonstrate the problem. Is this a bug?

Code: Select all

#define RAD2DEG(r) r*360.0/SIMD_2_PI

void testBulletRotation()
{
	btTransform trans;
	trans.setIdentity();

	for(int i = 0; i < 360; i+=10)
	{
		double angle = (double)i*(SIMD_2_PI/360.0);
		trans.setRotation(btQuaternion(btVector3(0.0,0.0,1.0),angle));
		double result = trans.getRotation().getAngle();
		printf("Rotation about z-axis deg: %.4f returns: %.4f\n",RAD2DEG(angle),RAD2DEG(result));
	}
}
Output:
Rotation about z-axis deg: 230.0000 returns: 230.0000
Rotation about z-axis deg: 240.0000 returns: 240.0000
Rotation about z-axis deg: 250.0000 returns: 110.0000
Rotation about z-axis deg: 260.0000 returns: 100.0000
Last edited by q-turn on Thu Feb 28, 2008 11:45 am, edited 1 time in total.
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: btTransform getRotation Bug?

Post by Dirk Gregorius »

I think the conversion flips the axis direction what is correct. Make sure that the axis returned by the transform is not the *negative* z-axis.
q-turn
Posts: 3
Joined: Mon Feb 25, 2008 12:43 pm

Re: btTransform getRotation Bug?

Post by q-turn »

If this is an axis flip it must be done in btTransform because btQuaternion works as expected. Also I can't seem to figure out how to read back the flipped axis from the btTransform?
Any help on how to get the current example to read back correct angle from the transform would be greatly appretiated.

Code: Select all

#define RAD2DEG(r) r*360.0/SIMD_2_PI

void testBulletRotation()
{
   btTransform trans;
   trans.setIdentity();
   btVector3 zaxis(0.0,0.0,1.0);

   for(int i = 0; i < 360; i+=10)
   {
      double angle = (double)i*(SIMD_2_PI/360.0);
      btQuaternion quat(zaxis,angle);
      double quatAngle = quat.getAngle();
      trans.setRotation(quat);
      quatAngle = trans.getRotation();
      double transAngle = quatAngle.getAngle();

      printf("Rotation about z-axis deg: %.1f quatAngle: %.1 transAngle : %.1f\n",RAD2DEG(angle),RAD2DEG(quatAngle),RAD2DEG(transAngle ));
   }
}
Output:
Rotation about z-axis deg: 250.0 quatAngle: 250.0 transAngle: 110.0


Thanks,
Filip
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: btTransform getRotation Bug?

Post by Dirk Gregorius »

Can't you debug this by stepping through the code? It doesn't look to hard to figure this out...
Hamstray
Posts: 15
Joined: Thu Jan 11, 2007 7:45 pm

Re: btTransform getRotation Bug?

Post by Hamstray »

btTransform stores rotation information as a 3x3matrix internally (which also covers scaling). Hence you lose part of the rotation information when converting to it. It can't tell the difference between 0 and 360 degrees of rotation.
q-turn
Posts: 3
Joined: Mon Feb 25, 2008 12:43 pm

Re: btTransform getRotation Bug?

Post by q-turn »

Hamstray wrote:btTransform stores rotation information as a 3x3matrix internally (which also covers scaling). Hence you lose part of the rotation information when converting to it. It can't tell the difference between 0 and 360 degrees of rotation.
No, but shurely it should be able to tell the difference between 90 and 270 degree rotation.
Also when you normalize the quaternion result, the axis vector should have been [0,0,1] or [0,0,-1] for a pure rotation about the z-axis. That way you can multiply the quaternion's angle with the axis value and get the rotation about that axis.

Code: Select all

btQuaternion quaternion = transForm.getRotation();
quaternion.normalize(); //For a quaternion rotation about z-axis with transform containing an identity matrix normalize should be redundant. I'd expect to get a z-value of -1.0 or 1.0
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: btTransform getRotation Bug?

Post by Dirk Gregorius »

A quaternion can only save a rotation between 0 and 180 degrees. So if you have a rotation around some axis with a an angle greater 180 the axis will be flipped and the angle adjusted accordingly. So after applying the rotation the orientation of the transformed object will correct. Anyway, it maybe wasn't rotated along the desired path, but this is another problem.

The book "Essential Math For Game Programmers" has a very good section about the representation of orientations. If you want to get into more details here is a nice paper: http://www.cs.cmu.edu/~spiff/moedit99/expmap.pdf. You also might want to look at Stan Melax short article about the difference between rotation and orientation here: http://www.melax.com/rotation.