# Procedural generation of jagged peaks

I would like to generate random cave type backgrounds similar to the one shown below.

I doubt anything I generate will look as good as the image, but something with that feel of sharp, jagged peak should be possible.

I don't have any experience in procedural generation and as such have no idea where to start....

Note: I am using libGdx on Android

• Minecraft uses 3D perlin noise to generate caves and stuff. Did you look into that? Maybe you can apply something similar to that to 2D to get the effect you want. Mar 22, 2016 at 3:43

One easy way to do it would be to use the midpoint displacement algorithm:

2. At the midpoint, displace it some random amount leaving the end points where they are.
3. Now take the resulting 2 line segments and do it to each of those (divide them in half and displace their centers by a random amount).
4. Continue doing this until you have a shape you like. Do the same for the floor.
5. Repeat the process for the floor, but displace upwards instead of downwards.

To get sharper peaks & valleys, you need to favor high & low numbers while suppressing mid-range numbers. For instance, assume some function rnd() returns a uniform random number in the range [0-1]. Then (sin(rnd()*PI)+1)/2.0 would skew that result to favor values closer to 0 or 1. Other mathematical transformations that would work, this one happens to be easy to prototype in a spreadsheet. If you need to do this in real-time, you might want something faster than sin().