I'm trying to make it like an FPS style camera where you can look around using the mouse. I've got it kind of working but when I move around and then look horizontally, it rotates everything from the original point. What am I doing wrong?
private float moveSpeed = 0.1f;
private Vector3f camera;
private float horizontalAngle = 0.0f;
private float verticleAngle = 0.0f;
public Game() {
Mouse.setGrabbed(true);
camera = new Vector3f(0.0f, 0.0f, 0.0f);
}
public void input(){
horizontalAngle += Mouse.getDX() * 0.05f;
verticleAngle += -Mouse.getDY() * 0.05f;
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
camera.x -= moveSpeed * Math.sin(Math.toRadians(horizontalAngle));
camera.z += moveSpeed * Math.cos(Math.toRadians(horizontalAngle));
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)){
camera.x += moveSpeed * Math.sin(Math.toRadians(horizontalAngle));
camera.z -= moveSpeed * Math.cos(Math.toRadians(horizontalAngle));
}
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
camera.x -= moveSpeed * Math.sin(Math.toRadians(horizontalAngle - 90));
camera.z += moveSpeed * Math.cos(Math.toRadians(horizontalAngle - 90));
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
camera.x -= moveSpeed * Math.sin(Math.toRadians(horizontalAngle + 90));
camera.z += moveSpeed * Math.cos(Math.toRadians(horizontalAngle + 90));
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
Mouse.setGrabbed(false);
}
}
public void update(){
}
public void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw quad
glTranslatef(camera.x, camera.y, camera.z);
glRotatef(verticleAngle, 1.0f, 0.0f, 0.0f);
glRotatef(horizontalAngle, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex3f(0.0f, 0.0f, -5.0f);
glVertex3f(1.0f, 0.0f, -5.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
glVertex3f(0.0f, 1.0f, -5.0f);
glEnd();
glRotatef(-horizontalAngle, 0.0f, 1.0f, 0.0f);
glRotatef(-verticleAngle, 1.0f, 0.0f, 0.0f);
glTranslatef(-camera.x, -camera.y, -camera.z);
Display.sync(60);
Display.update();
}
I'm using LWJGL with Java.