Jump to content
  • Advertisement
Sign in to follow this  
carew

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by carew
Can 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 this post


Link to post
Share on other sites
Thanks for Your help.

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 this post


Link to post
Share on other sites
Quote:
Original post by carew
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by Rattenhirn
Not 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by carew
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?
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!