I am trying to make a two sided platform game in Libgdx. Where I am scrolling through the TiledMap. I was easily implementing the horizontal scrolling using..
camera.position.x = player.getPositionX();
and it works fine. But when it comes to Vertical scroll .. I am observing very bad effects doing the same thing on y-axis. I tried it many ways. But No code could give could a real look and feel. Later I happen to find this video
http://blog.mimeoverse.com/post/581467761/the-ideal-platformer-camera-should-minimize
it says " The ideal platformer camera should:
minimize vertical motion lead in the direction the player is facing remain motionless during horizontal micro-adjustments The secret to achieving all three is (what I call) the trap. The trap is an invisible rectangle (made visible for this demo), roughly twice as wide and tall as the player sprite, with the same starting coordinates. Rather than follow the player sprite directly the camera follows the trap. When the player moves within the trap, the camera does not move. The trap only moves when the player pushes against one of its edges. " it demonstrates a good idea but Still there still seems something bad .. I am writing some wrong code
Rectangle trap_old,trap_new;
.....
trap_old = new Rectangle(player.getPositionX()-2*player.getWidth(),
player.getPositionY()+5*player.getHeight(),
5*player.getWidth(),
6*player.getHeight());
//.. inside render
trap_new = getNewTrapArea(player.getPositionX(),
player.getPositionY(),
player.getWidth(),
player.getHeight());
camera.position.x = trap_new.x;
camera.position.y = trap_new.y;
camera.update();
private Rectangle getNewTrapArea(float positionX, float positionY, float width, float height) {
Rectangle temp = new Rectangle(positionX, positionY, width, height);
if(temp.overlaps(trap_old))
trap_new = trap_old;
else
trap_old = trap_new = new Rectangle(positionX-2*width, positionY+5*height, 5*width, 6*height);
return trap_new;
}
5*player.getHeight()
instead of subtracting it. I can't tell what coordinate system you're using. \$\endgroup\$