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