Generating 2D Terrain: In Depth

Looking back at what I previously wrote when documenting the development of my game, currently only known as project Dunia, I realized that I barely went into detail into the actual process that I went about when generating the terrain. In order to help some of you actually create the terrain, I will go into more detail about how I did it and how you could replicate it.

Generating the actual terrain

There are plenty of methods that are used when generating terrains and many algorithms that you can use. I would recommend using either Perlin noise or simplex noise as there seems to be a ‘port’ of them for most languages. GameMaker users such as me can find Perlin noise or simplex noise on the old GameMaker Forums or on the GameMaker Marketplace. In my project, I used 2D simplex noise (note 3D simplex noise is covered by a patent).

You will need to set a fixed width and height for your terrain. Generally, you could use 2D simplex noise as a primarily 2D function that gave you a value that you would put against a threshold in order to determine whether or not a tile would be there. In my scenario, however, I decided to use it as a 1D function whereby I would be theoretically producing a heightmap. The y coordinate of the point in question would then be compared to the height of the terrain at its x-position.

In order to get features such as overhangs, this method isn’t enough. It would produce a generally flat terrain. What you will be required to do in order to have overhangs is to distort the x-value of the coordinate using another simplex noise fractal. For example, if we were at point (1,1) on the map and the height our fractal states the terrain is at x = 1 is 10, we could distort the x-value using a fractal so it would appear that we are checking for a value at x = 2 rather than 1 which would give us a different height to check against the y coordinate that we have. This gives us the possibility of having varied terrain but be careful about the max factor that you can distort in the x-value as larger values can create holes in your terrain and annoying floating islands.

 

GameMaker_ Studio 15_09_2016 07_35_06
Your first terrain is likely to look really plain

 

Caves

Previously, I discussed how I used cellular automaton to produce the caves in my game. This became a frustrating endeavor since the caves weren’t shaped with any form of control which left parts such as cave openings too large at times or caves that felt too hollow. After discussing on cave generation methods with MishMash from the YoYoGames Forums (Seriously check out his team’s game Vitality RPG), I settled on using a modified form of drunkard’s walk.

How this works in my game is that we create a temporary array of the map and set the whole array to 0. We then use a 2d simplex noise fractal along the x-coordinate (we ignore the y-coordinate just as we did when generating a height map). The value that that function returns is compared to a threshold and if it exceeds the threshold, we can start building a cave.

Each cave technically starts at the absolute top of the array but they only become ‘visible’ once in contact with the terrain. Here we start carving the terrain. Each step, we set a radius of values around where our current pointer is at in the array to a value that you can recognize as a cave (it is up to you when drawing the cave to distinguish which values stand for caves and which values stand for normal terrain). At the end of each step, we move downwards by the same radius we used above whilst also arbitrarily moving either to the left, right or staying put. We repeat this process every time we generate a new cave and the results are much better than before.

It’s hard to see from the pictures above (upper pictures are old, lower pictures are new), the caves have more of a controlled form with the new method I used to generate caves. This is crucial for my project as some of the caves that I will be generating will be required to have entrances to dungeons in them. It’s a lot more easier to place a door or an item in a structure that despite being random, you still have full control over.

What am I up to now?

Well progress has really been at a halt for this project and I hate to be fulfilling the stereotype of overambitious indie developers but school plus a general lack of motivation has kept me at bay for quite too long. Regardless, I am back to developing this and I’m currently learning 2d procedural animation so I hope I can share something with you soon.

Please leave a comment!

old

For us content creators, we really appreciate it when we find out that we aren’t just shouting out into a void and that we have given you some food for thought so please don’t hesitate to comment (including criticism). How would you approach this? Did this help you at all?

3 thoughts on “Generating 2D Terrain: In Depth

Leave a comment