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.
This commit is contained in:
Matthias Richter 2012-04-22 01:10:28 +02:00
parent 9ac3d870ff
commit 7d09bd30e2

View File

@ -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
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,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)
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)
love.graphics.print(("%d:%d"):format(k1,k2), x+3,y+3)
end
end
end
end