1
\$\begingroup\$

I'm currently programming a game using LibGDX and box2D. I have a sprite (player) that moves up the screen, kind of like doodle jump.

I'd like to have my camera to be centered around the player at all times--the sprite will be in the center of my screen as they move up, or as they fall down.

I'd also like to have a high score panel that is always at the top left corner of the screen.

As of now, I have this done, to an extent. As long as the player doesn't move to far up the screen at a time, it will remain centered, and the high score will generally be in the top. Sometimes it'll move off the screen, but it will come back as the player falls down.

Most of my confusion lies from the LibGDX coordinate system, so if you could explain that, that would enhance my understanding of the problem.

Here is the code I have:

//Access the world's reference to Player, and update the position of it's sprite with respect to the body
  world.getPlayer().updatePosition();
  world.getBlock().updatePosition();

  //Have the camera follow the player, but only in the y position
  batch.setProjectionMatrix(camera.combined);
  camera.position.set(camera.position.x, world.getPlayerSprite().getY() - 75, 0);
  camera.update();

  //Begin batching sprites here. This will include blocks and the player
  batch.begin();

  debugMatrix = batch.getProjectionMatrix().cpy().scale(100f,
        100f, 0);


  //Render the Player sprite here
  batch.draw(world.getPlayerSprite(), world.getPlayerSprite().getX(), world.getPlayerSprite().getY(),world.getPlayerSprite().getOriginX(),
        world.getPlayerSprite().getOriginY(),
        world.getPlayerSprite().getWidth(),world.getPlayerSprite().getHeight(),world.getPlayerSprite().getScaleX(),world.getPlayerSprite().
                getScaleY(),world.getPlayerSprite().getRotation());
  batch.draw(world.getPlatformSprite(), world.getPlatformSprite().getX(),
          world.getPlatformSprite().getY(),world.getPlatformSprite().getOriginX(),
          world.getPlatformSprite().getOriginY(),world.getPlatformSprite().getWidth(),
          world.getPlatformSprite().getHeight(),world.getPlatformSprite().getScaleX(),
          world.getPlatformSprite().getScaleY(),world.getPlatformSprite().getRotation());
  batch.draw(world.getBlockSprite(), world.getBlockSprite().getX(),
          world.getBlockSprite().getY(),world.getBlockSprite().getOriginX(),
          world.getBlockSprite().getOriginY(),world.getBlockSprite().getWidth(),
          world.getBlockSprite().getHeight(),world.getPlatformSprite().getScaleX(),
          world.getBlockSprite().getScaleY(),world.getBlockSprite().getRotation());

  Gdx.app.log("Position", "" + camera.position.y + " " + world.getPlayerSprite().getY() + " " + (camera.position.y - 275));
  font.draw(batch, "Score:" + (int)Math.ceil(22-world.getPlayerBody().getPosition().y), 0, camera.position.y - 275);

  batch.end();
\$\endgroup\$
1
\$\begingroup\$

You should not use the same SpriteBatch for drawing your game elements (players, paltforms etc.) as for drawing your HUD.

What are you are doing here is applying the camera matrix to your sprite batch, which means that everything rendered using this sprite batch will be affected by the camera.

batch.setProjectionMatrix(camera.combined);

What you can do:

  • Use another sprite batch for your HUD (and do not apply the camera projection matrix on this one)
  • Use Scene2D for your HUD (that's what i would do)

I would not use Scene2D for gameplay rendering but i think that using it for HUD is a very elegant solution. It will help you separate your gameplay logic from your HUD logic and will handle really nice things for you like screen resizing etc.

\$\endgroup\$
  • \$\begingroup\$ Excellent. Using a separate sprite batch worked for the HUD \$\endgroup\$ – Peter Kaminski Apr 2 '16 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.