Camera clipping issue

Hello! I’m currently working on a solo project with LibGDX for fun to learn game mechanics and other things and I can’t get my head around one issue.

My problem is simple - the camera that follows my player is not clipping properly. When the player moves right or left the screen doesn’t always catch up properly. This is best seen when I’m falling infinitely from which the character will be shaking heavily in the middle of the screen.

Here is my player class: http://pastebin.java-gaming.org/da9f55d003d18

My main loop updates all possible objects in the game and then draws them. If the “thing” passed in the loop is an instance of a player than set the camera on it.


public void render() {
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      batch.setProjectionMatrix(camera.combined);
      hud.setProjectionMatrix(screen.combined);
      debugger.setProjectionMatrix(camera.combined);
      /**
       * Update everything in the game.
       */
      
      for (Thing thing : level.getThings.array()) {   
         thing.update(Gdx.graphics.getRawDeltaTime());
      }
      
      //Render the map
      level.getRender().setView(camera);
      level.getRender().render();
      
      batch.begin();
      for (Thing thing : level.getThings.array()) {
         if(thing instanceof Player){
            Player player = (Player) thing;
            float offset = player.getXFrame();
            batch.draw(thing.getAnimationFrame(), thing.getX() + offset, thing.getY(),
                  thing.getAnimationFrame().getRegionWidth() * level.SCALE,
                  thing.getAnimationFrame().getRegionHeight() * level.SCALE);
            camera.position.x = player.getHitBox().x + player.getHitBox().width/2;
            camera.position.y = player.getHitBox().y + player.getHitBox().height/2;
            //camera.position.x = MathUtils.clamp(camera.position.x, 300f, level.getLayer("background").getWidth()*level.SCALE);
         }
         else {
         batch.draw(thing.getAnimationFrame(), thing.getX(), thing.getY(),
               thing.getAnimationFrame().getRegionWidth() * level.SCALE,
               thing.getAnimationFrame().getRegionHeight() * level.SCALE);
         }
      }
      batch.end();
      
      hud.begin();
         font.draw(hud, version, 30, 30);
      hud.end();
      
      debugger.setColor(Color.BLACK);
      debugger.begin(ShapeType.Line);
      if (DEBUG_LEVEL) {
         for (Block block : level.getBlocks.array()){
            debugger.rect(block.get().x, block.get().y, block.get().width,
                  block.get().height);
         }
      }
      if (DEBUG_THINGS) {
         for (Thing thing : level.getThings.array()) {
            debugger.rect(thing.getX(), thing.getY(), thing.getWidth(),
                  thing.getHeight());
         }
      }
      debugger.end();
      camera.update();
   }

I’m using LibGDX and I can’t figure out if translating the camera is the cause (I’ve read that there are many clipping issues with using floats, but I haven’t seen it happen with many tests)

Any advice would be extremely appreciated, thank you!

First guess: try setting the camera position prior to all rendering for that frame, rather than in the middle of the rendering.