Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quaternion To Euler

Started by Xeee Jul 3, 2003 at 10:01 PM 9 replies 24.1k views
Original Post
Xeee
Xeee
How to convert/extract a quaternion to euler angles? thanks very much... xee..
xee..
Sander
Sander
Go to Darwin 3D and look at the GDC papers on Inverse Kinematics (2001 i believe). He''s provided an extension to 3D which uses a Quaternion to Euler function but it''s a bit messy. However, it''s te only on I could find.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
Wudan
Wudan
Why would you want to? Not to start a quaternion/euler debate, but it should be pointed out that rotations on 2 axii(sp) (the 3rd axis having no rotation value specified) converted to a quaternion and then back will result in a rotation on all 3 axii. This is because there are obviously several sets of euler angles that achieve the same quaternion. I''d be interested in knowing how to solve for the other sets, if someone has a bright idea. I''m sure I could figure it out, in time.

I could post my function for doing this conversion, if you wanted.
Wudan
Wudan
I went to Darwin3D to see if what they had was close enough to what I had, because I remember reading about it on Darwin3D when I was researching the same problem.

Darwin3D's code:
http://www.darwin3d.com/gamedev/quat2eul.cpp

the Code I'm using, based on a few different snippets I've seen but modified for my uses:

void QuatToEuler(const quat_t *quat, double *rotx, double *roty, double *rotz)
{
double sqw;
double sqx;
double sqy;
double sqz;

double rotxrad;
double rotyrad;
double rotzrad;

sqw = quat->w * quat->w;
sqx = quat->x * quat->x;
sqy = quat->y * quat->y;
sqz = quat->z * quat->z;

rotxrad = (double)atan2l(2.0 * ( quat->y * quat->z + quat->x * quat->w ) , ( -sqx - sqy + sqz + sqw ));
rotyrad = (double)asinl(-2.0 * ( quat->x * quat->z - quat->y * quat->w ));
rotzrad = (double)atan2l(2.0 * ( quat->x * quat->y + quat->z * quat->w ) , ( sqx - sqy - sqz + sqw ));

*rotx = rad2deg(rotxrad);
*roty = rad2deg(rotyrad);
*rotz = rad2deg(rotzrad);

return;
}


I used double because single wasn't cutting the mustard.

[edited by - Wudan on July 8, 2003 2:43:08 PM]
Sander
Sander
quote:
Original post by Wudan
Why would you want to? Not to start a quaternion/euler debate, but it should be pointed out that rotations on 2 axii(sp) (the 3rd axis having no rotation value specified) converted to a quaternion and then back will result in a rotation on all 3 axii.

Well, I''ve been playing with quaternions myself for use in Inverse Kinematics and I have come across one good reason to convert back to eulers. You''re right, it''s ugly but I could not find any other way.

In my IK implementation I needed to limit the degrees of freedom of joints, represented by quaternions (like the IK papers at Darwin3D). I needed to limit the rotation around the X and Z axis to 25 degrees and around the Y axis to 15 degrees (I was making a 16 bone tentacle. Realtime 3D IK).

If you can figure out how to do this while staying in quaternion space then you''re my absolute hero I had to convert to Eulers, limit the DOF''s, and convert back to quaternions. Very messy, and it didn''t work properly.



Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
Sander
Sander
AP: That''s the problem with Eulers. You don''t know the order. IIRC there are 6 or 12 different possibilities and you don''t know which one you need to get the correct rotation. Most people however just choose one order (usually xyz or zyx) and stick with it, which will give you gimble lock. Hence quaternions and matrices. Quaternions are slightly better though because they can be calculated faster and are numerically more stable.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!
Wudan
Wudan
The order I use is XYZ. I wrote an animation scripting system that generates quats from euler angles, and from quats to euler angles, which is where I realized the true nature of the Gimbal problem. There''s oodles of papers about it if you Google it.
bjmumblingmiles
bjmumblingmiles
Hey Sander, is any of your IK stuff open-source? I''m working on an IK demo just so I can learn how to do it...What you are doing sounds kinda similar, so if you have anything for me I''d appreciate it!


Brian J
DL Vacuum - A media file organizer I made | Mumbling Miles - A band I was the guitarist/vocalist for
Brian J
Sander
Sander
Yes it is Go to my site and download the RoboBlast demo. It comes with full source. Note however that I still need to update the download (new demo isn''t finished yet). In the old one, there''s a bug in the IK system. It uses the system with quaternions as described on darwin3d.com. I got soooo very frustrated by the bug that I threw it all out and implemented Huge Elias'' method. (it''s slower but it works flawless). Kudos to you if you can find the bug in my old method. It *should* work but it doesn''t always

Keep an eye on the news page to see when the new demo is up.



Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.