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.
|
|
|
|
]]--
|
|
|
|
|
2013-07-21 10:51:35 +00:00
|
|
|
local _NAME, common_local = ..., common
|
|
|
|
if not (type(common) == 'table' and common.class and common.instance) then
|
|
|
|
assert(common_class ~= false, 'No class commons specification available.')
|
2012-01-21 22:49:46 +00:00
|
|
|
require(_NAME .. '.class')
|
|
|
|
end
|
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
|
|
|
|
2013-07-21 10:51:35 +00:00
|
|
|
-- reset global table `common' (required by class commons)
|
|
|
|
if common_local ~= common then
|
|
|
|
common_local, common = common, common_local
|
|
|
|
end
|
|
|
|
|
2012-01-21 22:49:46 +00:00
|
|
|
local newPolygonShape = Shapes.newPolygonShape
|
|
|
|
local newCircleShape = Shapes.newCircleShape
|
|
|
|
local newPointShape = Shapes.newPointShape
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local function __NULL__() end
|
|
|
|
|
2012-01-21 22:49:46 +00:00
|
|
|
local HC = {}
|
|
|
|
function HC:init(cell_size, callback_collide, callback_stop)
|
2011-11-13 13:15:46 +00:00
|
|
|
self._active_shapes = {}
|
|
|
|
self._passive_shapes = {}
|
|
|
|
self._ghost_shapes = {}
|
|
|
|
self.groups = {}
|
2012-10-08 11:48:30 +00:00
|
|
|
self._colliding_only_last_frame = {}
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
self.on_collide = callback_collide or __NULL__
|
|
|
|
self.on_stop = callback_stop or __NULL__
|
2013-07-21 10:51:35 +00:00
|
|
|
self._hash = common_local.instance(Spatialhash, cell_size)
|
2012-01-21 22:49:46 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
function HC:clear()
|
2011-11-13 15:00:08 +00:00
|
|
|
self._active_shapes = {}
|
|
|
|
self._passive_shapes = {}
|
|
|
|
self._ghost_shapes = {}
|
|
|
|
self.groups = {}
|
2012-10-08 11:48:30 +00:00
|
|
|
self._colliding_only_last_frame = {}
|
2013-07-21 10:51:35 +00:00
|
|
|
self._hash = common_local.instance(Spatialhash, self._hash.cell_size)
|
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
|
|
|
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
|
|
|
|
|
2012-07-10 14:29:59 +00:00
|
|
|
function HC:addShape(shape)
|
|
|
|
assert(shape.bbox and shape.collidesWith,
|
|
|
|
"Cannot add custom shape: Incompatible shape.")
|
2012-02-11 18:26:19 +00:00
|
|
|
|
2012-10-08 22:50:46 +00:00
|
|
|
self._active_shapes[shape] = shape
|
2012-07-10 14:29:59 +00:00
|
|
|
self._hash:insert(shape, shape:bbox())
|
2011-01-18 14:04:27 +00:00
|
|
|
shape._groups = {}
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
local hash = self._hash
|
2012-07-05 15:30:13 +00:00
|
|
|
local move, rotate,scale = shape.move, shape.rotate, shape.scale
|
|
|
|
for _, func in ipairs{'move', 'rotate', 'scale'} do
|
|
|
|
local old_func = shape[func]
|
|
|
|
shape[func] = function(self, ...)
|
|
|
|
local x1,y1,x2,y2 = self:bbox()
|
|
|
|
old_func(self, ...)
|
|
|
|
local x3,y3,x4,y4 = self:bbox()
|
|
|
|
hash:update(self, x1,y1, x2,y2, x3,y3, x4,y4)
|
|
|
|
end
|
2012-02-11 18:26:19 +00:00
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2014-04-27 09:12:47 +00:00
|
|
|
function shape:_removeFromHash()
|
|
|
|
shape.move, shape.rotate, shape.scale = move, rotate, scale
|
|
|
|
return hash:remove(shape, self:bbox())
|
|
|
|
end
|
|
|
|
|
2012-03-05 09:26:14 +00:00
|
|
|
function shape:neighbors()
|
2012-07-10 14:21:33 +00:00
|
|
|
local neighbors = hash:inRange(self:bbox())
|
|
|
|
rawset(neighbors, self, nil)
|
|
|
|
return neighbors
|
2011-01-16 16:10:35 +00:00
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2012-12-15 15:01:21 +00:00
|
|
|
function shape:inGroup(group)
|
|
|
|
return self._groups[group]
|
|
|
|
end
|
|
|
|
|
2012-02-11 18:26:19 +00:00
|
|
|
return shape
|
|
|
|
end
|
|
|
|
|
2012-03-05 09:26:14 +00:00
|
|
|
function HC:activeShapes()
|
2012-10-08 22:50:46 +00:00
|
|
|
return pairs(self._active_shapes)
|
2012-03-05 09:26:14 +00:00
|
|
|
end
|
|
|
|
|
2012-07-10 14:35:27 +00:00
|
|
|
function HC:shapesInRange(x1,y1, x2,y2)
|
|
|
|
return self._hash:inRange(x1,y1, x2,y2)
|
|
|
|
end
|
|
|
|
|
2012-02-11 18:26:19 +00:00
|
|
|
function HC:addPolygon(...)
|
2012-07-10 14:29:59 +00:00
|
|
|
return self:addShape(newPolygonShape(...))
|
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
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addCircle(cx, cy, radius)
|
2012-07-10 14:29:59 +00:00
|
|
|
return self:addShape(newCircleShape(cx,cy, radius))
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:addPoint(x,y)
|
2012-07-10 14:29:59 +00:00
|
|
|
return self:addShape(newPointShape(x,y))
|
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-01-13 13:38:36 +00:00
|
|
|
-- check for collisions
|
2011-11-13 13:15:46 +00:00
|
|
|
function HC:update(dt)
|
2012-06-10 12:24:30 +00:00
|
|
|
-- cache for tested/colliding shapes
|
2011-01-13 13:38:36 +00:00
|
|
|
local tested, colliding = {}, {}
|
2012-06-10 12:24:30 +00:00
|
|
|
local function may_skip_test(shape, other)
|
2012-07-10 14:19:27 +00:00
|
|
|
return (shape == other)
|
|
|
|
or (tested[other] and tested[other][shape])
|
2012-06-10 12:24:30 +00:00
|
|
|
or self._ghost_shapes[other]
|
|
|
|
or self:share_group(shape, other)
|
|
|
|
end
|
|
|
|
|
2012-10-08 11:48:30 +00:00
|
|
|
-- collect active shapes. necessary, because a callback might add shapes to
|
|
|
|
-- _active_shapes, which will lead to undefined behavior (=random crashes) in
|
|
|
|
-- next()
|
|
|
|
local active = {}
|
2012-10-08 22:50:46 +00:00
|
|
|
for shape in self:activeShapes() do
|
|
|
|
active[shape] = shape
|
2012-10-08 11:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local only_last_frame = self._colliding_only_last_frame
|
2012-10-08 22:50:46 +00:00
|
|
|
for shape in pairs(active) do
|
2012-06-10 12:24:30 +00:00
|
|
|
tested[shape] = {}
|
2012-07-10 14:29:59 +00:00
|
|
|
for other in self._hash:rangeIter(shape:bbox()) do
|
2012-10-08 22:50:46 +00:00
|
|
|
if not self._active_shapes[shape] then
|
2012-10-08 11:48:30 +00:00
|
|
|
-- break out of this loop is shape was removed in a callback
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2012-06-10 12:24:30 +00:00
|
|
|
if not may_skip_test(shape, other) then
|
|
|
|
local collide, sx,sy = shape:collidesWith(other)
|
|
|
|
if collide then
|
|
|
|
if not colliding[shape] then colliding[shape] = {} end
|
|
|
|
colliding[shape][other] = {sx, sy}
|
2012-10-08 11:48:30 +00:00
|
|
|
|
|
|
|
-- flag shape colliding this frame and call collision callback
|
|
|
|
if only_last_frame[shape] then
|
|
|
|
only_last_frame[shape][other] = nil
|
|
|
|
end
|
|
|
|
self.on_collide(dt, shape, other, sx, sy)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
2012-06-10 12:24:30 +00:00
|
|
|
tested[shape][other] = true
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-13 13:15:46 +00:00
|
|
|
-- call stop callback on shapes that do not collide anymore
|
2012-10-08 11:48:30 +00:00
|
|
|
for a,reg in pairs(only_last_frame) do
|
2012-06-10 12:24:30 +00:00
|
|
|
for b, info in pairs(reg) do
|
|
|
|
self.on_stop(dt, a, b, info[1], info[2])
|
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2012-10-08 11:48:30 +00:00
|
|
|
self._colliding_only_last_frame = colliding
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2012-03-05 09:37:36 +00:00
|
|
|
-- get list of shapes at point (x,y)
|
|
|
|
function HC:shapesAt(x, y)
|
|
|
|
local shapes = {}
|
2012-05-30 13:27:09 +00:00
|
|
|
for s in pairs(self._hash:cellAt(x,y)) do
|
2012-03-05 09:37:36 +00:00
|
|
|
if s:contains(x,y) then
|
|
|
|
shapes[#shapes+1] = s
|
2012-03-04 02:06:39 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-05 09:37:36 +00:00
|
|
|
return shapes
|
2012-03-04 02:06:39 +00:00
|
|
|
end
|
|
|
|
|
2011-01-13 13:38:36 +00:00
|
|
|
-- remove shape from internal tables and the hash
|
2012-02-20 09:54:07 +00:00
|
|
|
function HC:remove(shape, ...)
|
|
|
|
if not shape then return end
|
2012-10-08 22:50:46 +00:00
|
|
|
self._active_shapes[shape] = nil
|
|
|
|
self._passive_shapes[shape] = nil
|
|
|
|
self._ghost_shapes[shape] = nil
|
2013-02-09 21:04:28 +00:00
|
|
|
for name, group in pairs(shape._groups) do
|
|
|
|
group[shape] = nil
|
|
|
|
end
|
2011-01-18 14:04:27 +00:00
|
|
|
shape:_removeFromHash()
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2012-02-20 09:54:07 +00:00
|
|
|
return self:remove(...)
|
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
|
2012-10-08 22:50:46 +00:00
|
|
|
assert(self._active_shapes[shape] or self._passive_shapes[shape],
|
|
|
|
"Shape is not registered with HC")
|
2011-11-13 13:15:46 +00:00
|
|
|
if not self.groups[group] then self.groups[group] = {} end
|
|
|
|
self.groups[group][shape] = true
|
2012-10-08 22:50:46 +00:00
|
|
|
shape._groups[group] = self.groups[group]
|
2011-11-13 13:15:46 +00:00
|
|
|
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
|
2012-10-08 22:50:46 +00:00
|
|
|
assert(self._active_shapes[shape] or self._passive_shapes[shape],
|
|
|
|
"Shape is not registered with HC")
|
2011-11-13 13:15:46 +00:00
|
|
|
self.groups[group][shape] = nil
|
2012-10-08 22:50:46 +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
|
2012-10-08 22:50:46 +00:00
|
|
|
if not self._ghost_shapes[shape] then
|
|
|
|
assert(self._active_shapes[shape], "Shape is not active")
|
|
|
|
self._active_shapes[shape] = nil
|
|
|
|
self._passive_shapes[shape] = shape
|
|
|
|
end
|
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
|
2012-10-08 22:50:46 +00:00
|
|
|
if not self._ghost_shapes[shape] then
|
|
|
|
assert(self._passive_shapes[shape], "Shape is not passive")
|
|
|
|
self._active_shapes[shape] = shape
|
|
|
|
self._passive_shapes[shape] = nil
|
|
|
|
end
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
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
|
2012-10-08 22:50:46 +00:00
|
|
|
assert(self._active_shapes[shape] or self._passive_shapes[shape],
|
|
|
|
"Shape is not registered with HC")
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2012-10-08 22:50:46 +00:00
|
|
|
self._active_shapes[shape] = nil
|
2011-11-13 13:15:46 +00:00
|
|
|
-- 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
|
2012-10-08 22:50:46 +00:00
|
|
|
assert(self._ghost_shapes[shape], "Shape not a ghost")
|
2011-11-13 13:15:46 +00:00
|
|
|
|
|
|
|
-- re-register shape. passive shapes were not unregistered above, so if a shape
|
|
|
|
-- is not passive, it must be registered as active again.
|
2012-10-08 22:50:46 +00:00
|
|
|
if not self._passive_shapes[shape] then
|
|
|
|
self._active_shapes[shape] = shape
|
2011-11-13 13:15:46 +00:00
|
|
|
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
|
|
|
|
2012-01-21 22:49:46 +00:00
|
|
|
-- the module
|
2013-07-21 10:51:35 +00:00
|
|
|
HC = common_local.class("HardonCollider", HC)
|
2012-01-21 22:49:46 +00:00
|
|
|
local function new(...)
|
2013-07-21 10:51:35 +00:00
|
|
|
return common_local.instance(HC, ...)
|
2012-01-21 22:49:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable({HardonCollider = HC, new = new},
|
|
|
|
{__call = function(_,...) return new(...) end})
|