# Getting euler from quaternion

This topic is 3375 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).

##### Share on other sites
Quote:
Original post by jyk
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).

Then I'll elaborate.... Gimbal lock occurs because you have 3 rotations multiplied in order. Typically this is seen with basic Euler angles since you have

X * Y * Z

where X,Y,Z are axis angle rotations around the axes (1,0,0), (0,1,0) and (0,0,1). You can avoid the possibility of gimbal lock by reducing the rotation to a single axis angle rotation - which can also be represented by a Quat.

You can however get gimbal lock with quats by performing the same X*Y*Z rotation. i.e.

if X is a quat with values (1,0,0,0), and then you multiply by Y and Z afterwards, you have a gimbal lock....

In practice this doesn't tend to happen very often, since people using Quats just use a single quat to represent rotation.

##### Share on other sites
To address all the issues at hand would take a fairly long post. There are however quite a few threads in the archives that touch on this subject, so for the interested I suggest searching the archives for 'quaternion gimbal lock'. You may have to do some reading to get to the good stuff, but you'll find that this particular 'gimbal lock myth' has been addressed on more than a few occasions.

Meanwhile, the points I would like to stress most are these:

1. Quaternions and matrices have exactly the same behavior with respect to gimbal lock.

2. Quaternions have no 'special' properties as far as gimbal lock is concerned.

@Maze Master and metalman666: If after reading some of the previous threads on the topic and/or doing other research you're still not convinced that the preceding statements are true, please post back and I'll try to go into a little more detail.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628734
• Total Posts
2984444

• 25
• 11
• 10
• 16
• 14