Full LibGDX Game Tutorial – Infinite Level Generation with Simplex Noise

Simplex noise - example terrain output - colourized

Sharing is caring!

Full LibGDX Game Tutorial – Infinite Level Generation

Welcome to part 11 of our Full LibGDX Game Tutorial. This part will focus on creating a system to infinitely generate our world using Simplex Noise. 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 10, you can continue on.

In the last part of this tutorial we replaced our model with the Ashley ECS system, in this part we will replace the level generation code in the MainScreen and move it into a LevelFactory which will control adding all our world objects such as the platforms to stand on, the enemies and obstacles. Before we do that, we need to talk about noise.

What is noise?

Noise refers to various pseudo-random functions usually used to create textures. However, due to their ability to create seemingly random repeatable values they have become useful in generating random terrain for games. We will be using what’s known as the Simplex Noise to generate our world platforms. The image below was created using simplex noise:

Simplex noise - example terrain output - colourized

We will be using the below code to generate our simplex noise so add them to your project in their own package. The first part is this code to create a simplex noise octave ( a single layer of noise which will be added to other layers ).

The next part is for combining simplex noise octaves with other simplex noises octaves to create random values.

Creating a LevelFactory

Now that we have the Simplex noise code in our project we can star working on out LevelFactory which will generate platforms based on the values provided by the simplex noise.

In the LevelFactory code above you will see some familiar code, namely the create methods which have been moved out of the MainScreen and added here. In order to add those methods we have had to add the Box2D world creation and BodyFactory into the LevelFactory which is prefereable as only the LevelFactory needs to use the BodyFactory.

A new method generateLevel has been created which uses 4 separate layers of a 3D simplex noise. Noise 1 and 3 are used to check if a platform should be created. Then noise 2 and 4 are used to set the x position of those platforms.

Update MainScreen

Now we have our LevelFactory we need to use it in our MainScreen instead of the current system we have in place. So our MainScreen now looks like this:

The astute among you will notice the class now has a new Ashley system defined here “engine.addSystem(new LevelGenerationSystem(lvlFactory));” This is a new system we will be using to keep the LevelFactory updated with the player position. This could have been done in the player control system however one of the aims of Ashley ECS is to separate individual areas so they are easily changeable in the future which may not be the case if we start combining functions with each other for convenience.

So our new system is simply this:

So, What have we done in this part? Well, we have added the simplex noise code (SimplexNoise_octave and SimplexNoise) which allows us to create random values that will always be the same given the same seed value. We also added a LevelFactory which will allow us to create platforms dynamically as our player gets higher and higher. Finally, we added a new Ashley system and updated our MainScreen to plug our new code into our game.

As usual, the finished code for this part can be downloaded from StorMyVids here.

In The next part, Part 12 we will start adding some game logic to add a death mechanic and a score system.

← Entities and Ashley ECS Contents Game Mechanics →


Sharing is caring!

One Reply to “Full LibGDX Game Tutorial – Infinite Level Generation with Simplex Noise”

  1. “player.cam = cam”

    In the last tutorial, you left this class empty.
    Should I add an OrthoCamera class to the PlayerComponent class?

Leave a Reply

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