cell - Lua Separation Steering algorithm groups overlapping rooms into one corner -
i'm trying implement dungeon generation algorithm (presented here , demo-ed here ) involves generating random number of cells overlap each other. cells pushed apart/separated , connected. now, original poster/author described using separation steering algorithm in order uniformly distribute cells on area. haven't had experience flocking algorithm and/or separation steering behavior, turned google explanation (and found this ). implementation (based on article last mentioned) follows:
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, } = 1, rtwidth if _agent ~= local distance = math.dist(self._rooms[_agent].startx, self._rooms[_agent].starty, self._rooms[i].startx, self._rooms[i].starty) if distance < 12 --print("separating agent: ".._agent.." 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 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 table contains original x , y position of room in grid, along it's width , height (endx, endy).
the problem that, instead of tiddly arranging cells on grid, takes overlapping cells , moves them area goes 1,1 distance+2, distance+2 (as seen in video [youtube])
i'm trying understand why happening.
in case it's needed, here parse grid table, separate , fill cells after separation:
function pdg:separate( ) if #self._rooms > 0 --print("nr rooms: "..#self._rooms.."") -- reset map empty x = 1, self._pdgmapwidth y = 1, self._pdgmapheight self._pdgmap[x][y] = 4 end end -- now, separate rooms local numrooms = #self._rooms = 1, numrooms local v = pdg:_computeseparation(i) --we adjust x , y positions of items in 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 -- render them again = 1, numrooms local px = math.abs( math.floor(self._rooms[i].startx) ) local py = math.abs( math.floor(self._rooms[i].starty) ) k = self.rectminwidth, self._rooms[i].endx v = self.rectminheight, self._rooms[i].endy print("px at: "..px.." , k is: "..k.." , sum is: "..px+k.."") print("py at: "..py.." , v is: "..v.." , sum is: "..py+v.."") if k == self.rectminwidth or v == self.rectminheight or k == self._rooms[i].endx or v == self._rooms[i].endy self._pdgmap[px+k][py+v] = 1 else self._pdgmap[px+k][py+v] = 2 end end end end end
i have implemented generation algorithm well, , came across more or less same issue. of rectangles ended in topleft corner.
my problem was normalizing velocity vectors 0 length. if normalize those, divide zero, resulting in nan.
you can fix performing check whether velocity's length 0 before using in further calculations.
i hope helps!
Comments
Post a Comment