Mars Project : Map Generation
Introduction
In order to develop my city-builder, I decided to start with one of the most fundamental yet complex elements : the map. Most elements rely on it, and because of its complexity, I wanted to be done with it at the very beginning of my project. I thought about what kind of map I wanted. Do I want to build it myself ? Not really, because no matter how many map I build, they wouldn't change and the players would get bored after a finite number of games because the maps would always be the same. Do I want a real map of Mars ? Definitly not. That would require too much work and resources that I don't have as a student. Do I want a randomly generated map ? Randomness is good for replayability but not so much for immersion or realism. A map should have the same feels as the universe it is set in. Mars isn't random, its topographic map isn't either. But that doesn't mean I should ignore randomness.
Having the same map over and over again would be boring so I still wanted a bit of randomness. So I started looking for procedural map generation : based on randomness, but following a set of defined rules. Many games have used this approach to draw a line between randomness and arbritary choices (Dead Cells for example). That's when I discovered Perlin Noise.
Algorithm
Perlin Noise is a type of gradient noise, and was created for use in Computer-Generated Imagery (CGI) by Ken Perlin. In this context, the word noise refers to the introduction of perturbation on a image/surface to produce natural appearing textures. The main goal is to create a more natural look than what true randomness can create, even if it's not directly linked to image generation. Its functionnement is rather simple. Given two coordonates (width and height, or X and Y for example), the algorithm will give you a number between 0 and 1. It's then up to you to interpret this number depending on your goal. In the case of the two images below, the black parts represent a value of 1 and the white parts represent a value of 0.
This first picture is what a truly random algorithm can create. While it does look random, it does not resemble a topographic map at all.
This second picture is what the Perlin Noise Algorithm creates, and this is what I based my map generation on.
Implementation
To utilize Perlin Noise in my map generation, I loop through every tile of my isometric grid. Then, for each tile, I send its coordonates to the Perlin Noise function (included in C#, the language used for scripting in Unity). Depending on the value returned by the function, my algorithm assigns different sprites to create an illusion of elevation/altitude. I did not have a satisfying result at first but after some tweaking, I achieved a result that looks like this :
The advantage of this algorithm is my ability to tweak it easily. I could for example decide to have more amplitude between the low and high points, or a higher frequency between the moutains/plains changes.
posté par adrienmelia,