Begins explaining my achieve: as I said in my last question, I am tryna makin' a 2D game with 3D player as Factorium. For hours I am tryna to make a kinda of bounding system:

1)Camera will follow player untill the left border of the screen will reach the left edge of the map

2)If left edge has been reached, only player keep walking towards left

3)Once player come back, camera will follow player again only when it will have reached the right distance

These steps for each side

First image: left edge of the map still not reached, so camera keep following player constraints it at its center.

Second image: camera reached it, but layer kept waling, then begins come back

Thirds image: distance between player and left edge is at lest as half camera viewport, so camera begins follow player again.

Of course, I tried something...

 * Using tile map proeprties
 * @param translateCords how much player has to be moved
 * @return if the player can me moved<br> e.g. if the camera will not be over the map's edge
 * after have been moved
private boolean canPlayerMove(Vector3 translateCords) {
    Vector3 playerPos = this.entityPlayer.getPosition();
    Vector3 futurePos = playerPos.add(translateCords.x, translateCords.y, 0);

    TiledMapTileLayer.Cell cell = ((TiledMapTileLayer)this.tiledMap.getLayers().get(0)).getCell((int)futurePos.x, (int)futurePos.y);
    return cell.getTile().getProperties().containsKey("blocked");

This for move camera

 * @param translateCords how much camera has to be moved
 * @param camPos current camera position
 * @return if the camera can me moved<br> e.g. if the camera will not be over the map's edge
 * after have been moved
private boolean canCameraMove(Vector2 translateCords, Vector3 camPos){
    if( (translateCords.x < 0 && camPos.x - this.viewportWCam >= 0) ||
        (translateCords.x > 0 && camPos.x + this.viewportWCam <= this.widthWorld) ||
        (translateCords.y < 0 && camPos.y - this.viewportHCam >= 0) ||
        (translateCords.y > 0 && camPos.y + this.viewportHCam <= this.heightWorld)) {
        return true;
    return false;

I tried a lots of other stuff...


1 Answer 1


To make the camera be centered on the player, but also not pan beyond the bounds of the map you simply need to clamp the camera position in such a way that is never is more than half the visible distance from an edge.

So for example

Vector2 playerPos = /* you set this somehow */
float mapWidth = /* this should be the width of your map in world space */
float mapHeight = /* this should be the height of your map in world space */

float cameraX = Math.min(Math.max(playerPos.x, camera.viewportWidth / 2.0f), mapWidth - camera.viewportWidth / 2.0f);
float cameraY = Math.min(Math.max(playerPos.y, camera.viewportHeight / 2.0f), mapHeight - camera.viewportHeight / 2.0f);
camera.position.set(cameraX, cameraY, 0);

Side note; the above fails if the camera viewport is larger than the entire map.

  • \$\begingroup\$ About the problem if the viewport is larger, I could set in runtime the it as the half of map... And could you help me also to not allow player go over map edge? \$\endgroup\$
    – DynoZ
    Commented Mar 21, 2021 at 20:44
  • \$\begingroup\$ Your answer has been very useful. But doesn't work very properly: it works when player walks torwards left and down edges of map, but not when to right and up \$\endgroup\$
    – DynoZ
    Commented Mar 22, 2021 at 7:27
  • 1
    \$\begingroup\$ Edit: I fixed it... my mapWidth/mapHeight didn't considering the width/height of tiles (16 px in my case) \$\endgroup\$
    – DynoZ
    Commented Mar 22, 2021 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .