Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Euler angles to quaternion: Is this correct?  (Read 1626 times) 0 Members and 1 Guest are viewing this topic.
theagentd

« JGO Bitwise Duke »

Medals: 410
Projects: 2
Exp: 8 years

 « Posted 2012-12-17 20:38:23 »

Quick question:

I have the Euler angles (defined as rotX, rotY, rotZ in radians) and I'm trying to convert these to a quaternion. Now, my understanding of quaternions is extremely limited, but I'm wondering if the following code is working:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `    public void setOrientation(float x, float y, float z){      x*=0.5f;      y*=0.5f;      z*=0.5f;            float c1 = (float)Math.cos(x);      float c2 = (float)Math.cos(y);      float c3 = (float)Math.cos(z);            float s1 = (float)Math.sin(x);      float s2 = (float)Math.sin(y);      float s3 = (float)Math.sin(z);            localOrientation = new Quaternion(            s1*s2*c3 + c1*c2*s3,            s1*c2*c3 + c1*s2*s3,            c1*s2*c3 - s1*c2*s3,            c1*c2*c3 - s1*s2*s3      );   }`

Input:
 1 `1.570796 0.000000 0.000000`

Output:
 1 `0.0 0.70710665 0.0 0.7071069`

From what I can see, the direction seems to be off, assuming bones aren't supposed to extrude in a 90 degree angle out of the body. =S

Myomyomyo.
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #1 - Posted 2012-12-17 21:12:37 »

I'm not really sure about what you are trying to archive and I'm not a pro at Quaternion math at all, but I tried to do some googleing and searching in Stackoverflow.

I've found this and I found very much similarities between your code and the one on the site. But I'm not pretty sure what you want to do with this
 `x = (float) Math.PI/2;`
... Is it supposed to be there? As well as the *= 0.5f; operations?
I guess they are... I've got no idea about what you are doing anyways, but I hope the site I've linked helps you

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
theagentd

« JGO Bitwise Duke »

Medals: 410
Projects: 2
Exp: 8 years

 « Reply #2 - Posted 2012-12-17 21:20:42 »

I've found this and I found very much similarities between your code and the one on the site. But I'm not pretty sure what you want to do with this
 `x = (float) Math.PI/2;`
... Is it supposed to be there?
Ah, dammit, that was a remnant of a test. Someone mentioned that you sometimes had to add PI/2 to the x rotation. That line used to be x += (float)Math.PI/2...

That was the site I used to write that code. If you look at it, you'll see that they all need the rotation angles divided by 2, so I just did that once in the beginning.

Anyway, I think I got it working correctly, at least for this simple conversion. I just switched around the coordinates so it became like this:

 1  2  3  4  5  6  7 `      float c1 = (float)Math.cos(z);      float c2 = (float)Math.cos(y);      float c3 = (float)Math.cos(x);            float s1 = (float)Math.sin(z);      float s2 = (float)Math.sin(y);      float s3 = (float)Math.sin(x);`

Still not sure if it's really correct though...

Myomyomyo.
 Games published by our own members! Check 'em out!
Roquen
 « Reply #3 - Posted 2012-12-17 21:29:53 »

No time to look through this atm, but it should be: {sin(t/2)u, cos(t)} where t is the angle of orientation and 'u' is the unit axis of rotation.
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #4 - Posted 2012-12-17 21:31:03 »

Oh yes... I just further searched wikipedia::

Seems like
• Heading = z rotation
• Elevation = y rotation
• Bank = x rotation

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
theagentd

« JGO Bitwise Duke »

Medals: 410
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2012-12-17 22:05:25 »

Yup! Though I have to say that naming the axes like that makes no sense what so ever.

On a slightly unrelated note, now I just need to figure out what this means:
Quote
X is north in SMD. Elsewhere in Source it is east.
X is NORTH?!

Mapping the position and normal coordinates like this produces the right result but is it really correct?
 1  2  3  4  5 `   public void setPosition(float x, float y, float z){      this.x = x;      this.y = z;      this.z = -y;   }`

Myomyomyo.
Stranger

Senior Devvie

Medals: 9

I'm going slightly mad. It finally happened.

 « Reply #6 - Posted 2012-12-18 07:33:32 »

This should work:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 ` public static Quaternion4f eulerToQuaternion( float eulerX, float eulerY, float eulerZ )    {        double sx = Math.sin( eulerX / 2 );        double sy = Math.sin( eulerY / 2 );        double sz = Math.sin( eulerZ / 2 );        double cx = Math.cos( eulerX / 2 );        double cy = Math.cos( eulerY / 2 );        double cz = Math.cos( eulerZ / 2 );        double cycz = cy * cz;        double sysz = sy * sz;        double d = cycz * cx - sysz * sx;        double a = cycz * sx + sysz * cx;        double b = sy * cz * cx + cy * sz * sx;        double c = cy * sz * cx - sy * cz * sx;        Quaternion4f q = new Quaternion4f( ( float ) a, ( float ) b, ( float ) c, ( float ) d );        q.normalize();        return ( q );    }`

Anton
Roquen
 « Reply #7 - Posted 2012-12-18 13:02:59 »

The end result depends on the conventions...how the pre-defined euler angles match yours.  right vs left handed & how they point within each.  What I'm going to assume you're asking is:  local coordinate frame changes of x followed by y followed by z.  Quaternion perspective:  rotation of 'a' radians about unit axis in direction 'u' = (sin[a/2]u, cos[a/2]),  so:

R0 = (sin[x/2](1,0,0), cos[x/2]) = ((sx,0,0),cx)
R1 = (sin[y/2](0,1,0), cos[y/2]) = ((0,sy,0),cy)
R2 = (sin[z/2)(0,0,1), cos[z/2]) = ((0,0,sz),cz)

If right handed coordinate frames, then the product of quaternions A & B (AB) can be considered a global rotation of B with respect to A...OR a local rotation of A with respect to B.  So since Euler angles are local rotations:

R0R1R2 = ((cy cz sx + cx sy sz, cx cz sy - cy sx sz, cz sx sy + cx cy sz), cx cy cz - sx sy sz)

If you change to left-handed, then the notions of global & local flip:

R2R1R0 = ((cy cz sx - cx sy sz, cx cz sy + cy sx sz, cx cy sz - cz sx sy), cx cy cz + sx sy sz)

Notice that neither of these match Stranger's code, as there is a different ordering occurring...there are combinatorial number of ways to define Euler-angles and ya gotta know which it is.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ags1 (18 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (47 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (32 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (58 views) 2015-03-10 11:36:42 ClaasJG (42 views) 2015-03-10 11:33:01
 LiquidNitrogen 21x BurntPizza 21x basil_ 19x KevinWorkman 18x EgonOlsen 17x theagentd 16x Roquen 16x 65K 11x Riven 11x wessles 11x Varkas 11x princec 9x Ashedragon 8x Rayvolution 8x phu004 8x SHC 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org