Making bodies will be our first step into programming interactions and collisions in our game. To do this, you will need to use the Java library called Box2D. Using Box2D, you will be able to create objects that your code can detect, which allows you to handle collisions and physics.
World – A box that surrounds your whole game, everything in the box encounters physics.
Bodies – The properties of an object that you cannot see or touch. This includes mass, velocity, location, angles, etc. All bodies must be created within a World.
Fixtures – Defines the shape of the object and whether other objects can collide with it. This has the properties of shape, density, friction and restitution (bounciness).
Bodies and Fixtures must be used together to create objects that can be interacted with and moved around your map.
Remember those objects you made in Tiled while creating your Tilemap? Now it’s time to add them to your game.
First add a World variable to your Screen class.
Then initialize your World variable in your constructor. The first parameter (a Vector2 object) describes the gravity of your world. The second parameter describes whether your world does calculations for Bodies in your World that are currently not moving (or at rest). This saves a lot of processing time for your computer.
To create your bodies you need to first make a BodyDef object, which holds all the information about a specific Body. Then create a PolygonShape object which will describe the shape of the outline (or hitbox) of your Body. This PolygonShape can then be added to a FixtureDef object, which will create the outline. The last thing is to create your Body object, which is where all these other objects will be stored.
Then, check your Tilemap in tiled to see all your object layers. Each layer has an index corresponding to it as seen in the example below.
To reference each layer in your game, you should (if you haven’t already) make a new Constants class where you can store static integers and Strings. Add each layer’s index as a constant to this class.
For each object layer in your Tilemap, add a for loop like the one below.
What this for loop does is that it goes through each object in that layer and creates a Rectangle object. It then defines the Body‘s ‘Type’, sets the position and then creates a Body using a method in the World class. Then the fixture is also set to the Body in order to make the hitbox.
A Body can have 3 types:
For something like the ground or a tree, these Bodies would be StaticBodies. For something like a rock that the player can push, you should use a DynamicBody.
When games are played, usually the hitboxes are hidden, but for the sake of programming and finding bugs, it’s usually more helpful to be able to see your fixture outlines. To do this you can use a Box2DDebugRenderer object.
A Box2DDebugRenderer causes your fixtures to show an outline instead of being invisible as it would normally be. This will be useful for you while you are coding as it can help you find bugs. To add one to your code, create a Box2DDebugRenderer variable and initialize it in your Screen’s constructor. Then in the Screen’s render method, call the method below.
With that you should be able to see your fixture’s outlines around certain tiles!