# 2D Terrain generation with controlled variations

Well, I'm trying to create a simple endless runner and -this may sound repetitive- I'm searching for ways to generate the terrain. The game will only have 3 types of ground tiles, ascending, descending and plain, and it should look (somewhat) like this:

So, what I want to say is that the terrain can only vary 1 unit of height.

I first thought of perlin noise, but I haven't found any information about restricting height variations. I've also thought about choosing randomly between the three posible tiles, but I still want that "natural randomness" that perlin noise can produce (very high hills, deep valleys, etc)

Any idea on how to procede?

Thanks! And please excuse my english.

You can still use Perlin noise. You simply need to constrain the random value to three values. Depending on how you're generating the noise, you'll get a value between -1 and 1, or 0 and 1. Whatever you have, just divide it by three, and place your tiles based on that.

float range = Perlin.max - Perlin.min
float value = Perlin.GetValue(xCoord)

if(value < range * (1/3))
placeDecending()
else if (value < range * (2/3))
placeFlat()
else
placeAccending()


Essentially you're just taking the range of random values you can get from Perlin noise, and splitting them into three categories:

• Less than 1/3
• Between 1/3 and 2/3
• Greater than 2/3

This gives you the benefit of gradient noise, while also constraining the output to three tile types.

• Awesome, thanks! I was thinking about making the perlin noise affect the probability of placing each tile (if it's too high, then ascending tiles will apear more often). Would I get more or less the same result?
– vdrg
Sep 8 '14 at 0:26
• It's another way of doing it. It's going to be a subjective decision about which one you like more, or gives better results. Try both and see what you like.
– House
Sep 8 '14 at 1:37

You can also set a target height to move towards, with smaller target height variations in between. Then you can have a sort of rolling hills effect where stuff still gets generally higher or lower as desired.

This is a great use case for recursion where you can tweak the parameters of each step to get different spacings.