I've been following some tutorials about libGdx and I've tried to create a 2d platformer but I've got stuck at how would be the right way to make the camera not go out of the screen when the player moves :( The camera follows the player when it moves on the x coordinate position but if i go near where the map ends the camera will go out of the screen and i'm not sure how to solve that enter image description here

Here is the main code of the game:

public PlayScreen(SuperShooter game){
        this.game = game;
        atlas = new TextureAtlas("Diz_Pack.atlas");
        gamecam = new OrthographicCamera();
        gamePort = new StretchViewport(SuperShooter.V_WIDTH / SuperShooter.PPM, SuperShooter.V_HEIGHT / SuperShooter.PPM, gamecam);

        hud = new Hud(game.batch);
        mapLoader = new TmxMapLoader();
        map = mapLoader.load("Level1WithObjects2.tmx");
        renderer = new OrthogonalTiledMapRenderer(map, 1/SuperShooter.PPM);
        //initailly set our gamecam to be centered correctly at the start of
        gamecam.position.set(gamePort.getWorldWidth()/2 , gamePort.getWorldHeight()/2, 0);

        world = new World(new Vector2(0,-10), true);

        new B2WorldCreator(world, map);

        controller = new Controller(game);

        // b2dr = new Box2DDebugRenderer();
        player = new John(world, this);

        world.setContactListener(new WorldContactListener());


public void update(float dt){
        world.step(1/60f, 6, 2); // in order for box2d to execute our physics simulation we need to tell it how many times to calculate per second
        gamecam.position.x = player.b2body.getPosition().x; 

public void render(float delta) {

        Gdx.gl.glClearColor(1, 1, 1, 1);
        //render our Box2dDebugLines
        //b2dr.render(world, gamecam.combined);


        game.batch.setProjectionMatrix(hud.stage.getCamera().combined);  //you have something that can draw to the screen via gpu like the spritebatch. And you have a camera.
        //you gotta tell the spritebatch where we are / where the camera is looking, because on screen shall be what the camera is looking at.


The camera is at first set on the left bottom of the screen but then, while the game updates it's placed on the x position of the player and that's why it looks like that:( do you guys know how should it be done to show only the map when the player moves? should i position the camera in front of the player or something like that?

Help would be appreciated! thanks

  • \$\begingroup\$ i had a similar problem when i made a top down 2d game similar to zelda 1. Basically i solved it by having the camera move function follow the player IF it is not less than or greater than the 'bounds' of the world (also including the offset for the width and height of the camera viewport) \$\endgroup\$ Sep 5, 2019 at 14:39

1 Answer 1


I haven't ever used LibGDX, but what you'll have to do is find the width of the camera viewport, and limit the x position of the camera like this pseudocode:

camera.x = min((levelWidth / 2) + levelPosition.x - (camera.viewportWidth / 2), camera.x);

Essentially, we find the smaller number with the min function, which will limit our camera.x value to the x position of where the camera can be and not show any of the outside of the level. The OrthographicCamera class from LibGDX provides the viewportWidth value, so you only need to make sure you know where your level object is located and how wide it is.

That code with prevent you from having the camera go too far to the right, to prevent it going to far too the left, use an implementation similar to:

camera.x = max(-(levelWidth / 2) + levelPosition.x + (camera.viewportWidth / 2), camera.x);

This does a similar thing to the original, it just ensures that the camera's left bound never goes further left than the leftmost point on the level. Both of these snippets assume that the position of the level is in its center. If the level is at (0, 0), then the levelPosition.x can be omitted completely:

camera.x = min((levelWidth / 2) - (camera.viewportWidth / 2), camera.x);
camera.x = max(-(levelWidth / 2) + (camera.viewportWidth / 2), camera.x);
  • \$\begingroup\$ Hello! thanks for answering! I'm not really sure what do you mean with levelPosition.x. Is that the position of the player? \$\endgroup\$ May 26, 2018 at 23:15
  • \$\begingroup\$ @Player It would just be where the center of the level is, so if you spawn the level around the origin (0, 0), then you can ignore it completely. \$\endgroup\$
    – CJ Burkey
    May 27, 2018 at 15:02

You must log in to answer this question.

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