Euler angles/3D rotation problem

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:


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

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.

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. :slight_smile:

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.

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


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.

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:


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

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

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!