Creating Android Game From Scratch With LibGDX(box2d)

  

The industry of mobile gaming grows every year. According to Newzoo, the revenue from mobile games in 2018 is estimated at $57.9 billion, so no wonder more and more people are attracted by the idea of developing their own app. Luckily, modern technologies offer a ton of ways to do that. In today’s article, we’ll show you how to create an Android game using a simple yet robust library called libGDX(box2d).

With libGDX(box2d), you can also develop games for other platforms, including Web, iOS, Windows, Linux, MacOS, and even Blackberry, not just Android. The code is cross-platform, so you don’t need to write several apps. You can create one and it will run across all the platforms, which makes libGDX(box2d) a huge time-saver.

As for the technical side, libGDX(box2d) consists of several modules that can provide you with such cool features as the ability to manage light behavior, create AI-based game characters, use different input types, etc. However, there is more. Thanks to its high performance, you can also create 3D games using libGDX(box2d).

That said, let’s consider the main steps of developing a 2D game using libGDX(box2d).

(At the end of this article, we’ve also included a list of links that should help you getting started with the library).

STEP 0: Install libGDX

First, you need to create a project. Download and run the SetupApp, which is available by the following link:

https://libgdx.badlogicgames.com/download.html

Here, you can choose the platforms and libraries your game will support. For this specific project, however, you’ll need Box2d:

After creating the project, open it and go to the Core module in the main application class. Delete all the unnecessary resources and objects to have the same code as follows:

STEP 1: Set and configure camera

To create a 2D game, you need to create a camera with the orthographic projection fitting to the size of the device screen. You need to introduce the scaling factor SCALE for the possible mass scale of the camera.

For display on the screen, you’re going to use pixels per meter since Box2D works with meters. In this case, the number of pixels should be set to 32. Thus, for one block (meter), there will be 32 pixels.

The result should be a white screen:

STEP 2: Create World and add Player

Next, you need to create a word object in which you’ll set the gravity vector:

After that, create the player object.

To see the boundaries of the Player object, add the Box2DDebugRenderer to the MyGdxGame class, which will render to the Body objects.

In the Update method, update the world object

When that’s done, tie the camera to the Player’s coordinates

This is how full content of the MyGdxGame class should look like:

Next, you need to create the Player class. Create BodyDef and declare its position and behavior DynamicBody:

Create a PolygonShape, a square shape and with the specified size:

When creating Player, you transfer the World object into it:

Also, you need to specify the path to the image and the initial position:

Here is how the full content of the Player class should look like:

As a result, you should see a red square on the screen:

STEP 3: Add textures

Now it’s time to add some textures to the game. Add the picture cat.png for the player in the assets folder:

In the Player class, specify the path to the texture; the path to the image is relative to the Assets folder:

Create SpriteBatch, Texture objects:

In the render () method, you need to draw the textures:

Let’s change the color of the screen to a more pleasant one:

For Batch, set up the projection matrix:

Here is how the full content of the MyGdxGame class should look like:

As a result, you should see a game object with a bound texture:

STEP 4: Create game map with Tiled

To create a map, you need a tile set. You can find it or create it yourself in a graphic editor. Also, you need Tiled to create a map from tiles. It’s available via the following link:

http://www.mapeditor.org/download.html

After you’ve created a new map, you need to draw separate layers:

  • tiles — a layer of textures;
  • bounds — a layer of lines, the boundaries of the world;
  • ground — a layer of lines, an object from which you can repulse;
  • dangers — a layer of lines, dangerous areas which inflict damage upon contact.

As a result, you should have a file with the GameMap.tmx map, a file with tiles Tiles2d.tsx, and a file where the tiles are stored — Tiles2d.png

To load the map, create the MapParser class that will create an object for each line containing Body (Ground, Bounds, DangerZone) and add it to World:

Now, the Bounds Class:

Ground Class:

DangerZone Class:

In the MyGdxGame class, you need to specify the path to the file with the map:

In the onCreate () method, create:

Add to the update () method:

Add to the render() method:

Do not forget to add to the dispose () method:

To disable the drawing of the boundaries of objects, remove:

Here is how the full code of MyGdxGame class should look like after that:

As a result, you should get the rendered level with the game character:

STEP 5: Handle input

Let’s create the inputUpdate() method, in which depending on the point of touch of the screen the game character will jump or move over the surface. For bouncing, the Player’s applyForceToCenter() method will be called. For horizontal movement, you need to call setLinearVelocity() :

Call intputUpdate () in the update () method

To track the touch with surfaces in the onCreate () method, set WorldContactListener:

Create WorldContactListener:

To store the jump and life state in Player, you need to add:

Go to the input method. There, you need to create the PlayerUpdate () method to check whether the game object is still alive and, in the opposite case, delete it from the World and create a new object Player:

libGDX documentation;

libGDX wiki;

libGDX on GitHub;

Conclusion

In this post, we’ve showed you how to develop a 2D game for Android using libGDX(box2d). As you can see, even such a complex task as creating a mobile game can be relatively easy; you just need to break it down into smaller steps.

For now, we’ve only used this library to develop 2D games, but maybe some of you have already had experience in making three dimensional apps with libGDX(box2d)?

Post a Comment

أحدث أقدم