LibGDX Tutorial - Advanced collision detection (Common Problems 3)

If you have followed all the tutorials since the beginning, then you now have a fully working and responsive game. Don't be afraid to make changes yourselves and make your own unique game with the help of everything you have learnt until now.

A common problem that you may encounter and a little imperfection of our current game is the collision detection. And by that, i don't mean the actual code of the collision detection because this works great, but i mean the fact that, even though our hero's shape cannot be defined by a simple geometrical shape like a rectangle or a circle, his actual Box2D body is a rectangle.

" What does this mean? "

It means that the player could lose the game even though the graphic of our hero hasn't touched an enemy, because the collision takes place at a lower level, at the level of the bodies that we have created. 

That's the reason why we created circular enemies, even though it would be better to define their shapes as circles and not as boxes, but this is totally easy and to be done. All that we would need to do, would be to just change the shape from a box to a circle and instead of defining the width and the height, we would have to define the radius of the circle. 

We also made the animations of our enemies and hero a little bit bigger, because it is always better for the player to not lose when he sees a little overlap in the graphics, than to lose without the graphics having touched each other.

To sum up;


  1. You can make the graphics to have simple geometrical shapes, like rectanglse or circles.
  2. You can make the graphics bigger than their bodies, to avoid some unwanted situations, like losing when you clearly can't see the graphics to have touched each other.


Unfortunately, if you want to create a more complex game, the above solution will not work at all. You would have to create your own functions that create complex shapes if for exampled your goal is to create a complex platform game.

There are some tools that you can use that do exactly that; They create such complex geometrical shapes. There are a lot of them out there, some are free and for some others you need to pay. Here is a list of some tools that i know of.

- Physics Body Editor. This is a free tool that you can use to create complex shapes. I personally have used it and it is very helpful. You just run the executable, then you trace the shape of the graphic you want to and it produces a .json file. After that you need to create a class that loads this .json file. This project hasn't had updates in the last 6 years though (this is a major drawback) and because java keeps updating you have to find the loader from here. If you want to create big platform games though or games with joints and more advanced collision then i think that this is not the tool for you.

- Physics Editor. This is a tool that you have to pay for (it's not that expensive though). I have never used it so i don't know how exactly it works or if it is useful but if i remember correctly i heard that the guys who created the famous Badland game used it.

- R.u.b.e. I haven't used this one either. It is a tool that you have to pay for but i have heard that it is very useful and you can even create whole levels, export them and then with a loader, load them into your game.

The choice is all yours! I am sure that there are many other editors which i don't know of, so it would be great if you made a search yourselves. 

That was it for today's tutorial. Next time i will show you how to add Admob ads in your game and this will be the final tutorial of these series.

Feel free to ask any questions in the comments below!

Comments