2D tile map generation

后端 未结 2 1593
时光说笑
时光说笑 2021-01-30 04:32

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 lik

2条回答
  •  一生所求
    2021-01-30 04:57

    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

提交回复
热议问题