0

I have a camera, with a yaw and pitch.

The position of the camera is x, y, and z.

I need a way to translate the yaw and a button press (WASD) to movement along the x and z.

So, I press W, and the camera matrix moves backwards, giving the appearance of moving forwards, based off of the yaw, but I don't know the formula to do so.

Camera class (I dont think its needed):

public static Vector3f position = new Vector3f(0, 1, 0);
public static double sensitivity;
private float pitch;
private float yaw;
private float roll;
private boolean escape;
private boolean fullscreen;

public Camera() {
    escape = false;
    fullscreen = false;
    Mouse.setGrabbed(true);
}

public void move() {
    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
        if (escape == true) {
            Mouse.setGrabbed(true);
            Mouse.setCursorPosition(Display.getWidth()/2, 
            Display.getHeight()/2);
            escape = false;
        } else if (escape == false) {
            Mouse.setGrabbed(false);
            escape = true;
        }
    }
    //Not working rn
    if (Keyboard.isKeyDown(Keyboard.KEY_GRAVE) && escape == false) {
        if (fullscreen == true) {
            try {
                Display.setFullscreen(false);
            } catch (LWJGLException e) {
                e.printStackTrace();
            }
            fullscreen = false;
        } else {
            try {
                Display.setFullscreen(true);
            } catch (LWJGLException e) {
                e.printStackTrace();
            }
            fullscreen = true;
        }
    }
    //Change pitch based off of mouse y
    if (Mouse.getY() != Display.getHeight() / 2 && escape == false) {
        pitch += (Display.getHeight()/2 - Mouse.getY())/2;
        if (pitch > 360) {
            pitch = pitch % 360;
        }
    }
    //Change yaw with mouse movement
    if (Mouse.getX() != Display.getWidth() / 2 && escape == false) {
        yaw -= (Display.getWidth()/2 - Mouse.getX())/2;
        if (yaw > 360) {
            yaw = yaw % 360;
        }
    }
    //get out of the game
    if (escape == false) {
        Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() 
        /2);
    }

    //Here is the movement code, currently just moves along x/z normally
    if (Keyboard.isKeyDown(Keyboard.KEY_S) && escape == false) {
        position.x -= (Math.sin(yaw)*.2)*sensitivity;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_W) && escape == false) {
        position.x += (Math.sin(yaw)*.2)*sensitivity;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_A) && escape == false) {
        position.z -= (Math.cos(yaw)*.2)*sensitivity;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_D) && escape == false) {
        position.z += (Math.cos(yaw)*.2)*sensitivity;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && escape == false) {
        position.y -= 0.015;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && escape == false) {
        position.y += 0.015;
    }
}

public Vector3f getPosition() {
    return position;
}

public float getPitch() {
    return pitch;
}

public float getYaw() {
    return yaw;
}

public float getRoll() {
    return roll;
}

Thanks for reading.

EDIT:

Found a solution here [How to move my camera straight to mouse cursor, with any rotation?

For some reason if I look forwards my W moves forwards, if I look backwards I move backwards, but no in between

New code:

public static final Vector3f getRotationVector() { float r = (float) Math.toRadians(roll); Matrix4f mat = GameMath.createTransformationmatrix(new Vector3f(), pitch, yaw, 0, 1); Vector3f vector = new Vector3f(mat.m20, mat.m21, -mat.m22); return new Vector3f((float)(vector.x * Math.cos(r) + vector.y * Math.sin(r)), (float)(vector.y * Math.cos(r) - vector.x * Math.sin(r)), vector.z); }

Movement:

if (Keyboard.isKeyDown(Keyboard.KEY_S) && escape == false) {
        position.x -= getRotationVector().x;
        position.z -= getRotationVector().z;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_W) && escape == false) {
        position.x += getRotationVector().x;
        position.x += getRotationVector().z;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_A) && escape == false) {
        position.z -= getRotationVector().z;
        position.x -= getRotationVector().x;
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_D) && escape == false) {
        position.z += getRotationVector().z;
        position.x += getRotationVector().x;
    }
Big_Bad_E
  • 947
  • 1
  • 12
  • 23
  • You will need some trigonometry to do this. I suggest starting with movement in a 2D plane. Given a current (x, y) position and a heading angle theta. How do you calculate the new position (x', y')? Get a piece of paper and pencil and figure out how to do this by hand before trying to write code to do it. – Code-Apprentice Jun 19 '18 at 22:47
  • You will also want to learn some linear algebra. Once you understand matrix multiplication, the formulas for these kinds of transformations become easier to manage. – Code-Apprentice Jun 19 '18 at 22:48
  • [This question](https://stackoverflow.com/questions/11994819/how-can-i-move-the-camera-correctly-in-3d-space) uses C++ instead of Java, but it shows some concepts from OpenGL – Code-Apprentice Jun 19 '18 at 22:50
  • And here's another one: https://stackoverflow.com/questions/38825659/fix-for-3d-camera-to-move-in-the-direction-its-facing – Code-Apprentice Jun 19 '18 at 22:52
  • Take a look at the links I gave at the top of your question. If you still need help, feel free to post a new question with updates about what you have learned. – Code-Apprentice Jun 19 '18 at 22:54
  • Code-Apprentice I went through those questions, and I think you should reopen this as it is NOT a duplicate, those questions don't answer my question completely, though I found this line. Math::RotationMatrix(rot.Z, 0, 0, 1.0f); There is no Math.rotationMatrix method in the Math class. – Big_Bad_E Jun 19 '18 at 23:03
  • I agree that this isn't an exact duplicate. I ope that the links provide some information that you can use to come up with your own solution. You will need to find the correct Java versions of the OpenGL functions to build your rotation and translation matrices. – Code-Apprentice Jun 19 '18 at 23:15
  • I do not know that much trigonometry, do you know a solution to this Code-Apprentice? I haven't seen anything. – Big_Bad_E Jun 19 '18 at 23:34
  • I got my backwords working right, so ima just debug it myself :3 – Big_Bad_E Jun 19 '18 at 23:58

0 Answers0