Java-Gaming.org Hi !
 Featured games (88) games approved by the League of Dukes Games in Showcase (681) Games in Android Showcase (196) games submitted by our members Games in WIP (744) 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 2407 times) 0 Members and 1 Guest are viewing this topic.
theagentd

« JGO Bitwise Duke »

Medals: 798
Projects: 4
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: 138
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: 798
Projects: 4
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: 138
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: 798
Projects: 4
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: 12
Exp: 10 years

I STILL HATE DOTA !

 « 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.

 CopyableCougar4 (55 views) 2016-06-25 16:56:52 Hydroque (89 views) 2016-06-22 02:17:53 SwampChicken (90 views) 2016-06-20 13:22:57 SwampChicken (92 views) 2016-06-20 13:22:49 SwampChicken (85 views) 2016-06-20 13:22:26 Hydroque (129 views) 2016-06-15 08:22:50 Hydroque (123 views) 2016-06-13 06:40:55 DarkCart (240 views) 2016-05-29 02:30:33 Hydroque (206 views) 2016-05-26 14:45:46 Mac70 (188 views) 2016-05-24 21:16:33
 Spasi 49x theagentd 45x NegativeZero 39x KaiHH 30x LiquidNitrogen 23x princec 19x CoDi^R 16x ra4king 13x ags1 12x kingroka123 10x Opiop 10x purenickery 9x Hydroque 8x TritonDreyja 8x Archive 8x SHC 8x
 Making a Dynamic Plugin Systemby Hydroque2016-06-25 00:13:25Java Data structures2016-06-13 21:22:09Java Data structures2016-06-13 21:20:42FPS Camera Tutorialby Hydroque2016-05-22 05:40:58Website offering 3D Models specifically for games for freeby vusman2016-05-18 17:23:09Website offering 3D Models specifically for games for freeby vusman2016-05-09 08:50:56Website offering 3D Models specifically for games for freeby vusman2016-05-06 11:10:21Website offering 3D Models specifically for games for freeby vusman2016-04-29 12:56:17
 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