【发布时间】: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() 之后将光标的位置设置回中心,但这非常有问题。
我不是在寻找对我的代码的更正,而是在寻找一种最佳实践的好方法。
【问题讨论】: