java - Java 中的鼠标移动 FPS 样式

标签 java rotation lwjgl frame-rate

所以我正在尝试实现一个简单的 mouseLook 方法来探索 LWJGL 中的游戏世界。现在我只希望它在旋转 Y 轴上工作。由于屏幕的尺寸阻止鼠标移动,起初移动受到限制。所以我隐藏了光标,并在返回 deltaX 后将鼠标的 x 位置设置为 0。当我把它写在纸上时,我认为计算似乎是正确的,但是当我运行它时,旋转不断回到 0,并产生抖动效果。我该如何解决这个问题?

public void rotate() { //rotate the camera

    System.out.println(getGlobalDX() + "   " + MouseInfo.getPointerInfo().getLocation().getX());

    float speed = 0.03f;

    //START OF CODE TO IGNORE
    float ratioX = (float)Display.getWidth() / 360;
    float ratioY = (float)Display.getHeight() / 360;

    float mouseRotX = Mouse.getX() / ratioX;
    float mouseRotY = Mouse.getY() / ratioY;

    //END OF CODE TO IGNORE

    rotY += getGlobalDX() * speed; // add the deltaX of the mouse position X and multiply it by our speed in order to rotate the camera based on the mouse
    lockCursor();
}

public float getGlobalDX() { //returns the difference between the current mouse position and the mouse position during the last frame



    frames += 1; //add 1 to frames variable every frame

    if(frames >= 1.5) { //if 1.5 frames have passed since last method call, run method again. this is to smooth things out since it doesnt work properly every frame

        float mouseX = (float) MouseInfo.getPointerInfo().getLocation().getX(); //get x pos of mouse
        frames = 0; //set frames to zero

        if(lastMouseX != mouseX) { //if the lastmouse position isnt equal to our current mouse position, then continue

            if(mouseX == 963) { //if the mouse position is equal to the centered X position of the screen then continue

                lastMouseX = (float) MouseInfo.getPointerInfo().getLocation().getX(); //set this variable to the new x position
                System.out.println("returning zero"); //debug by checking if this code runs (it does)
                return 0; //return 0 since we dont want to undo the position we returned last frame

            }

            float deltaX = (float) (mouseX - lastMouseX); // difference between current mouse position and last to get the overall change in position
            lastMouseX = (float) MouseInfo.getPointerInfo().getLocation().getX(); //set to new x position
            MouseInfo.getPointerInfo().getLocation().setLocation(MouseInfo.getPointerInfo().getLocation().getX()/2, MouseInfo.getPointerInfo().getLocation().getY()/2); // set mouse position to 0 for infinite movement
            return deltaX; //return the overall change in position and add it to the rotation Y of our camera

        } else { // return 0 if the lastMouse position is equal to the current mouse position, since there was no change in position

            return 0;

        }

    } else { //return 0 since a frame was skipped

        return 0;

    }


}

最佳答案

与其自己重置鼠标的位置,不如在开始为此使用鼠标之前调用 Mouse.setGrabbed(true)

关于java - Java 中的鼠标移动 FPS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868592/

相关文章:

javascript - Java - ScriptEngineManager nashorn Math.random 不起作用

java - maven java.lang.NoClassDefoundError Linux

c# - 以编程方式(在 C# 中)从 javaw.exe 进程获取 java 应用程序名称?

java - Mac 和 Windows 上的字节码

ios - 在 Swift 4 中用一根手指进行旋转

graphics - 在 Silverlight 中旋转图像而不裁剪

javascript - 如何在 Samsung Tizen Gear s2 中使用边框滚动列表中的项目?

OpenGL 2D 形状问题

Java FloatBuffer 大小错误 OpenGL

java - ArrayList 中的顶点缓冲区对象?