简体   繁体   中英

3rd person camera movement in LWJGL3

I've been following along with ThinMatrix's OpenGL tutorial on making a game in Java recently. However as he uses LWJGL2, and I'm using LWJGL3, there's a few differences that require some work arounds. I'm stuck at one point in particular pertaining to creating a 3rd person character on a "player".

I've done enough so that when I click and drag the screen, the camera rotates around the player like it should. However when I let go and move my mouse to make another rotation, instead of continuing from where the position is, it resets it relative to where my second click is.

As LWJGL3 doesn't have a mouse.getDY() or mouse.getDX(), I made one in my DisplayManager class like so:

public float getDY() {
    newMouseY = (float) getMouseY();
    float dy = newMouseY - oldMouseY;
    oldMouseY = newMouseY;
    return dy;
}

public float getDX() {
    newMouseX = (float) getMouseX();
    float dx = newMouseX - oldMouseX;
    oldMouseX = newMouseX;
    return dx;
}

And I call it in my camera class like so:

private void calculatePitch(DisplayManager window) {
    if (window.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
        float pitchChange = window.getDY() * 0.2f;
        pitch -= pitchChange;
    }
}

private void calculateAngleAroundPlayer(DisplayManager window) {
    if (window.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
        float angleChange = window.getDX() * 0.3f;
        angleAroundPlayer -= angleChange;
    }
}

I'm just not sure if this should work and I'm missing something really obvious, or it can't be done this way. I'm pretty new to game dev.

Managed to figure out the issue, all I had to do was call my getDX() and getDY() functions again after the mouse has been pressed in my calculations:

private void calculatePitch(DisplayManager window) {
    if (window.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
        float pitchChange = window.getDY(window) * 0.2f;
        pitch += pitchChange;
    }
    window.getDY(window);
}

private void calculateAngleAroundPlayer(DisplayManager window) {
    if (window.isMouseDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
        float angleChange = window.getDX(window) * 0.3f;
        angleAroundPlayer -= angleChange;
    }
    window.getDX(window);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM