2D瓦片地图生成
对于 2D 图块引擎,我正在研究地图生成算法。我尝试了高度图生成:
- 山丘生成
- 柏林噪声
- 钻石方形
适合具有高度分量的平铺地图,但我有像草,海,沙漠这样的精灵,它们应该放置如下:
- 从海洋
- 岛屿开始的所有东西都放置在中间地图(这是我尝试的算法大多失败的地方)
- 生成沙漠(它们应该像周围的随机点)
- 生成山脉和山链(它们应该像蛇)
我应该尝试什么方法?
我解决了沙漠、丘陵和山脉(例如山脉从一个点开始,然后沿着一个有转弯机会的方向),但我在生成岛屿时失败了(应该可定制为盘古大陆或许多程度的大小) )。
我正在寻找类似文明算法的东西:
For a 2D tile engine I'm working on map generation algorithms. I tried heightmap generation :
- hill generation
- Perlin noise
- Diamond-square
Suitable for tile maps that have a height component but I have sprites like grass, sea, desert and they should be placed like :
- everything starts from ocean
- islands are placed in the middle of the map (this is where algorithms that I tried failed mostly)
- deserts are generated (they should be like random spots around)
- mountain and hill chains are spawned (they should be like snakes)
What approach should I try?
I solved deserts, hills and mountains (for example mountain starts from a point then follows a direction with a chance of turning) but I'm failing with the generation of islands (which should be customizable to be just a pangea or many degrees of size).
I'm looking for something like the Civilization algorithm:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但这是你的情况。山高于平原,平原高于水。
您可以量化数据,以便如果它位于一组级别之间,则将其视为一种图块类型,而当其位于不同范围内时,则将其视为不同类型的图块。您将丢弃一些细节,但您仍然会得到与您生成的噪声类型相匹配的轮廓。
它可能需要大量的调整,并且需要您自己生成其他陆地特征(除了不可逾越的山脉),但您必须对任何内容生成解决方案进行大量调整。
But it is your case. Mountains are higher than plains, and plains are higher than water.
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.
我使用了一种被其他人称为使用“蚂蚁”来创建随机地形的方法。所用方法的描述:
首先,我使用专用图块类的二维矩形数组 (x,y) 生成图块地图。图块类保存与图块相关的信息,例如绘图点和地形类型。
然后我创建了一个特殊的“ant”类,它可以被认为是一个不可见的实体,在瓦片地图周围“行走”。每当蚂蚁移动到新的图块时,底层的地形类型就会发生变化。蚂蚁可以向 8 个方向移动,并且每移动一格就会改变方向。每一步之后的方向是随机的。
我生成固定或随机数量的蚂蚁,它们的寿命是固定或随机的。生命周期是指它在被移除之前可以遍历/步进的瓷砖数量。
为了能够控制最常见的地形类型,我创建了一个“地形类型”数组。该数组包含地形类型列表(基本上只是一个 int)。为了在所有类型的地形之间获得平等的平衡,您只需将每种地形类型之一添加到地形类型数组中。如果您希望某种地形类型更常见,您可以使用该特定地形类型向数组添加更多条目。
然后,当蚂蚁需要确定要更改的地形时,您可以使用随机整数作为数组索引在地形类型数组中进行查找。
这需要对参数(蚂蚁数量、蚂蚁寿命、地形类型数组)进行一些调整,但到目前为止我已经实现了一些非常好的地形。
它可以通过使用更复杂类型的蚂蚁类来进一步增强,例如以专门的模式遍历等。为了在海洋中创建可信的岛屿,您可能需要修改蚂蚁的行为,以便它们在以下方面有一些限制:移动方式(这样你就不会随机得到长长的“尖刺”土地、非常分散的小岛屿等)。
下面是一个森林图块地图的示例,它是由我使用 ant 方法制作的一个小应用程序生成的。希望这可以帮助您走上自己的道路!
您可以在 Github
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