I have a camera in OpenGL and it can move on the X and Z axises.
You can rotate the camera with the left and right arrow buttons and move forward, backward, left and right with the WASD buttons. Here is the method for movement.
public void move(float amount, float dir) {
z += amount * Math.sin((Math.toRadians(ry + 90 * dir)));
x += amount * Math.cos((Math.toRadians(ry + 90 * dir)));
}
"amount" is the speed and "dir" is either "0" or "1" so it sets "90" to "0" or leaves it as "90". "z" is the position on the z axis. So as "x" and "y". "rz" is rotation on the z axis. So as "rx" and "ry".
With these, I can not move on the Y axis, which is UP and DOWN. I managed to add the rotation code to make the camera LOOK UP and DOWN but I can't make the camera go where you are LOOKING at. Here is the rotation method:
public void rotate(float amount, float way) {
if (way == ROTATE_RIGHT)
ry += amount;
else if (way == ROTATE_LEFT)
ry -= amount;
if (way == ROTATE_UP && rx >= -90)
rx -= amount;
else if (way == ROTATE_DOWN && rx <= 90)
rx += amount;
}
This is how I call the move() method.
if (Keyboard.isKeyDown(Keyboard.KEY_W))
cam.move(0.01f, Camera.MOVE_STRAIGHT);
if (Keyboard.isKeyDown(Keyboard.KEY_S))
cam.move(-0.01f, Camera.MOVE_STRAIGHT);
if (Keyboard.isKeyDown(Keyboard.KEY_A))
cam.move(0.01f, Camera.MOVE_STRAFE);
if (Keyboard.isKeyDown(Keyboard.KEY_D))
cam.move(-0.01f, Camera.MOVE_STRAFE);
Camera.MOVE_STRAIGHT = 1 and Camera.MOVE_STRAFE = 0 and cam is a Camera object.