Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1365 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline matheus23

JGO Kernel


Medals: 109
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 Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline matheus23

JGO Kernel


Medals: 109
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
Offline 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.
Offline Stranger

Senior Duke


Medals: 7



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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!