2015-10-06 98 views
2

所以我一直在研究一种生存RTS游戏的terragen。到目前为止,我一直在使用径向岛生成(基本上它创建了一个有点圆形的岛形状)。我发现这是无趣的,所以开发了一种分形岛发生器,生成(我相信,更好和更多样化的岛屿形状)。Procedural Island Generation confusion

下面是使用这种方法我打了几个岛屿:

enter image description here

它通过使四边形,直到达到细节的要求的水平递归细分。 要创建岛状覆盖图,我需要用白色填充此轮廓。我最初的计划像一个油漆桶工具,并从像素操作。我发现这对我的喜好来说太慢了,所以开发了一种线交叉方法。这种方法的问题是,我无法弄清楚我出错的地方。

它的工作(或至少是指)由每一个行与形状相交时间反转的像素的颜色在一个水平线上。因此它填补了形状。

问题的示例图片:

enter image description here

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) 

任何机会,任何人有一个线索,这是怎么回事?

+0

打印出您的结果进行的中间图像。看看他们开始出错的地方。插入更多的打印语句来跟踪该操作。将计算分解成中间部分并在必要时跟踪这些部分。 – Prune

+0

好主意,谢谢。似乎是一个很好的选择。 –

回答

0

我不知道我完全理解你想如何做到这一点,因为我不熟悉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] 
    ]