0
votes

I have run into a problem making a first person camera on LWJGL 2. I am using the following code to rotate the camera (up down left and right) based on how the mouse moves. This is basically what every other tutorial has, however, its movement is flawed and ends up spiraling out of control.

    float mouseDX = Mouse.getDX();
    float mouseDY = Mouse.getDY();

    rotation.x = mouseDX;
    rotation.y = mouseDY;

    glRotatef(rotation.y, 1, 0, 0);
    glRotatef(rotation.x, 0, 1, 0);

Rotation is a Vector3f

I am aware that the rotation.y is rotating the x access and the x is rotating the y. I am not totally sure why but it doesn't work for me unless its this way. The problem may be related to this.

Here is a video I made showing what I mean: https://www.youtube.com/watch?v=V6Iu5oQuWo4&feature=youtu.be

In the video I attempt to show that both the x and y rotation work fine separately, but when used together they don't work at all.

I know this is only a small section of my code, but it is the only part dealing with rotation so the problem must be there somewhere.

1
If you have a matrix defining the position of your person, you can use its inverse as view matrix. - mad_manny

1 Answers

0
votes

The flaw that stands out to me is the value by which you rotate.

Mouse.getDY returns the change in y pixels so if you move your mouse half way down the screen you will move typically 300 pixels (800x600).

Now you also have glRotatef which rotates by radians which compared are tiny compared to degrees.(360 degrees -> 6.28 radians)

Now take 300 hundred pixels, use it as the number of radians to rotate by and you get 17188.7 degrees of rotation. And that's the cause of your spiralling (47 revs/few milliseconds)

What you will need to do if divide your dy and dx by a good couple of hundred. And you can also still use degrees by using Math.toRadians in the glRotatef method