Allows traversing the spatialhash with a ray

This commit is contained in:
Ignazio Setti 2016-06-01 13:13:23 +02:00
parent eac8874ef9
commit d34709c426

View File

@ -148,6 +148,43 @@ function Spatialhash:update(obj, old_x1,old_y1, old_x2,old_y2, new_x1,new_y1, ne
end
end
function Spatialhash:intersectionsWithSegment(x1, y1, x2, y2)
local odx, ody = x2 - x1, y2 - y1
local len, cur = vector.len(odx, ody), 0
local dx, dy = vector.normalize(odx, ody)
local step = self.cell_size / 2
local visited = {}
local points = {}
local mt = math.huge
while (cur + step < len) do
local cx, cy = x1 + dx * cur, y1 + dy * cur
local shapes = self:cellAt(cx, cy)
cur = cur + step
for _, shape in pairs(shapes) do
if (not visited[shape]) then
local ints = shape:intersectionsWithRay(x1, y1, dx, dy)
for _, t in ipairs(ints) do
if (t >= 0 and t <= len) then
local px, py = vector.add(x1, y1, vector.mul(t, dx, dy))
table.insert(points, {shape, t, px, py})
end
end
visited[shape] = true
end
end
end
table.sort(points, function(a, b)
return a[2] < b[2]
end)
return points
end
function Spatialhash:draw(how, show_empty, print_key)
if show_empty == nil then show_empty = true end
for k1,v in pairs(self.cells) do