1
\$\begingroup\$

I'm using LibGDX and I'm trying to implement a FPS camera into my game. I'm not using the FirstPersonCameraController for multiple reasons:

  • I don't want to touch to roll (only yaw and pitch), with this camera when looking on sides the horizon goes up and down like waves (meaning that the roll is changing)
  • I don't want dragging, I want the camera to always move with the mouse.
  • I want movements to stay consistent whatever the player is looking at. I don't want it to go slower even if he looks at the ground.

So I've come up with this which does pretty much what I want:

    @Override
    public boolean mouseMoved(int screenX, int screenY)
    {
        float deltaX = (mousePos.x - screenX) * sensitivity;
        float deltaY = (mousePos.y - screenY) * -sensitivity;

        Vector3 tmpAxis = game.getGame().getPool().obtain(Vector3.class);

        game.getCamera().rotate(Vector3.Y, deltaX);
        tmpAxis.set(game.getCamera().direction.z, 0, -game.getCamera().direction.x);

        game.getCamera().rotate(tmpAxis, deltaY);
        //if(game.getCamera().up.y < 0)
        //  game.getCamera().up.y = 0;

        mousePos.set(screenX, screenY, 0);
        game.getGame().getPool().free(tmpAxis);
        return true;
    }

However, I have an issue described by the commented lines. When the camera goes to low or too high (when the y value of the up vector become negative), the rotation is reversed and the rotation starts to make it come back. In pratice, it just flickers a lot and when I try to go back, I have 1/2 chance of going back normally, and 1/2 chance of looking backward with my camera reversed (if the up vector is negative).

So naturally, what I want is to lock the movement so It can never look pass his feets or pass the sky. Locking the up vector is easy, I just have to prevent it from going negative but I need to replace the direction vector also. However, I don't know how to "replace" the direction vector. I search on google for how to set the camera rotation or just how to get it and didn't found anything convincing.

I've just tried also adding game.getCamera().direction.y = -1f; to my commented if to make it look down when getting past the point I don't want it to get past. However, I found out the direction vector isn't normalized and that's really confusing.

\$\endgroup\$
  • \$\begingroup\$ just keep the orientation as two euler angles instead of vectors, and limit them. update vectors from these angles when needed, it's just some trivial math \$\endgroup\$ – Sarge Borsch Nov 10 '16 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.