Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Euler angles to quaternion: Is this correct?  (Read 5275 times) 0 Members and 1 Guest are viewing this topic.
theagentd
 « 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
 « 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.
Roquen

JGO Kernel

Medals: 517

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

JGO Kernel

Medals: 517

 « 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

 EgonOlsen (59 views) 2018-06-10 19:43:48 EgonOlsen (42 views) 2018-06-10 19:43:44 EgonOlsen (61 views) 2018-06-10 19:43:20 DesertCoockie (241 views) 2018-05-13 18:23:11 nelsongames (142 views) 2018-04-24 18:15:36 nelsongames (141 views) 2018-04-24 18:14:32 ivj94 (883 views) 2018-03-24 14:47:39 ivj94 (144 views) 2018-03-24 14:46:31 ivj94 (795 views) 2018-03-24 14:43:53 Solater (159 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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