Full LibGDX Game Tutorial – Box2D

Box2d Example Output

Sharing is caring!

Full LibGDX Game Tutorial – Box2D

Welcome to part 3 of our Full LibGDX Game Tutorial. This part will focus on the Box2D basic, creating a world, stepping it through time and adding different types of bodies to it. If you haven’t seen the earlier parts of this tutorial I advise you to start at Full LibGDX Game Tutorial – Project setup as this tutorial continues from these earlier parts. For those of you who have come from part 2, you can continue on.

We’ve already made our MainScreen which will be the screen that displays our game. In addition to that, we need a model which will control all the logic in our game. So let’s create a new class called B2DModel and in this model, we will add a world, which, Box2D uses to keep all the physical objects in the game.

Since this is the logic part of our game we need a logic method which will run the logic part of the model. We will also add a world step that tells the Box2D world to move forward in time.

If we start our application now nothing will happen when we select “new game” as we need to tell the MainScreen to use our model. In our MainScreen add the following to the constructor:

And add this to the render method:

What we have just done is created a new model when the MainScreen is created, we then we create a camera which controls how much of the box world we see(32 units wide and 24 units tall). In Box2D all objects are based on a unit, which is 1 meter. When you create a physical object that is 1 wide and 1 tall you are making a 1m by 1m object. This helps keep the physics correct with the real world. Our screen will show a 32 meter high, 24 meter wide area of our map and this is good enough for now. We have also created a debugRenderer which is used to redner objects in a Box2D world without the need to add textures which we will use until our asset manager is added.

We also added a call to our model’s logicStep method. This will be used to move our game logic forward. Later we can use an if statement to disable running this logic to essentially pause our game. We then clear the screen and use the debugRenderer to render our world.

Now our MainScreen is ready we need to tell the model to create something in the Box2D world. This is done by creating a body. A body is a physical object in a Box2D world, it stores all the physics information such as it’s size, shape, position, speed, restitution etc. There are 3 types of bodies you can use in Box2D. They are DynamicBody, StaticBody and KinematicBody. A DynamicBody is affected by gravity and other bodies, a StaticBody is a body that isn’t affected by gravity or other bodies and doesn’t move and a KinematicBody is a body that isn’t affected by gravity or other bodies but can move.

So dynamic bodies will be used for players and enemies, static bodies used for the floors and walls and kinematic bodies for things like wheels or moving platforms. We will create one of each and add them to our world.

Dynamic Body

Create a new private method in our model class call createObject with the following code:

This private method can only be called in our model our screens or orchestrator class have no access to this method because it is private. It creates a BodyDef which is like the data for the physical object, it will hold information such as, type of body, location, speed, rotation etc. Next we add it to the world so the world knows about the object and can update it. We then create a FixtureDef which is the data for a physical body part. We could create a body with multiple parts like an ice cream has a cone and the ice cream part, we would want the cone to have a low density because it’s light whereas the ice cream should be denser as it is heavier. We then use the bodies createFixture method to add that fixture to the body. We finally dispose of the shape as it is no longer needed and we want our memory back.

Now we can work on our static body

The static body is almost identical to our dynamic body except we don’t need to define a density and we can just use the shape in our body’s createFixture method.

Finally, we can make our Kinematic Body

This is exactly the same as our dynamic code except we have set the linear velocity. Now all we need to do before we test our code is to call these private methods in our model class.

Your B2DModel code should look like this:

When you run your application your main screen should output somethign similar to this.

Box2d Example Output

We now have the first part of our game setup. We have create the project, created the 5 screens we will eventually use, added a menu system, created the model for our logic, added box2D components and added Box2D bodies.

Part 4 of our Full LibGDX game tutorial will go over moving the creating of bodies into its own class so we can keep the specifics of bodies in its own section.

← Preferences And Menu Contents Box2D Body Factory →

Sharing is caring!

One Reply to “Full LibGDX Game Tutorial – Box2D”

  1. When setting the fixture on each of the three bodies, I was a little confused regarding the purpose of the FixtureElement until I looked at the JavaDoc for Box2D, and I believe there may be an oversight in your example.

    For anyone going through the tutorial, there are two different methods to set a fixture on a body, you can either pass a shape and a density, or you can pass a FixtureElement. In the example, the fixtures for all three objects are set by passing the shape and the density. I think the intent was to add the fixture using the shape/density method when adding the floor, and then for the other two objects set the fixture by passing the FixtureElement.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.