Quaternion to Euler problems

ryan0618
Posts: 9
Joined: Sun Mar 29, 2009 10:27 pm

Quaternion to Euler problems

Post by ryan0618 »

Hello everyone. I am new to bullet, and I made a demo to try and get the feel of it. It is based of the Hello World tutorial. I have three objects, a sphere, a box, and another sphere, fallong from a height of 50, 52, and 54 meters. I am using Dark GDK to display the objects. The position works fine, but I think I have a problem with my Quaternion to Euler angle converter(writen from the formulas from here) :

Code: Select all

void QtoE (float qx, float qy, float qz, float qw)
{	
	double test = qx * qy + qz * qw;
	float sqx = qx * qx;
	float sqy = qy * qy;
	float sqz = qz * qz;
	float sqw = qw * qw;
	double unit = sqx + sqy + sqz + sqw;
	double RadtoDeg = 57.2957795;
	
	if (test > 0.499 * unit)
	{
		Ey = 2 * atan2(qx,qw);
		Ez = pi / 2; 
		Ex = 180;
		
		if (Ey < 0)
			Ey += 360;
		if (Ex < 0)
			Ex += 360;
		if (Ez < 0)
			Ez += 360;
		return;
	}

	if (test < -0.499 * unit)
	{
		Ey = -2 * atan2(qx,qw);
		Ez = -pi / 2;
		Ex = -180;
		
		if (Ey < 0)
			Ey += 360;
		if (Ex < 0)
			Ex += 360;
		if (Ez < 0)
			Ez += 360;
		return;
	}

	Ey = RadtoDeg * (atan2(2 * qy * qw - 2 * qx * qz, sqx - sqy - sqz + sqw));
	Ez = RadtoDeg * (asin(2*test/unit));
	Ex = RadtoDeg * (atan2(2*qx*qw - 2*qy*qz, -sqx + sqy - sqz + sqw));
	
	if (Ey < 0)
		Ey += 360;
	if (Ex < 0)
		Ex += 360;
	if (Ez < 0)
		Ez += 360;
	
	return;
}
The rotation starts smoothly at first, but then the box spins crazily before settling again.

1) Is there a Quaternion to Euler converter in bullet?
2) If not, is there something wrong with my code?
3) If not, does bullet use global or local rotation?

Thanks in Advance!
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: Quaternion to Euler problems

Post by bone »

The problem may be due to the fact that there's no agreed standard definition of the order of rotations for Euler angles. The page you got that code from assumes NASA's standards, while I couldn't quickly figure out what order that Dark SDK uses.

I can get you as far as a quaternion to matrix conversion. I don't think there's any confusion on this part of the conversion (except possibly for the use of transposed matrices):

Code: Select all

  const float x2 = q.x + q.x;
  const float xx = q.x * x2;

  const float y2 = q.y + q.y;
  const float yy = q.y * y2;

  const float z2 = q.z + q.z;
  const float zz = q.z * z2;

  const float xz = q.x * z2;
  const float xy = q.x * y2;
  const float wy = q.w * y2;
  const float wx = q.w * x2;
  const float wz = q.w * z2;
  const float yz = q.y * z2;

  m[0][0] = (float) 1.0 - ( yy + zz );
  m[0][1] = xy - wz;
  m[0][2] = xz + wy;
  m[1][0] = xy + wz;
  m[1][1] = (float) 1.0 - ( xx + zz );
  m[1][2] = yz - wx;
  m[2][0] = xz - wy;
  m[2][1] = yz + wx;
  m[2][2] = (float) 1.0 - ( xx + yy );
After that, you've got to figure out what Dark SDK is using for the order of rotations, and compute the Matrix-To-Euler part. Eberly's book has a bunch of these conversions, although I'm sure you can find them on the 'net somewhere, too.

Note that you will only end up needing a few of the matrix entries for the second conversion, so a little optimization is easy.
ryan0618
Posts: 9
Joined: Sun Mar 29, 2009 10:27 pm

Re: Quaternion to Euler problems

Post by ryan0618 »

Thank you for your help. I'll post something on the Dark GDK fourms and work it out. It might be easier to buy an attachment to Dark GDK that supports quaternion angles, but I don't want to spend money, which was why I went with bullet in the first place. :D
ryan0618
Posts: 9
Joined: Sun Mar 29, 2009 10:27 pm

Re: Quaternion to Euler problems

Post by ryan0618 »

I Got It! It turns out htere was an undocumented function that takes a transformation matrix and displays the position. I also used a D3DX Matrix, so I didn't have to worry about the euler to matrix code(sorry :( ) Thank you for your help, again.
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: Quaternion to Euler problems

Post by bone »

You're welcome. Glad you got it working!
dehseth
Posts: 5
Joined: Mon Dec 28, 2009 9:48 am

Re: Quaternion to Euler problems

Post by dehseth »

I kinda have the same problem with yours... When I convert bullet quaternion to euler angles my sphere object rotates so not in the direction... any idea?