Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D tile map generation

I'm developing a 2D tile engine and at this moment I'm working on map generation algorithms.

I tried the basic ones usually involved in simple heightmap generation like

  • hill generation
  • perlin noise
  • diamond square

but I always get the same problem: this kind of algorithms seems suitable when dealing with tile maps that also have a height component but this is not my case.

I basically have sprites like grass, sea, desert and so on but they shouldn't be placed inside the map according to a generated height but something like

  • everything starts from ocean
  • islands are placed in the middle of the map (this is where algorithms that I tried failed mostly)
  • desert are generated (they should be like random spots around)
  • mountain and hills chains are spawned (they should be like snakes)

What kind of approach should I try?

I solved the subcomponent problems (like deserts, hills and mountains) by developing specialized algorithms to do what I needed (for example mountain starts from a point and then follow a direction with a chance of turning) but I'm failing with the generation of the basic islands (which could be customizable to be just a pangea or many degrees of size).

Just to give you a practical idea what I'm looking for is something like the civilization algorithm:

alt text

like image 218
Jack Avatar asked Jan 16 '11 00:01

Jack


People also ask

What is procedural map generation?

In computing, procedural generation is a method of creating data algorithmically as opposed to manually, typically through a combination of human-generated assets and algorithms coupled with computer-generated randomness and processing power. In computer graphics, it is commonly used to create textures and 3D models.

How do I make tiles in unity?

Create a Tile directly from the Assets menu by going to Assets > Create > Tile. You will be prompted to name and save the new blank Tile Asset to your chosen location. Refer to the documentation on Tile Assets for more information about the Asset properties. Creating a new Tile Asset.

What is a tile map file?

A tiled web map, slippy map (in OpenStreetMap terminology) or tile map is a map displayed in a web browser by seamlessly joining dozens of individually requested image or vector data files.


2 Answers

I used an approach which others have referred to as using "ants" for creating the random terrain. A description of the approach used:

First, I generated a tilemap by using a twodimensional rectangular array (x,y) of a specialized tile class. The tile class holds tile related information such as drawpoint, and terrain type.

Then I created a special "ant" class, which can be thought of as an invisible entity that takes "steps" around the tilemap. Every time the ant moves to a new tile, the underlying terrain type is changed. The ant can move in 8 directions and it changes direction, every time it has moved one tile. The direction it takes after each step is random.

I spawn either a fixed or random amount of ants with a fixed or random amount of lifetime. Lifetime is the amount of tiles it can traverse / step to before being removed.

To be able to control what kind of terrain is most common, I create an "terrain type" array. This array contains a list of the terrain types (basically just an int). To get equal balance between all types of terrain, you add only one of each terrain type to the terrain type array. If you wanted a certain terrain type to be more common, you would add further entries to the array, with that particular terrain type.

Then when the ant needs to determine what terrain to change, you do a lookup in the terrain type array using a random integer as the array index.

It takes a bit of tweaking the parameters (ant amount, ant lifetime, terrain type array), but I'm achieving some really good terrains so far.

It could be further enhanced by using more sophisticated types of ant classes, that for example traversed in specialized patterns etc. For making believeable islands in a ocean, you'd probably want to modify the ant behavior so they have some constraints in terms of which way to move (so you don't randomly get long "spikes" of land , very dispersed small islands etc).

The below is an example tilemap of a forest, that's generated procedurally by a little app I made using the ant approach. Hope this can set you on your way!

You can get the source of the app (VB.NET) at Github

Procedurally generated 2D tile map of a forest section

like image 67
Morten Kirsbo Avatar answered Sep 22 '22 16:09

Morten Kirsbo


[hill generation, perlin, diamond square] ... this kind of algorithms seems suitable when dealing with tile maps that also have a height component but this is not my case.

But it is your case. Mountains are higher than plains, and plains are higher than water.

                        ___/                     ___/ ___ Mountain cutoff                 ___/          ______/     ____/ ___ Water cutoff __/ 

You can quantize your data so that if it is between one set of levels, it is counted as one type of tile, whereas when it is in a different range, it is a different type of tile. You'll be throwing out some detail, but you'll still get outlines that match the type of noise that you're generating.

It'll probably take a good amount of tweaking, and will require you to generate other land features (besides impassible mountains) yourself, but you'll have to tweak a lot with any content generation solution.

like image 41
Merlyn Morgan-Graham Avatar answered Sep 20 '22 16:09

Merlyn Morgan-Graham