Procedurally generated 2d terrain?

Talk about anything else you want

Moderator: BigEvilCorporation

Post Reply
djcouchycouch
Very interested
Posts: 710
Joined: Sat Feb 18, 2012 2:44 am

Procedurally generated 2d terrain?

Post by djcouchycouch » Sat Nov 24, 2012 2:08 pm

Crazy ideas time.

Any ideas on how to generate fast and decent 2d tile terrain in real time? The game would generate new columns or rows depending on the direction the player is scrolling in.

If say, Goplanes had wide areas between cities or towns or whatever, then procedurally generated terrain would save me from building those maps by hand. And save memory as well. Of course the generation would have to be deterministic. The same seed value would generate the same terrain.

DJCC

ammianus
Very interested
Posts: 124
Joined: Sun Jan 29, 2012 2:10 pm
Location: North America
Contact:

Post by ammianus » Sun Nov 25, 2012 1:50 pm

I'll go out on a limb here and say that if you wanted basic terrain that could be just a simple loop that draws a pattern of tiles?

This is what I was doing in early versions of my 32x demo see this thread

So to draw a floor made of a single tile that was repeating was something like this:

Code: Select all

      for(background_tile_x = 0;background_tile_x < MAP_SCREEN_WIDTH; background_tile_x++)
      {
         for(background_tile_y = 10;background_tile_y < MAP_SCREEN_HEIGHT; background_tile_y++)
         {
            //where map_tiles[0] contains my 16x16 tile
            drawSprite(map_tiles[0],...);
         }
      } 
From the above it could be more complex putting in additional logic as needed in the loops. E.g. this puts different tiles depending on which row of the background. This is just simple stuff, you could certainly make the terrain more complex with some additional if statements to control hills and valleys etc..

Code: Select all

     //for each row
      for(background_tile_y = 8;background_tile_y < MAP_SCREEN_HEIGHT; background_tile_y++)
      {
			switch(background_tile_y){
				case 8:
					tile_select = 2;
					break;
				case 9:
					tile_select = 1;
					break;
				default:
					tile_select = 0;
					break;
			}
    //for each column in row
         for(background_tile_x = 0;background_tile_x < MAP_SCREEN_WIDTH; background_tile_x++)
         {
            
            //where map_tiles[tile_select ] contains my 16x16 tile
            drawSprite(map_tiles[tile_select ],...);
         }
      } 
There would need to be some kind of other counter that tracks how long you are in this looping terrain so you could eventually break out of it.

What are your requirements for the terrain? How complex would it need to be?

haroldoop
Very interested
Posts: 160
Joined: Sun Apr 29, 2007 10:04 pm
Location: Belo Horizonte, MG, Brazil

Post by haroldoop » Sun Nov 25, 2012 9:56 pm

This page presents some map generation algorithms for roguelike games:
http://roguebasin.roguelikedevelopment. ... e=Articles
I'm not entirely sure if any of those would be useful in your case, though.

djcouchycouch
Very interested
Posts: 710
Joined: Sat Feb 18, 2012 2:44 am

Post by djcouchycouch » Sun Nov 25, 2012 10:57 pm

I should've originally posted an example of what I had in mind, so here's the general idea:

Image

Warning, not a math guy:

If you ignore the large floating rock chunks, the mountain range could be represented by some kind of formula. (really simplest case, a sine wave) Using X position as a parameter, it would give a height on Y. Then everything over Y is sky and everything below is ground. The formula would be set up so that it creates a decent looking mountain range.

For the floating rock chunks to be generated randomly, I have no idea.

An alternate idea but not exactly what I have in mind is to pre-create a bunch of sections that look like a mountain and stitch them together according to a pattern.

haroldoop
Very interested
Posts: 160
Joined: Sun Apr 29, 2007 10:04 pm
Location: Belo Horizonte, MG, Brazil

Post by haroldoop » Sun Nov 25, 2012 11:25 pm

Maybe you could use some variant of the plasma fractal, a.k.a. diamond-square algorithm:
http://library.thinkquest.org/26242/ful ... /ch10.html
http://en.wikipedia.org/wiki/Diamond-square_algorithm

Image
Image

This algorithm is often used to make random mountains, forests, oceans, etc.

One possible way to do it would be to use the 1D variant for the montain ranges, and the 2D variant for the floating rocks.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Sun Nov 25, 2012 11:28 pm

I would make a bunch of large blocks of various terrain and then use RNG to create some kind of arrangement of them
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

djcouchycouch
Very interested
Posts: 710
Joined: Sat Feb 18, 2012 2:44 am

Post by djcouchycouch » Tue Nov 27, 2012 1:10 pm

TmEE co.(TM) wrote:I would make a bunch of large blocks of various terrain and then use RNG to create some kind of arrangement of them
That's probably the easiest route to take.

Post Reply