Libgdx tutorial beginner Part 5 – Collision, Camera

Like the previous part for animations heere we will look into collision and camera logic.

  • Bounds is a rectangle that we check for collision with tiles on the map.
  • We get the tiles for collision using fetchCollidableRects.
  • We do two checks for collision, one for left/right and the other for top/bottom
  • In fetchCollidableRects we get the tiles around the player, in case CUBE is fixed we also get its tile

  • OrthographicCamera is our camera that follows bob. Constructs a new OrthographicCamera, using the given viewport width and height.
  • At start it will be positioned in the same position as Bob.
  • To change its position we are using lerp. Lerp linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
  • If we are following cube, cubes position will be used.

11 responses to “Libgdx tutorial beginner Part 5 – Collision, Camera”

  1. Paul says:

    great tutorial so far. Please can you post the required changes to Player and Level to allow the code to compile.

  2. Michael Roy says:

    Awesome, this tutorial worked great. When can we get more? =P

    I’d love to see a tutorial with some collision detection or a scoring system.

    • metin says:

      There is already some collision detection logic but I’m guessing what you want is bullets and stuff. I’m going to do some code cleaning for part 6 and then in 7 I’ll add some bullet logic.

  3. KG says:

    Thanks for sharing the source code to this project! It’s been helpful. I was wondering if you were familiar with the LibGDX tutorials from and if you had referenced the same book on game development. The source code in that tutorial and here were very similar, but the other doesn’t cover enemies and score system. Do you have other resources to recommend for beginners?

  4. metin says:

    Yes I did reference some parts from that site which I mention in the first part I think, but the collision was not working so I rewrote it. Other sources that I know

  5. KG says:

    Oh right you did mention it in part 1, which I skipped. 😀 I’ll check out the tutorials from the Github link. Thanks!

  6. Dave says:

    Hello and many thanks for this great tutorial! Can you please enlight me on how we can change the width and height of the hit box of the sprite? I modified the drawDebug method and the playerRect.set(…) lines to a diferent width, height and x,y positions, but these changes did not work properly. Thanks in advance 🙂

    • metin says:

      hi thank you,
      that should have worked actually, I suggest you edit the draw debug first, see the changes in position and size, then edit set rectangle accordingly

      • Dave says:

        Thanks for the quick answer 😀 I changed the drawDebug for a visual representation of what I want, and then changed both set rectangle lines. Everything works fine except the top colision, when I try to jump to destroy the block above, I jump through it and land on the block. I guess I have to change the code for the “check above blocks for colision”. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *