Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Euler angles/3D rotation problem  (Read 889 times) 0 Members and 1 Guest are viewing this topic.
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
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.

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

Porlus

Junior Member

 « 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 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().  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.
 BurntPizza (16 views) 2014-09-19 03:14:18 Dwinin (34 views) 2014-09-12 09:08:26 Norakomi (61 views) 2014-09-10 13:57:51 TehJavaDev (84 views) 2014-09-10 06:39:09 Tekkerue (42 views) 2014-09-09 02:24:56 mitcheeb (64 views) 2014-09-08 06:06:29 BurntPizza (47 views) 2014-09-07 01:13:42 Longarmx (35 views) 2014-09-07 01:12:14 Longarmx (39 views) 2014-09-07 01:11:22 Longarmx (36 views) 2014-09-07 01:10:19
 BurntPizza 38x Riven 18x Rayvolution 17x princec 17x basil_ 16x ags1 16x KevinWorkman 15x kevglass 12x LiquidNitrogen 11x nsigma 11x theagentd 11x HeroesGraveDev 9x The Lion King 7x SHC 6x Gibbo3771 6x cylab 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-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