From 7d09bd30e225dc0d3d7ffd8b59d878ca073b86dd Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Sun, 22 Apr 2012 01:10:28 +0200 Subject: [PATCH] Fix issue #10. Fix spatialhash drawing. Shapes were not inserted into the spatial hash when not moved. Shapes set to passive directly after creation would not be in the hash, resulting in no collision reports. Spatialhash drawing updated to the new cell indexing method. --- spatialhash.lua | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/spatialhash.lua b/spatialhash.lua index fa3fa79..2db756f 100644 --- a/spatialhash.lua +++ b/spatialhash.lua @@ -37,7 +37,6 @@ end -- cells = {[0] = {[0] = <>, [1] = <>, ... }, [1] = {...}, ...} local cells_meta = {} function cells_meta.__newindex(tbl, key, val) - print('newindex') local cell = rawget(tbl, key.x) if not cell then rawset(tbl, key.x, {[key.y] = val}) @@ -82,7 +81,7 @@ function Spatialhash:insert(obj, ul, lr) local lr = self:cellCoords(lr) for i = ul.x,lr.x do for k = ul.y,lr.y do - + rawset(self.cells[{x=i,y=k}], obj, obj) end end end @@ -108,6 +107,7 @@ end -- update an objects position function Spatialhash:update(obj, ul_old, lr_old, ul_new, lr_new) + print('hash:update', obj) local ul_old, lr_old = self:cellCoords(ul_old), self:cellCoords(lr_old) local ul_new, lr_new = self:cellCoords(ul_new), self:cellCoords(lr_new) @@ -146,17 +146,18 @@ end function Spatialhash:draw(how, show_empty, print_key) if show_empty == nil then show_empty = true end - for k,cell in pairs(self.cells) do - local empty = true - (function() for _ in pairs(cell) do empty = false; return end end)() - if show_empty or not empty then - local x,y = k:match("([^,]+),([^,]+)") - x = x * self.cell_size - y = y * self.cell_size - love.graphics.rectangle(how, x,y, self.cell_size, self.cell_size) + for k1,v in pairs(self.cells) do + for k2,cell in pairs(v) do + local empty = true + (function() for _ in pairs(cell) do empty = false; return end end)() + if show_empty or not empty then + local x = k1 * self.cell_size + local y = k2 * self.cell_size + love.graphics.rectangle(how or 'line', x,y, self.cell_size, self.cell_size) - if print_key then - love.graphics.print(k, x+3,y+3) + if print_key then + love.graphics.print(("%d:%d"):format(k1,k2), x+3,y+3) + end end end end