Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  How to get Yaw, Pitch, and Roll from a Matrix?  (Read 1491 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-05-23 03:44:29 »

Is this correct? If now can you post what would be?

this is the site I'm getting my formulas from: http://planning.cs.uiuc.edu/node103.html
1  
2  
3  
float yaw = (float) -(Math.tan(m.m10/m.m00));
        float pitch = (float) -(Math.tan((-m.m20)/(Math.sqrt(Math.pow(m.m21, 2) + Math.pow(m.m22, 2)))));
        float roll = (float) -(Math.tan(m.m21/m.m22));
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #1 - Posted 2011-05-23 06:15:42 »

Hi

Take a look at the source for Java3D or Ardor3d, they will do the maths in there somewhere, for ardor it's done using quaternions, which can be calculated from a matrix and then converted to euler angles.

HTH

Endolf

Offline Roquen
« Reply #2 - Posted 2011-05-23 11:53:55 »

Euler angles:  Just say no (at runtime).  Seriously, they aren't interesting unless you're doing some specific types of integration.

Want to improved speed? If not: Do you want to improve your accuracy?  If yes to at least one, then:

Math.pow(x,2) -> x*x
Math.sqrt(Math.pow(x,2)) -> Math.abs(x)

If you still decide to use Euler angles, then note that most references will be for right-handed coordinate frames (instead of OpenGL's left-handed).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #3 - Posted 2011-05-23 12:03:23 »

Unless you are doing simple rotations with them (land vehicles) you're best off using quaternions.

I'm no mathematician, see here for some discussion.

Endolf

Offline CyanPrime
« Reply #4 - Posted 2011-05-23 16:53:28 »

Math.sqrt(Math.pow(x,2)) -> Math.abs(x)

You forgot the + between the two Pow calls.
1  
Math.sqrt(Math.pow(m.m21, 2) + Math.pow(m.m22, 2))


Also is there a way to use quaternions from a openGL call, or a matrix to move/rotate?

Offline CyanPrime
« Reply #5 - Posted 2011-05-23 19:13:32 »

Alright, this is what I got so far, but my camera doesn't seem to move at all. Even if I put lookAt on (5,50,5) it stays looking foward. This is my code so far:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix3f;
import org.lwjgl.util.vector.Vector3f;

public class Camera {
    //3d vector to store the camera's position in
   Vector3f position = null;
    Vector3f lookAt = null;
    Vector3f up = null;
    //the rotation around the Y axis of the camera
   float yaw = 0;
   
    //the rotation around the X axis of the camera
   float pitch = 0;
   
    //the rotation around the Z axis of the camera
   float roll = 0;
   
    public Camera(float x, float y, float z)
    {
        //instantiate position Vector3f to the x y z params.
       position = new Vector3f(x, y, z);
        lookAt = new Vector3f();
        up = new Vector3f(0,1,0);
    }
   
    public void lookThrough()
    {
       Matrix3f m = new Matrix3f();
       
       
       up = new Vector3f(0,1,0);
       Vector3f out = new Vector3f();
       Vector3f.sub(position, lookAt, out);
       out.normalise();
       Vector3f.sub(up, (Vector3f) out.scale(Vector3f.dot(out,up)), up);
       
       Vector3f camUp = new Vector3f();
       Vector3f.sub(up , (Vector3f) out.scale(Vector3f.dot(out,up)), camUp);
       //set forward vector
      m.m00 = 0;
       m.m01 = 0;
       m.m02 = 1;
       
       //set right vector
      m.m10 = 1;
       m.m11 = 0;
       m.m12 = 0;
       
       //set up vector
      m.m20 = camUp.x;
       m.m21 = camUp.y;
       m.m22 = camUp.z;
       
       if(m.m00 != 0)
          yaw = (float) Math.atan2((-m.m20),(Math.sqrt(Math.pow(m.m21, 2) + Math.pow(m.m22, 2))));
       else
          yaw = 0;
       
       pitch = (float) Math.atan2(m.m21,m.m22);
       
       if(m.m22 != 0)
          roll =  (float) Math.atan2(m.m10,m.m00);
       else
          roll = 0;
       
       System.out.println("roll: " + roll + " pitch: " + pitch + " yaw: " + yaw);
       
        //roatate the pitch around the X axis
       GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        //roatate the yaw around the Y axis
       GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        //roatate the yaw around the Y axis
       GL11.glRotatef(roll, 0.0f, 0.0f, 1.0f);
        //translate to the position vector's location
       GL11.glTranslatef(position.x, position.y, position.z);
    }
}


And this is my output:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0
roll: 1.5707964 pitch: 2.819842 yaw: 0.0


Yaw should be at something aside of 0, right?
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #6 - Posted 2011-05-24 06:24:02 »

1  
2  
m.m00 = 0;
if(m.m00 != 0)...;else yaw = 0;

do you see?

lots of sillystupid games: http://www.emaggame.com
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-05-24 15:29:59 »

If you still decide to use Euler angles, then note that most references will be for right-handed coordinate frames (instead of OpenGL's left-handed).

I'm pretty sure OpenGL is right-handed, they just do some weirdness where the camera looks down the negative z-axis.

Offline Roquen
« Reply #8 - Posted 2011-05-24 15:51:44 »

You're correct.  It's Direct X that uses a left handed coordinate frame..shame on me.
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.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!