0

i have this trouble with making a first person camera i'm trying different ways that i find on google but i don't understand how to do it still here is how my camera class looks like

private Vector3f position = new Vector3f(0, 0, 0);
private float pitch;
private float yaw;
private float roll;

public Camera() {
    Mouse.setGrabbed(true);
}

public void move() {
    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        position.x += (float) (Math.sin(Math.toRadians(rotY)) * 0.5);
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
        position.x += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        position.x -= 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
        position.z += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
        position.y += 0.05f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
        position.y -= 0.05f;
    }
    yaw += Mouse.getDX();
    pitch += -Mouse.getDY();
}

public Vector3f getPosition() {
    return position;
}

public float getPitch() {
    return pitch;
}

public float getYaw() {
    return yaw;
}

public float getRoll() {
    return roll;
}

I calculate the rotation and stuff in another class called maths and i give it to the shaders to render but i don't know how to make it move in the direction of the pitch yaw.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    read [Understanding 4x4 homogenous transform matrices](http://stackoverflow.com/a/28084380/2521214) especially the last two links. To remedy your code you need to compute vectors pointing `forward` and `right` from your Euler angles. How that depends on the order of your angles ... Then in moving forward/backward you add/sub `forward` vector to camera position and in moving right/left you add/sub `right` vector to camera position. You are just increasing/decreasing single coordinate which works only if you are in specific rotation .... – Spektre Feb 12 '17 at 10:46
  • i didnt understand them thats why i asked this – HydraulicHydrox Feb 12 '17 at 20:32

1 Answers1

0

The rotation of the movement of the camera does not actually come from the rotation of the camera axis itself (x,y,z). The movement of the camera can actually be calculated through incrementation in the x and z direction based on the camera's yaw angle.

For your example in pseudo:

dx (change in x)  = -1*Math.sin(yaw)*speed (where speed is a constant the camera moves at.)

dz (change in z) = Math.cos(yaw)*speed

position.z+=dz;

position.x+=dx;

This works by incrementing the x and z position value based on a revolution of the yaw axis (360) degrees, although java will require you to convert these to radians if you haven't already!

Tom Zych
  • 13,329
  • 9
  • 36
  • 53