Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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/3D rotation problem  (Read 901 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2013-04-03 18:03:45 »

Hi all, I've been trying to make a 3D camera using Euler angles and I've been getting some unexpected results. I'm using LWJGL and I've plotted out a very basic flat scene to test whether the camera's working properly. I used the algorithm at the end of the FAQ question below to create and multiply the 3 rotation matrices X, Y and Z.

http://www.cs.princeton.edu/~gewang/projects/darth/stuff/quat_faq.html#Q36

To transform to the perspective of the rotated viewport I have the method below:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void transform(Player player) {
   float xcos = (float) Math.cos(Math.toRadians(xrot));
   float xsin = (float) Math.sin(Math.toRadians(xrot));
   float ycos = (float) Math.cos(Math.toRadians(yrot));
   float ysin = (float) Math.sin(Math.toRadians(yrot));
   float zcos = (float) Math.cos(Math.toRadians(zrot));
   float zsin = (float) Math.sin(Math.toRadians(zrot));
     
   float[] mat = new float[16];
   mat[0] = ycos * zcos;
   mat[1] = -ycos * zsin;
   mat[2] = ysin;
   mat[4] = xsin * ysin * zcos + xcos * zsin;
   mat[5] = -xsin * ysin * zsin + xcos * zcos;
   mat[6] = -xsin * ycos;
   mat[8] = -xcos * ysin * zcos + xsin * zsin;
   mat[9] = xcos * ysin * zsin + xsin * zcos;
   mat[10] = xcos * ycos;
   mat[3] = mat[7] = mat[11] = mat[12] = mat[13] = mat[14] = 0.0f;
   mat[15] = 1.0f;
   
   GL11.glMultMatrix((FloatBuffer) BufferUtils.createFloatBuffer(16).put(mat).flip());
}


With xrot, yrot and zrot being the rotation angles in degrees. I have some other methods that just add to these values to add/subtract from the angles.
The results are fairly hard to describe. When I only change one rotation angle the scene seems to rotate correctly, but when I add a second the scene seems to spiral out of control. Can anyone spot any errors that I've made?

Thanks
Offline Tyecon

Senior Newbie





« Reply #1 - Posted 2013-04-03 21:13:57 »

Here you can use one of my old camera's for reference: http://pastebin.java-gaming.org/4c7cb59245c
The big problem with that one though is that is recalculates the matrix every frame, you should have a camera.setPosition/Rotation which sets a boolean flag to true, then when the update is called if the boolean flag is true update the matrix, that will save performance.

Please have a baggy ready before looking at my code.
Offline Porlus

Junior Member





« Reply #2 - Posted 2013-04-03 21:18:15 »

Cheers for the reply. Yeah, I've got a few optimisation methods in mind. I'm just stumped by this unexpected transformation it's returning. It looks fairly straight forward in the article. I even double checked that all the matrix entries were being multiplied correctly. I'll give your implementation a look. Thanks. Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Porlus

Junior Member





« Reply #3 - Posted 2013-04-03 22:04:57 »

I've just tried implementing the same method from the code you provided, but I'm getting the same problem. If I rotate around one axis it's fine, but if I include the second rotate it seems to spiral. Even if I don't prompt it to rotate anymore, it continues to spiral.
Offline Porlus

Junior Member





« Reply #4 - Posted 2013-04-03 22:56:22 »

Sorry to keep posting, I've just realised something else. I've just replaced the contents of the method with this:

1  
2  
3  
GL11.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
GL11.glRotatef(zrot, 0.0f, 0.0f, 1.0f);


But the problem persists, so it must not be the rotations that cause the problem.
Offline Tyecon

Senior Newbie





« Reply #5 - Posted 2013-04-03 23:38:25 »

Make sure that it's not rotating above 360, and also for the look up/look down make sure you limit the angle to 89/179 or the tan function will glitch.

For example in my camera:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
if (cameraRot.x+mouseDX>=360) {
            cameraRot.x=cameraRot.x+mouseDX-360;
        } else if (cameraRot.x+mouseDX<0) {
            cameraRot.x=360-cameraRot.x+mouseDX;
        } else {
            cameraRot.x+=mouseDX;
        }
        if (cameraRot.y-mouseDY>=-90 && cameraRot.y-mouseDY<=90) {
            cameraRot.y+=-mouseDY;
        } else if (cameraRot.y-mouseDY<-90) {
            cameraRot.y=-90;
        } else if (cameraRot.y-mouseDY>0) {
            cameraRot.y=90;
        }


Also I would search up gimbal lock if your going to use euler angles (http://www.youtube.com/watch?v=zc8b2Jo7mno).

Please have a baggy ready before looking at my code.
Offline Porlus

Junior Member





« Reply #6 - Posted 2013-04-04 00:18:46 »

Thanks for your reply. Smiley I've added those checks, but it seems to still glitch. I think it may be the X axis rotation that's causing the problems. If I drag the mouse down, the camera doesn't appear to move down at all and when I drag the mouse up it's very sensitive and spins around quickly and then continues to spin after I've stopped moving the mouse.
Offline Porlus

Junior Member





« Reply #7 - Posted 2013-04-04 00:26:51 »

It wasn't even my camera class that was faulty, I was using Mouse.getY() instead of Mouse.getDY().  Cry I feel so foolish. Thanks to everybody for the help!
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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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!