【问题标题】:Procedural Island Generation confusion程序岛生成混乱
【发布时间】:2016-01-02 18:33:17
【问题描述】:

所以我一直在为一种生存 RTS 游戏开发 terragen。到目前为止,我一直在使用径向岛生成(基本上它创建了一个有点圆形的岛形状)。我觉得这很无趣,所以开发了一个分形岛生成器,它可以生成(无论如何我相信)更好、更多样化的岛形状。

以下是我用这种方法制作的几个岛屿:

它的工作原理是制作一个四边形并递归地细分,直到达到所需的详细程度。 要创建岛覆盖,我需要用白色填充这个轮廓。我最初的计划就像一个油漆桶工具,由像素操作。我发现这对我来说太慢了,所以开发了一种线相交方法。这种方法的问题是我无法弄清楚我哪里出错了。

每当水平线与形状相交时,它通过反转水平线中像素的颜色来操作(或至少意味着)。因此,它填充了形状。

问题的示例图片:

def invertFill(shape, res):
invertMap = numpy.zeros((res,res), dtype=bool)
#loop through outline vectors, find intersect points
for n in range(len(shape)):#loop through outline vectors
    if n == len(shape) - 1:
        nPlus = 0
    else:
        nPlus = n + 1
    sta = shape[n]
    fin = shape[nPlus]
    try:
        loopRange = getRange(sta[1], fin[1])
        for y in range(loopRange[0], loopRange[1]):#loop through y values in each vector
            if inRange(sta[1], fin[1], y) == True:#if y value is between start and finish of vector, find x coord
                xIntersect = (sta[1] - y) / (sta[1] - fin[1]) * (sta[0] - fin[0]) + sta[0]#intersect ratio multiplied against x dist between start and finish, added to x of start = x intersect
                invertMap[int(xIntersect)][y] = not invertMap[int(xIntersect)][y]#if a line intersects it, invert the intersect boolean (so if two lines intersect at that pixel, it stays false, three lines true etc)
    except:
        print("sta, fin = ", sta[1], fin[1])
#loop through pixels in y direction, then x, if pixel has invert property True, invert fill colour
map = numpy.zeros((res,res), dtype=numpy.uint8)
for y in range(res):
    colour = 0
    for x in range(res):
        if invertMap[x][y] == True:
            colour = 255 - colour
        map[x][y] = colour
return(map)

有人知道发生了什么吗?

【问题讨论】:

  • 打印出正在进行的结果的中间图像。看看他们开始出错的地方。插入更多打印语句以跟踪该操作。如有必要,将计算分解为中间部分并跟踪这些部分。
  • 好主意,谢谢。似乎是个不错的选择。

标签: python numpy procedural-generation


【解决方案1】:

我不确定我是否完全理解你想怎么做,因为我不熟悉 numpy,但我认为像这样更简单的解决方案可以解决问题:

def invert_colors(map):
    inside = False
    for x in range(len(_map)):
        for y in range(len(_map[0])):
            if map[x][y].color == 255:
                inside = not inside
                continue
            map[x][y].color = 255 if inside else 0

这个输入:

map = [
    [0, 0  , 0  , 0  , 0  ],
    [0, 255, 255, 255, 255],
    [0, 255, 0  , 0  , 255],
    [0, 255, 0  , 0  , 255],
    [0, 255, 255, 255, 255]
      ]

产生这个输出:

  [
[0, 0  , 0  , 0  , 0  ],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255],
[0, 255, 255, 255, 255]
  ]

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多