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

Junior Devvie

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

Junior Devvie

 « 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.
 Games published by our own members! Check 'em out!
Porlus

Junior Devvie

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

Junior Devvie

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

Junior Devvie

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

Thanks for your reply. 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.
Porlus

Junior Devvie

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

 orrenravid (242 views) 2016-07-16 03:57:23 theagentd (312 views) 2016-07-11 14:28:54 Hydroque (397 views) 2016-07-06 05:56:57 Hydroque (555 views) 2016-07-03 08:52:54 GrandCastle (417 views) 2016-07-01 09:13:47 GrandCastle (410 views) 2016-07-01 09:09:45 CopyableCougar4 (464 views) 2016-06-25 16:56:52 Hydroque (433 views) 2016-06-22 02:17:53 SwampChicken (397 views) 2016-06-20 13:22:57 SwampChicken (308 views) 2016-06-20 13:22:49
 Archive 30x Slyth2727 24x Brynn 23x EgonOlsen 22x orangepascal 21x orange451 20x TritonDreyja 19x Spasi 18x Opiop 11x LiquidNitrogen 11x Longor1996 10x ziozio 10x Phased 10x h.pernpeintner 9x theagentd 9x KaiHH 9x
 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