2011-01-13 13:38:36 +00:00
|
|
|
--[[
|
|
|
|
Copyright (c) 2011 Matthias Richter
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name(s) of the above copyright holders
|
|
|
|
shall not be used in advertising or otherwise to promote the sale, use or
|
|
|
|
other dealings in this Software without prior written authorization.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
]]--
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local _NAME = (...)
|
|
|
|
local Class = require(_NAME .. '.class')
|
2011-01-18 22:33:11 +00:00
|
|
|
local Shapes = require(_NAME .. '.shapes')
|
|
|
|
local Spatialhash = require(_NAME .. '.spatialhash')
|
2011-02-05 23:25:20 +00:00
|
|
|
local vector = require(_NAME .. '.vector')
|
|
|
|
|
2011-01-18 22:33:11 +00:00
|
|
|
local PolygonShape = Shapes.PolygonShape
|
|
|
|
local CircleShape = Shapes.CircleShape
|
2011-02-26 16:27:04 +00:00
|
|
|
local PointShape = Shapes.PointShape
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local function __NULL__() end
|
|
|
|
|
|
|
|
local HC = Class{name = "HardonCollider", function(self, cell_size, callback_collide, callback_stop)
|
|
|
|
self._active_shapes = {}
|
|
|
|
self._passive_shapes = {}
|
|
|
|
self._ghost_shapes = {}
|
|
|
|
self._current_shape_id = 0
|
|
|
|
self._shape_ids = setmetatable({}, {__mode = "k"}) -- reverse lookup
|
|
|
|
self.groups = {}
|
|
|
|
self._colliding_last_frame = {}
|
|
|
|
|
|
|
|
self.on_collide = callback_collide or __NULL__
|
|
|
|
self.on_stop = callback_stop or __NULL__
|
|
|
|
self._hash = Spatialhash(cell_size)
|
|
|
|
end}
|
|
|
|
|
|
|
|
function HC:clear()
|
|
|
|
self = HC(self.hash.cell_size, self.on_collide, self.on_stop)
|
|
|
|
return self
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:setCallbacks(collide, stop)
|
2011-06-03 16:06:42 +00:00
|
|
|
if type(collide) == "table" and not (getmetatable(collide) or {}).__call then
|
|
|
|
stop = collide.stop
|
|
|
|
collide = collide.collide
|
|
|
|
end
|
|
|
|
|
|
|
|
if collide then
|
|
|
|
assert(type(collide) == "function" or (getmetatable(collide) or {}).__call,
|
|
|
|
"collision callback must be a function or callable table")
|
2011-11-13 13:15:46 +00:00
|
|
|
self.on_collide = collide
|
2011-06-03 16:06:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if stop then
|
|
|
|
assert(type(stop) == "function" or (getmetatable(stop) or {}).__call,
|
|
|
|
"stop callback must be a function or callable table")
|
2011-11-13 13:15:46 +00:00
|
|
|
self.on_stop = stop
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
return self
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local function new_shape(self, shape, ul,lr)
|
|
|
|
self._current_shape_id = self._current_shape_id + 1
|
|
|
|
self._active_shapes[self._current_shape_id] = shape
|
|
|
|
self._shape_ids[shape] = self._current_shape_id
|
|
|
|
self._hash:insert(shape, ul,lr)
|
2011-01-18 14:04:27 +00:00
|
|
|
shape._groups = {}
|
2011-01-13 13:38:36 +00:00
|
|
|
return shape
|
|
|
|
end
|
|
|
|
|
|
|
|
-- create polygon shape and add it to internal structures
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addPolygon(...)
|
2011-01-16 16:10:35 +00:00
|
|
|
local shape = PolygonShape(...)
|
2011-11-13 13:15:46 +00:00
|
|
|
local hash = self._hash
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2011-01-16 16:10:35 +00:00
|
|
|
-- replace shape member function with a function that updates
|
2011-01-13 13:38:36 +00:00
|
|
|
-- the hash
|
|
|
|
local function hash_aware_member(oldfunc)
|
|
|
|
return function(self, ...)
|
|
|
|
local x1,y1, x2,y2 = self._polygon:getBBox()
|
|
|
|
oldfunc(self, ...)
|
|
|
|
local x3,y3, x4,y4 = self._polygon:getBBox()
|
2011-11-13 13:15:46 +00:00
|
|
|
hash:update(shape, vector(x1,y1), vector(x2,y2), vector(x3,y3), vector(x4,y4))
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-01-13 13:38:36 +00:00
|
|
|
shape.move = hash_aware_member(shape.move)
|
|
|
|
shape.rotate = hash_aware_member(shape.rotate)
|
|
|
|
|
2011-01-18 14:04:27 +00:00
|
|
|
function shape:_getNeighbors()
|
2011-01-16 16:10:35 +00:00
|
|
|
local x1,y1, x2,y2 = self._polygon:getBBox()
|
|
|
|
return hash:getNeighbors(self, vector(x1,y1), vector(x2,y2))
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-01-18 14:04:27 +00:00
|
|
|
function shape:_removeFromHash()
|
2011-01-16 16:10:35 +00:00
|
|
|
local x1,y1, x2,y2 = self._polygon:getBBox()
|
2011-11-13 13:15:46 +00:00
|
|
|
hash:remove(shape) --, vector(x1,y1), vector(x2,y2))
|
2011-01-16 16:10:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local x1,y1, x2,y2 = shape._polygon:getBBox()
|
2011-11-13 13:15:46 +00:00
|
|
|
return new_shape(self, shape, vector(x1,y1), vector(x2,y2))
|
2011-01-14 22:25:24 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addRectangle(x,y,w,h)
|
|
|
|
return self:addPolygon(x,y, x+w,y, x+w,y+h, x,y+h)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- create new polygon approximation of a circle
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addCircle(cx, cy, radius)
|
2011-01-13 13:38:36 +00:00
|
|
|
local shape = CircleShape(cx,cy, radius)
|
2011-11-13 13:15:46 +00:00
|
|
|
local hash = self._hash
|
|
|
|
|
2011-01-14 22:25:24 +00:00
|
|
|
local function hash_aware_member(oldfunc)
|
|
|
|
return function(self, ...)
|
|
|
|
local r = vector(self._radius, self._radius)
|
|
|
|
local c1 = self._center
|
|
|
|
oldfunc(self, ...)
|
|
|
|
local c2 = self._center
|
|
|
|
hash:update(self, c1-r, c1+r, c2-r, c2+r)
|
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-01-14 22:25:24 +00:00
|
|
|
shape.move = hash_aware_member(shape.move)
|
|
|
|
shape.rotate = hash_aware_member(shape.rotate)
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-01-18 14:04:27 +00:00
|
|
|
function shape:_getNeighbors()
|
2011-01-16 16:10:35 +00:00
|
|
|
local c,r = self._center, vector(self._radius, self._radius)
|
|
|
|
return hash:getNeighbors(self, c-r, c+r)
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-01-18 14:04:27 +00:00
|
|
|
function shape:_removeFromHash()
|
2011-01-16 16:10:35 +00:00
|
|
|
local c,r = self._center, vector(self._radius, self._radius)
|
|
|
|
hash:remove(self, c-r, c+r)
|
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
|
|
|
|
local c,r = shape._center, vector(radius,radius)
|
2011-11-13 13:15:46 +00:00
|
|
|
return new_shape(self, shape, c-r, c+r)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addPoint(x,y)
|
2011-02-26 16:27:04 +00:00
|
|
|
local shape = PointShape(x,y)
|
2011-11-13 13:15:46 +00:00
|
|
|
local hash = self._hash
|
|
|
|
|
2011-02-26 16:27:04 +00:00
|
|
|
local function hash_aware_member(oldfunc)
|
|
|
|
return function(self, ...)
|
|
|
|
rawset(hash:cell(self._pos), self, nil)
|
|
|
|
oldfunc(self, ...)
|
|
|
|
rawset(hash:cell(self._pos), self, self)
|
|
|
|
end
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-02-26 16:27:04 +00:00
|
|
|
shape.move = hash_aware_member(shape.move)
|
|
|
|
shape.rotate = hash_aware_member(shape.rotate)
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-02-26 16:27:04 +00:00
|
|
|
function shape:_getNeighbors()
|
|
|
|
local set = {}
|
|
|
|
for _,other in pairs(hash:cell(self._pos)) do
|
|
|
|
rawset(set, other, other)
|
|
|
|
end
|
|
|
|
rawset(set, self, nil)
|
|
|
|
return set
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2011-02-26 16:27:04 +00:00
|
|
|
function shape:_removeFromHash()
|
|
|
|
hash:remove(self, self._pos, self._pos)
|
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
return new_shape(self, shape, shape._pos, shape._pos)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:share_group(shape, other)
|
2011-01-18 14:04:27 +00:00
|
|
|
for name,group in pairs(shape._groups) do
|
|
|
|
if group[other] then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2011-02-01 22:39:59 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
-- get unique indentifier for an unordered pair of shapes, i.e.:
|
|
|
|
-- collision_id(s,t) = collision_id(t,s)
|
|
|
|
local function collision_id(self,s,t)
|
|
|
|
local i,k = self._shape_ids[s], self._shape_ids[t]
|
|
|
|
if i < k then i,k = k,i end
|
|
|
|
return string.format("%d,%d", i,k)
|
2011-01-14 22:25:24 +00:00
|
|
|
end
|
|
|
|
|
2011-01-13 13:38:36 +00:00
|
|
|
-- check for collisions
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:update(dt)
|
2011-01-13 13:38:36 +00:00
|
|
|
-- collect colliding shapes
|
|
|
|
local tested, colliding = {}, {}
|
2011-11-13 13:15:46 +00:00
|
|
|
for _,shape in pairs(self._active_shapes) do
|
2011-06-03 16:06:42 +00:00
|
|
|
local neighbors = shape:_getNeighbors()
|
|
|
|
for _,other in pairs(neighbors) do
|
2011-11-13 13:15:46 +00:00
|
|
|
local id = collision_id(self, shape,other)
|
2011-06-03 16:06:42 +00:00
|
|
|
if not tested[id] then
|
2011-11-13 13:15:46 +00:00
|
|
|
if not (self._ghost_shapes[other] or self:share_group(shape, other)) then
|
2011-06-03 16:06:42 +00:00
|
|
|
local collide, sep = shape:collidesWith(other)
|
|
|
|
if collide then
|
|
|
|
colliding[id] = {shape, other, sep.x, sep.y}
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
2011-06-03 16:06:42 +00:00
|
|
|
tested[id] = true
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- call colliding callbacks on colliding shapes
|
|
|
|
for id,info in pairs(colliding) do
|
2011-11-13 13:15:46 +00:00
|
|
|
self._colliding_last_frame[id] = nil
|
|
|
|
self.on_collide( dt, unpack(info) )
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
-- call stop callback on shapes that do not collide anymore
|
|
|
|
for _,info in pairs(self._colliding_last_frame) do
|
|
|
|
self.on_stop( dt, unpack(info) )
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
self._colliding_last_frame = colliding
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- remove shape from internal tables and the hash
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:remove(shape)
|
|
|
|
local id = self._shape_ids[shape]
|
2011-06-03 16:06:42 +00:00
|
|
|
if id then
|
2011-11-13 13:15:46 +00:00
|
|
|
self._active_shapes[id] = nil
|
|
|
|
self._passive_shapes[id] = nil
|
2011-06-03 16:06:42 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
self._ghost_shapes[shape] = nil
|
|
|
|
self._shape_ids[shape] = nil
|
2011-01-18 14:04:27 +00:00
|
|
|
shape:_removeFromHash()
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
return shape
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- group support
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addToGroup(group, shape, ...)
|
2011-01-18 14:04:27 +00:00
|
|
|
if not shape then return end
|
2011-11-13 13:15:46 +00:00
|
|
|
assert(self._shape_ids[shape], "Shape not registered!")
|
|
|
|
|
|
|
|
if not self.groups[group] then self.groups[group] = {} end
|
|
|
|
self.groups[group][shape] = true
|
|
|
|
shape._groups[group] = self.groups[group]
|
|
|
|
return self:addToGroup(group, ...)
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:removeFromGroup(group, shape, ...)
|
|
|
|
if not shape or not self.groups[group] then return end
|
|
|
|
assert(self._shape_ids[shape], "Shape not registered!")
|
|
|
|
|
|
|
|
self.groups[group][shape] = nil
|
2011-01-18 14:04:27 +00:00
|
|
|
shape._groups[group] = nil
|
2011-11-13 13:15:46 +00:00
|
|
|
return self:removeFromGroup(group, ...)
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:setPassive(shape, ...)
|
2011-06-03 16:06:42 +00:00
|
|
|
if not shape then return end
|
2011-11-13 13:15:46 +00:00
|
|
|
assert(self._shape_ids[shape], "Shape not registered!")
|
|
|
|
|
|
|
|
local id = self._shape_ids[shape]
|
|
|
|
if not id or self._ghost_shapes[shape] then return end
|
2011-06-03 16:06:42 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
self._active_shapes[id] = nil
|
|
|
|
self._passive_shapes[id] = shape
|
2011-06-03 16:06:42 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
return self:setPassive(...)
|
2011-06-03 16:06:42 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:setActive(shape, ...)
|
2011-06-03 16:06:42 +00:00
|
|
|
if not shape then return end
|
2011-11-13 13:15:46 +00:00
|
|
|
assert(self._shape_ids[shape], "Shape not registered!")
|
2011-06-03 16:06:42 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local id = self._shape_ids[shape]
|
|
|
|
if not id or self._ghost_shapes[shape] then return end
|
2011-06-03 16:06:42 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
self._active_shapes[id] = shape
|
|
|
|
self._passive_shapes[id] = nil
|
|
|
|
|
|
|
|
return self:setActive(...)
|
2011-06-03 16:06:42 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:setGhost(shape, ...)
|
2011-01-18 14:04:27 +00:00
|
|
|
if not shape then return end
|
2011-11-13 13:15:46 +00:00
|
|
|
local id = self._shape_ids[shape]
|
|
|
|
assert(id, "Shape not registered!")
|
|
|
|
|
|
|
|
self._active_shapes[id] = nil
|
|
|
|
-- dont remove from passive shapes, see below
|
|
|
|
self._ghost_shapes[shape] = shape
|
|
|
|
return self:setGhost(...)
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:setSolid(shape, ...)
|
2011-01-18 14:04:27 +00:00
|
|
|
if not shape then return end
|
2011-11-13 13:15:46 +00:00
|
|
|
local id = self._shape_ids[shape]
|
|
|
|
assert(id, "Shape not registered!")
|
|
|
|
|
|
|
|
-- re-register shape. passive shapes were not unregistered above, so if a shape
|
|
|
|
-- is not passive, it must be registered as active again.
|
|
|
|
if not self._passive_shapes[id] then
|
|
|
|
self._active_shapes[id] = shape
|
|
|
|
end
|
|
|
|
self._ghost_shapes[shape] = nil
|
|
|
|
return self:setSolid(...)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
return setmetatable({new = HC}, {__call = function(_,...) return HC(...) end})
|