# Getting euler from quaternion

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

## Recommended Posts

I'm trying to get the euler angles from a quaternion but I'm not getting the correct results. I'm using D3DXQuaternionRotationMatrix() to create a quaternion from the world matrix of my object then using the following code to try and extract the angles but the angles are off and math really isn't my strong point.
float roll = atan2(2.0f*(quat.x*quat.y + quat.w*quat.z), quat.w*quat.w + quat.x*quat.x - quat.y*quat.y - quat.z*quat.z);
float yaw = asin(-2.0f*(quat.x*quat.z - quat.w*quat.y));
float pitch = atan2(2.0f*(quat.y*quat.z + quat.w*quat.x), quat.w*quat.w - quat.x*quat.x - quat.y*quat.y + quat.z*quat.z);

The yaw part seems fine but the other two are not. I know I should use quaternions for rotations but I have my reasons for using the euler angles and am just wondering what is wrong with the math.

##### Share on other sites
That code doesn't work. Use the version to be found here.

##### Share on other sites
Quote:
 I know I should use quaternions for rotations...
Can you elaborate on that a bit? Is there any particular reason you think you should prefer quaternions over some other representation? (Just curious...)

##### Share on other sites
I'm using Bullet for physics/collision detection, but at some points its possible for the rigid body to be removed yet still render the graphical body. So before I delete the rigid body I want to get its location/orientation so I can still render it in its final position. Bullet stores rotations in quaternions and I wanted to get the euler angles out so that I could use that way to do rotations. I was using the euler angles because they are easier to deal with and for me to visualize. But for the life of me I can't get euler angles out of a quaternion, they are just never the proper angles. I must have tried half a dozen ways and all were off. But I was reading up on quaternions today and I realized that can do this: original = new rotation * original and the original is rotated by the new rotation.

DirectX already handles all the nasty implementation bits of quaternions so I will just go with them.

##### Share on other sites
Quaternions are kinda nice since they innately avoid gimbal lock.

##### Share on other sites
Quote:
 Quaternions are kinda nice since they innately avoid gimbal lock.
Although a common misconception, this is completely false. Quaternions have no 'innate' properties with respect to gimbal lock; in fact, quaternions and rotation matrices behave in exactly the same way as far as gimbal lock is concerned.

##### Share on other sites
Err, are you sure?

##### Share on other sites
Quote:
 Original post by jykAlthough a common misconception, this is completely false. Quaternions have no 'innate' properties with respect to gimbal lock; in fact, quaternions and rotation matrices behave in exactly the same way as far as gimbal lock is concerned.

Interpolating between orientations expressed by Euler's angle may rise to Gimble locks. This is a difficult problem and no easy solution exists. An Euler's angle uses only 3 numbers, while Qaternion uses 4 numbers to express an orientation. The properties of a Qaternion helps solving the problem of Gimble lock. A Qaternion has 1 real and 3 imaginary part.
Q = [w (x, y, z)]

[Edited by - metalman666 on September 19, 2008 3:23:48 AM]

##### Share on other sites
From the math perspective, one of the basic results about quaternions is that for quaternions q1 and q2 and vector v, then q1 v q1^-1 = q2 v q2^-1 if and only if q1 is a (nonzero) scalar multiple of q2.

But rotations are a special subset of quaternions that preserve length, so if you scale the quaternion up or down by a constant (say, 2), then it wont be a rotation anymore. So q1 v q1^-1 = q2 v q2^-1 implies q1=q2.

In other words, there can be no such point like the "north pole" where 2 different rotation quaternions would have the same effect as each other.

##### Share on other sites
Quote:
 Err, are you sure?
Yes, I'm sure.

Unfortunately I have to run, so I can't elaborate at the moment. I'll try to post in more detail later today though (meanwhile, a forum search for 'quaternion gimbal lock' will probably turn up some previous discussions on the topic).

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×