Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) 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 1214 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.

 MrMapcom (13 views) 2015-05-23 20:26:16 MrMapcom (19 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (35 views) 2015-05-20 03:42:21 BurntPizza (70 views) 2015-05-10 15:53:18 FrozenShade (55 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (207 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x BurntPizza 16x Riven 14x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x KevinWorkman 9x princec 8x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x SauronWatchesYou 6x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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