• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# 2D artillery game terrain generation

## Recommended Posts

Much to my dismay, the forums are abundant with information on terrain generation.. in 3 dimensions. What about Scorched Earth? How can I just generate a simple, one-pixel color 2D terrain with decent-looking slopes (as opposed to random, spiky heights of pixels). I am a complete beginner in this respect, so please don''t refer me to something like Worms or Liero which are much too complicated a starting place for me.

##### Share on other sites
fractoid    703
Three ideas:

1. Generate white noise and smooth it somewhat. This will give you smooth rolling curvy hills.

2. Perlin noise.

3. Random midpoint displacement.

Basically anything you can use for a 3D landscape you can use for a 2D landscape, just cut a slice out of the 3D landscape.

##### Share on other sites
cozman    583
2 years ago I played around with this during a CS class, I started rather simple and by adding small enhancements, ended up with a technique which worked very well.

pseudocode:

min : min height
max : max height
slope : maximum slope
smoothness : low values = high smoothness
sign : -1 or 1
width : width of terrain, if on pixel by pixel basis, width of screen
terrainHeight[width] : array of heights
rand(low,high) : return random # between low and high

terrainHeight[0] = rand(min,max)

start sign at either +1 or -1, depending on if height is closer to max or min

for i=1 to height:
terrainHeight [ i] = terrainHeight[i-1] + rand(0,slope)*sign;
if rand(0,smooth) = 0 switch sign

You also want to change sign if you go above maxheight or below minheight.

That's a basic outline, I later added in a second pass, that'd get rid of anything that was "out of place"

[edited by - cozman on March 22, 2004 9:25:38 PM]

##### Share on other sites
Here''s what I ended up going with..

1) Divide the field into some number of columns such that each column has at least 2 pixels in it.

2) Place a random value between HIGH and LOW at the left side of the field. Call it A.

3) Generate a random degree from +-DEGREE

4) Find point B (from A) using that slope, and draw a line between them, and fill in all values below that line with "ground."

5) Repeat the process using B as a starting point until the right side of the screen is reached.

DEGREE is a value between 0 and 90 that determines the avaliable ranges of slopes, so I can choose how steep each point -can- be, though I don''t have a minimum, and I don''t have any way of biasing towards the last slope, either.

HIGH and LOW are percentages of the height of the screen, so if I have multiple types of terrain (different layers of dirt and stone for example), I can ensure that each layer is at least as high as LOW, but no higher than HIGH.

It works fairly well, but I find that there aren''t many mountains in my terrain, it just seems to move roughly up and down.. so it needs a bit of tweaking, but I think it''s a nice solution considering I''ve never done this before.. =P

Thanks for the replies.