【问题标题】:LWJGL unlimited mouse movementLWJGL 无限鼠标移动
【发布时间】:2020-03-04 22:25:00
【问题描述】:

我想知道“无限鼠标移动”的好方法是什么? (就像在第一人称游戏中你可以无限地环顾四周)

我正在使用 OpenGL 和 LWJGL(为 Java 提供绑定)。我有以下可能的绑定:https://www.lwjgl.org/customize(在目录下列出) 目前我只使用 GLFW 来处理鼠标输入。

我目前的做法如下,但显然光标最终会到达屏幕边缘:

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]

}

现在我可以在其他地方使用计算得到的 displayVector 来旋转相机。

我必须使用与 GLFW 不同的东西吗?我尝试在每次 input() 之后将光标的位置设置回中心,但这非常有问题。

我不是在寻找对我的代码的更正,而是在寻找一种最佳实践的好方法。

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    glfwSetInputMode():

    • GLFW_CURSOR_DISABLED 隐藏并抓取光标,提供虚拟和unlimited 光标移动。这对于实现例如 3D 相机控件很有用。

    【讨论】:

    • 这似乎只适用于两个方向(向下和向右)。编辑:这可能与我在 input() 方法中的代码有关。
    • 我必须删除 previousPosition.x > 0 && previousPosition.y > 0 条件。但是现在,玩家旋转的次数越多,值就会越来越高。我能做些什么呢?重置到一定数量?
    猜你喜欢
    • 2015-02-01
    • 2023-03-31
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多