This year isn’t really about traveling or doing anything outside our homes, isn’t it? These times we rely on various ways to relax and have some time alone or maybe with the family. A good videogame can bring you far away, you can walk through forests, deserts, even worlds out of your imagination.
We can differentiate two kind of worlds from gaming perspective:
- pre-built worlds where the artists and designers do their best to create the landscape, the scene which tells you a story
- generated worlds where the game itself generates the landscape, vegetation, buildings to the game world
Lately, I’m working on creating a generator that handles the creation of the landscape and makes it alive by spawning entities like vegetation, animals, ruins, etc. I think the process can be interesting to anyone who was thinking about how these games are doing their magic.
There are a couple of key aspects the game need to implement:
- the world can be generated (pseudo-)randomly
- generation can be customized using input parameters
- for the same input parameter, the generator should always create the same result
For this task, the common way is to use a noise algorithm, and for terrains, the most widely used method is to use Perlin-noise or some modified version of it. As I’m not a math guy I’m lucky that there’s a lot of implemented versions of it, Unity even has one by default. It’s pretty much easy to create some alterations using parameters to create very different results. Let’s just say you want a smoother surface which can act as a mountain-valley kind of terrain: let’s scale up the noise map and voila you got the result you want:
The noise map is basically a grayscale image where white means 1, black means -1. Using these values we can calculate the “height” of a given coordinate and apply it on the terrain mesh. This gives us good control over how our terrain looks like but also we can go further. If we are using an output function which let’s say calculates the square root of the given height value we’ll have a different result, but also different height boundaries. It’s experiment time, everyone can create their own landmass which is awesome. But what if we still want to go further, maybe add some more detail? Little bumps to spice up things a little bit maybe? We can add another layer to our result by using multiple noise maps so we can create as detailed surfaces as we want, which is a wonderful start on the journey of creating a playable area.
Noise maps are also good from another perspective as well: they can be scrolled in both X and Y dimensions therefore if the character moves from its starting position we can generate as many areas as we need, the new chunks will always be connecting to the old ones seamlessly.
Now that we have this the next step is to spawn some vegetation, use various biomes, etc. This way we can create an almost infinite world that is alive and can be always interesting for the player. But that’s for another day. 🙂