# Euler to Quaternion - error near 90, 180, 270... [SOLVED]

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

## Recommended Posts

Hi, I have problem with convert Euler to Quaternion. For conversion I use this function: Quaternion Euler2Quaternion(float EX, float EY, float EZ) { double x,y,z; x = EX*(PI / 180.0); y = EY*(PI / 180.0); z = EZ*(PI / 180.0); double c1 = cos(x/2); double s1 = sin(x/2); double c2 = cos(y/2); double s2 = sin(y/2); double c3 = cos(z/2); double s3 = sin(z/2); double c1c2 = c1*c2; double s1s2 = s1*s2; double qw = c1c2*c3 - s1s2*s3; double qx = c1c2*s3 + s1s2*c3; double qy = s1*c2*c3 + c1*s2*s3; double qz = c1*s2*c3 - s1*c2*s3; Quaternion quat(qx,qy,qz,qw); return quat; } When I do: -- start frame--- ... float Y = Node->getRotationY(); Y += 0.2; quaternion qt = Euler2Quaternion(0,Y,0); ... HERE I GET EULER VALUES FROM QUATERNION (qt) BY MATRIX CALCULATION Node->setRotationY(Y); // this work ok, but when Y value is near 90, 180, 270 degrees I have problem, rotation is stopping, Y isn't go up... How can I solved my problem? Maybe I should use matrix for translation my rotations? [Edited by - carew on December 3, 2008 5:45:07 AM]

##### Share on other sites
Assuming there are no errors in the code you haven't posted, I'd say that's a normal case of gimbal lock.

This article on this very site explains what that is and offers solutions.

##### Share on other sites
Can You show me a better solution for gimbal lock than this from article?

##### Share on other sites
Quote:
 Original post by carewCan You show me a better solution for gimbal lock than this from article?

Not really. The only way to get rid of it is to get rid of Euler angles.

In the example you posted that would actually be very easy, because you're only rotation around the y axis.

The article gives the formula for creating a rotation around the y axis:

Qy = [ cos(b/2), (0, sin(b/2), 0)]

##### Share on other sites

Now I have last problem. When I convert quaternion to euler, my euler values don't have values (0,360), but (-90,90). How can I calculate independent X,Y and Z values (range 0,360) from quaternion?

[Edited by - carew on December 2, 2008 8:18:27 AM]

##### Share on other sites
Quote:
 Original post by carewNow I have last problem. When I convert quaternion to euler, my euler values don't have values (0,360), but (-90,90). How can I calculate independent X,Y and Z values (range 0,360) from quaternion?

Converting from a Quat to Euler angles, is a bit like buying a ferrari and towing it behind a horse. Just because you can do it, doesn't make it any less stupid.

Anyhow, basically you can't get eulers returned in the 0 to 360 range. Quat's represent rotations in the -180 to 180 degree range around a specific axis. All Quat->Euler and Euler->Quat methods work by doing Quat->Matrix->Euler and Euler->Matrix->Quat respectively.

An euler rotation in one axis of: 0,360,720 or 1080 produces the identity matrix. It therefore always produces the identity quat, and since the specific input angles are lost, the Quat to Euler will always returns 0,0,0 in that case. Applications such as Maya/Max etc handle this problem by storing euler angles, which are converted to quats whenever they are needed. At no point do they ever do a Quat->Euler conversion.

The rotations returned from a Quat to euler conversion, will always be in the -90 to 90 degree range. The other angles typically flip from -90 to 90 to be able to represent the rotations outside of that range. This is pretty typical, and the same is true of axis_angle to euler and matrix to euler conversions.

The only way to get something that goes outside the -90 to 90 range is to have a sequence of quats that need converting. In that case, you can compare the returned euler angles to the previous one to see if x/y or z have jumped 180 degrees. It's not so much an algorithm as a hard hack - and i've typically only see it used to convert mocap data into euler angles for Maya/Max etc.

My normal advice in these situations is to throw everything you know about euler angles in the bin. Think of a Quat as just a rotation matrix, and you've pretty understood them. The rules of multiplication, quat inverse, etc are all the same as with matrices (though quats are more efficient). For rendering, just convert it to a matrix....

##### Share on other sites
Quote:
 Original post by RattenhirnNot really. The only way to get rid of it is to get rid of Euler angles.

Which doesn't actually solve gimbal lock. Consider the following 3 quats:

Qx = [ cos(ex/2), (sin(ex/2), 0, 0)]Qy = [ cos(ey/2), (0, sin(ey/2), 0)]Qz = [ cos(ez/2), (0, 0, sin(ez/2))]

If you do the multiplication:

Q = Qx * Qy * Qz

you've got a possible gimbal lock. (It's the same reason you get gimbal with euler angles - you're only doing 3 axis angle rotations in a row).

The only way that quats solve gimbal lock is if you use 1 of them to represent a rotation (and you may still run into it in some very rare edge cases where it occurs).

##### Share on other sites
OK, I have object rotation in quaternion and I have to move my object forward. Eg 5 units. When I have euler this is very easy process, so how can i calculate direction for this movment from quaternion?

##### Share on other sites
Quote:
 Original post by carewOK, I have object rotation in quaternion and I have to move my object forward. Eg 5 units. When I have euler this is very easy process, so how can i calculate direction for this movment from quaternion?
Simply convert the quaternion to a matrix, and then extract the direction vectors directly from the matrix rows or columns. (It's actually very easy - in fact, the process is arguably more straightforward than the corresponding Euler-angle conversion.)

##### Share on other sites
Very thanks for Yours help :)

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 10
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632941
• Total Posts
3009333
• ### Who's Online (See full list)

There are no registered users currently online

×