I have a side scroller game made in OpenGL, and I'm trying to center the player in the viewport when he moves. I know how to do it:
cameraX = Width / 2 / TileSize - playerPosX
cameraY = Height / 2 / TileSize - playerPosY
However, I have a problem. The player and "camera" move, but the player moves faster than the "camera" scrolls. So, the player can actually move out of the screen.
Some code, this is how I translate the camera:
public Camera(){
}
public void update(Player p){
glTranslatef(-p.getPos().x - Main.WIDTH / 64 / 2, -p.getPos().y - Main.HEIGHT / 64 / 2, 1);
}
Here's how I move the player:
public void update(){
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
this.move(MOVESPEED, 0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
this.move(-MOVESPEED, 0);
}
}
The move method:
public void move(float x, float y){
this.getPos().set(this.getPos().x + x, this.getPos().y + y);
}
And then after I move the player, I update the player's geometry, which shouldn't matter.
What am I doing wrong here, this seems like such a simple problem, yet it doesn't work!