Hey guys! First post here
So I have a little problem. I have a 2D platformer, and I want to keep the player in the center of the screen. Well, that should be easy right? Just set the camera position to half the size of the screen plus half the size of the player. So, I did that. But the camera actually scrolls slower than the player, so that the player can actually escape the screen. I’ll show you some code now:
The camera class:
public Camera(){
}
public void update(Player p){
glTranslatef(Main.WIDTH / 64 / 2 + -p.getPos().x / 2, Main.HEIGHT / 64 / 2 - -p.getPos().y / 2, 0);
}
Here’s how I update my player’s position:
public void update(){
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
this.move(MOVESPEED, 0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
this.move(-MOVESPEED, 0);
}
}
Which then in turn calls the move method:
public void move(float x, float y){
this.getPos().set(this.getPos().x + x, this.getPos().y + y);
}
I create the player and initialize it’s position to half the screen, which works fine. But when I start scrolling, the player isn’t centered in the screen! How can I fix this?