我正在尝试实现一个地下城生成算法(
presented here和
demo-ed here),该算法涉及生成彼此重叠的随机数量的单元格.然后将细胞推开/分离然后连接.现在,原始海报/作者描述他正在使用分离指导算法,以便在一个区域上均匀分布细胞.我对植绒算法和/或分离转向行为没有太多经验,因此我转向谷歌进行解释(并找到了
this).我的实现(基于上次提到的文章)如下:
function pdg:_computeSeparation(_agent) local neighbours = 0 local rtWidth = #self._rooms local v = { x = self._rooms[_agent].startX,y = self._rooms[_agent].startY,--veLocity = 1,} for i = 1,rtWidth do if _agent ~= i then local distance = math.dist(self._rooms[_agent].startX,self._rooms[_agent].startY,self._rooms[i].startX,self._rooms[i].startY) if distance < 12 then --print("Separating agent: ".._agent.." from agent: "..i.."") v.x = (v.x + self._rooms[_agent].startX - self._rooms[i].startX) * distance v.y = (v.y + self._rooms[_agent].startY - self._rooms[i].startY) * distance neighbours = neighbours + 1 end end end if neighbours == 0 then return v else v.x = v.x / neighbours v.y = v.y / neighbours v.x = v.x * -1 v.y = v.y * -1 pdg:_normalize(v,1) return v end end
self._rooms是一个表,其中包含网格中房间的原始X和Y位置,以及它的宽度和高度(endX,endY).
问题在于,它不是将细胞整齐地排列在网格上,而是将重叠的细胞移动到一个从1,1到距离2的区域,距离2(as seen in my video [youtube])
我试图理解为什么会这样.
如果需要,我在这里解析网格表,分离并填充分离后的单元格:
function pdg:separate( ) if #self._rooms > 0 then --print("NR ROOMS: "..#self._rooms.."") -- reset the map to empty for x = 1,self._pdgMapWidth do for y = 1,self._pdgMapHeight do self._pdgMap[x][y] = 4 end end -- Now,we separate the rooms local numRooms = #self._rooms for i = 1,numRooms do local v = pdg:_computeSeparation(i) --we adjust the x and y positions of the items in the room table self._rooms[i].startX = v.x self._rooms[i].startY = v.y --self._rooms[i].endX = v.x + self._rooms[i].endX --self._rooms[i].endY = v.y + self._rooms[i].endY end -- we render them again for i = 1,numRooms do local px = math.abs( math.floor(self._rooms[i].startX) ) local py = math.abs( math.floor(self._rooms[i].startY) ) for k = self.rectMinWidth,self._rooms[i].endX do for v = self.rectMinHeight,self._rooms[i].endY do print("PX IS AT: "..px.." and k is: "..k.." and their sum is: "..px+k.."") print("PY IS AT: "..py.." and v is: "..v.." and their sum is: "..py+v.."") if k == self.rectMinWidth or v == self.rectMinHeight or k == self._rooms[i].endX or v == self._rooms[i].endY then self._pdgMap[px+k][py+v] = 1 else self._pdgMap[px+k][py+v] = 2 end end end end end
解决方法
我也实现了这一代算法,我遇到了或多或少相同的问题.我的所有矩形都在最后的角落里.
我的问题是我正在将长度为零的速度矢量归一化.如果对这些进行标准化,则除以零,得到NaN.
您可以通过在进一步计算中使用它之前简单地检查速度的长度是否为零来解决此问题.
我希望这有帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。