0
votes

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.

1
Try this: Initialize your oldMouseX and oldMouseY to -1. On the first frame you detect a click, you dont move the camera, just set these two varaibles to the current screen pos. From the second one onwards, you start rotating the camera. Whenever you detect the player has stopped clicking, reset the oldMouse[X|Y] variables to -1. On a new click, repeat the process.Nadir
I see no matrices no transformations either what I see is just updating some Euler angles without any order ... Without knowing your coordinate system, notations and transform math we can only guess see Understanding 4x4 homogenous transform matrices once you grasp the basics see the last links in there ... where player and camera control examples are ... The idea is to take direct matrix of your player ... transform it to relative camera position then invert the result and use it as camera ...Spektre

1 Answers

0
votes

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);
}