LWJGL unlimited mouse movement

bluefireoly :

I am wondering what is a good approach on "unlimited mouse movement"? (Like in first person games where you can look around infinitely)

I am using OpenGL and LWJGL (which provides bindings for Java). I have the following possible bindings: https://www.lwjgl.org/customize (listed under contents) Currently I am only using GLFW to handle the mouse input.

My current approach is the following, but obviously the cursor eventually reaches the screen edge:

public class MouseInput {

    private final Vector2d previousPosition;
    private final Vector2d currentPosition;

    private final Vector2f displayVector;

    private boolean inWindow = false;

    // [some code here]

    public void init() {

        glfwSetCursorPosCallback(window.getHandle(), (windowHandle, xpos, ypos) -> {
            currentPosition.x = xpos;
            currentPosition.y = ypos;
        });

        glfwSetCursorEnterCallback(window.getHandle(), (windowHandle, entered) -> {
            inWindow = entered;
        });

        // [some code here]

    }

    public void input() {

        displayVector.x = 0;
        displayVector.y = 0;

        if (previousPosition.x > 0 && previousPosition.y > 0 && inWindow) {

            double deltaX = currentPosition.x - previousPosition.x;
            double deltaY = currentPosition.y - previousPosition.y;

            if (deltaX != 0) {
                displayVector.y = (float) deltaX;
            }
            if (deltaY != 0) {
                displayVector.x = (float) deltaY;
            }

        }

        previousPosition.x = currentPosition.x;
        previousPosition.y = currentPosition.y;

    }

    // [some code here]

}

Now I can use the calculated displayVector somewhere else to rotate the camera.

Do I have to use something different than GLFW? I tried setting the position of the cursor back to the center after every input(), but that was very glitchy.

I am not looking for a correction of my code, but for a good approach which is the best practice.

genpfault :

glfwSetInputMode():

  • GLFW_CURSOR_DISABLED hides and grabs the cursor, providing virtual and unlimited cursor movement. This is useful for implementing for example 3D camera controls.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25278&siteId=1