Building a physics world from level data
Physics for levels can be hardcoded in the source code with terrible consequences for maintenance purposes, but I am sure that you have already heard about the data-driven approach that suggests a solution.
Most level editors include mechanisms to attach physics data to the final level file and one of them, Tiled, is familiar to you from Chapter 8, User Interfaces with Scene2D. It supplies a layers system where you can define geometric shapes and some metadata for their properties.
As all the data is parsed by the map, you can create a generic physics populator for your games so the process of applying them to static bodies becomes almost automatic.
Getting ready
The juice of this recipe is concentrated into two Java source files: Box2DMapPopulatorSample.java
and MapBodyManager.java
. At the end, you will have a bunch of your game objects with their physics shapes properly attached on your screen.
How to do it…
Fortunately, you are a master in dealing...