Simple terrain generator experiment

Just great how easy it is to make a simple terrain generator :D

This evening I started an experiment to try and create a terrain generator (like the one in my all-time favourite SimCity2000). So far I don’t really have use for it but it might come in handy in the future.

I managed to work out the basics in just 3 to 4 hours. Now I need to find a way to get smoother edges and get rid of all the really small islands. I recon that’s gonna take a bit more work.

 I used an enterFrame event to get the animated effect. Just to see what happens.

Check the first results here:
http://labs.justgreat.nl/exp070702.htm

PS: this is the first time I used actual flash components since I don’t have a slider or spinner ready in as3; boy do those things SUCK!!! :roll:

UPDATE (2007-07-09):
I managed to remove the majority of the small (one or two px) islands by tweaking the algorithm a little. The result is now a bit more realistic.

Also I increased the max world size to 960×960 (to illustrate: SimCity2000 has a mapsize of 128×128).

Finally I added the world wrap option (because it took me just 2 minutes to add :D ).

UPDATE (2008-03-09):
Here is a short explanation of how the code works:

The general trick is to place a first pixel somewhere on the map and than check each of its neighbours to see if they need to be filled with the same “terrain-type”. You can do this by checking a random value against a fixed limit value (in this case represented by the “spread”).
When a certain pixel on the map is filled you place it in an array so you can always check if a pixel is already occupied and you can also use it to check if the maximum number of a certain type is reached (in this case represented by the “pct”).
After this whole sequence you repeat the same process for the newly generated pixels (there is your recursion). If there are no more pixels left to check and the maximum is not reached yet, you just place a new pixel somewhere on the map and start again.

I hope this will help you along.

6 Responses to “Simple terrain generator experiment”


  • Can we talk? Email me please.
    Can you show us our source code ?
    http://www.desgraff.com

  • I’m not just giving away my sourcecode.
    Do you have a particular part you are interested in?

    In general it’s just a recursion of pixels checking which of it’s neighbours should also turn into it’s type. The checking is done by chance, where the chance depends on the settings of the sliders…

  • don’t know where I found your examples – but’s the first time 4 me to follow links just to make a post in a blog: really nifty experiment! a few more lines on the algorythm would be fine – but i stil love it. thnx 4 inspiration!
    struschie

  • Cool arno,
    First thought it was a perlin calculation… when do we port it to bitmap 3D?
    fab

  • Hello Arno,
    The link http://labs.justgreat.nl/terraingenerator1.htm is not working.

    I too am trying to build a terrain using c language and graphics library.
    Can u please guide me by providing the initial steps and requirements.

  • Hi Harsh,

    Thanks for reporting the bad link. I fixed it and I updated the post with a little explanation of the genaration process. I hope it will help you along. When you have some results please let me know…

Leave a Reply