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
|
|
|
|
2012-01-21 22:49:46 +00:00
|
|
|
local HC = {}
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:init(cell_size)
|
2018-04-08 12:35:15 +00:00
|
|
|
self:resetHash(cell_size)
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 12:35:15 +00:00
|
|
|
function HC:hash() return self._hash end -- consistent interface with global HC instance
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
-- spatial hash management
|
|
|
|
function HC:resetHash(cell_size)
|
2018-04-08 12:35:15 +00:00
|
|
|
self._hash = common_local.instance(Spatialhash, cell_size or 100)
|
2011-11-13 13:15:46 +00:00
|
|
|
return self
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:register(shape)
|
2018-04-08 12:35:15 +00:00
|
|
|
self._hash:register(shape, shape:bbox())
|
2012-02-11 18:26:19 +00:00
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
-- keep track of where/how big the shape is
|
|
|
|
for _, f in ipairs({'move', 'rotate', 'scale'}) do
|
|
|
|
local old_function = shape[f]
|
|
|
|
shape[f] = function(this, ...)
|
|
|
|
local x1,y1,x2,y2 = this:bbox()
|
|
|
|
old_function(this, ...)
|
2018-04-08 12:35:15 +00:00
|
|
|
self._hash:update(this, x1,y1,x2,y2, this:bbox())
|
2015-10-09 21:09:35 +00:00
|
|
|
return this
|
2012-07-05 15:30:13 +00:00
|
|
|
end
|
2012-02-11 18:26:19 +00:00
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
|
2012-02-11 18:26:19 +00:00
|
|
|
return shape
|
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:remove(shape)
|
2018-04-08 12:35:15 +00:00
|
|
|
self._hash:remove(shape, shape:bbox())
|
2015-10-09 21:09:35 +00:00
|
|
|
for _, f in ipairs({'move', 'rotate', 'scale'}) do
|
|
|
|
shape[f] = function()
|
|
|
|
error(f.."() called on a removed shape")
|
2012-06-10 12:24:30 +00:00
|
|
|
end
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
2015-10-09 21:09:35 +00:00
|
|
|
return self
|
2011-01-13 13:38:36 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
-- shape constructors
|
|
|
|
function HC:polygon(...)
|
|
|
|
return self:register(newPolygonShape(...))
|
2012-03-04 02:06:39 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:rectangle(x,y,w,h)
|
|
|
|
return self:polygon(x,y, x+w,y, x+w,y+h, x,y+h)
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:circle(x,y,r)
|
|
|
|
return self:register(newCircleShape(x,y,r))
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:point(x,y)
|
|
|
|
return self:register(newPointShape(x,y))
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
-- collision detection
|
|
|
|
function HC:neighbors(shape)
|
2018-04-08 12:35:15 +00:00
|
|
|
local neighbors = self._hash:inSameCells(shape:bbox())
|
2015-10-09 21:09:35 +00:00
|
|
|
rawset(neighbors, shape, nil)
|
|
|
|
return neighbors
|
2011-06-03 16:06:42 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
function HC:collisions(shape)
|
|
|
|
local candidates = self:neighbors(shape)
|
|
|
|
for other in pairs(candidates) do
|
|
|
|
local collides, dx, dy = shape:collidesWith(other)
|
|
|
|
if collides then
|
|
|
|
rawset(candidates, other, {dx,dy, x=dx, y=dy})
|
|
|
|
else
|
|
|
|
rawset(candidates, other, nil)
|
|
|
|
end
|
2012-10-08 22:50:46 +00:00
|
|
|
end
|
2015-10-09 21:09:35 +00:00
|
|
|
return candidates
|
2011-01-18 14:04:27 +00:00
|
|
|
end
|
|
|
|
|
2015-10-09 21:09:35 +00:00
|
|
|
-- the class and the instance
|
|
|
|
HC = common_local.class('HardonCollider', HC)
|
|
|
|
local instance = common_local.instance(HC)
|
2011-11-13 13:15:46 +00:00
|
|
|
|
2012-01-21 22:49:46 +00:00
|
|
|
-- the module
|
2015-10-09 21:09:35 +00:00
|
|
|
return setmetatable({
|
|
|
|
new = function(...) return common_local.instance(HC, ...) end,
|
|
|
|
resetHash = function(...) return instance:resetHash(...) end,
|
|
|
|
register = function(...) return instance:register(...) end,
|
|
|
|
remove = function(...) return instance:remove(...) end,
|
|
|
|
|
|
|
|
polygon = function(...) return instance:polygon(...) end,
|
|
|
|
rectangle = function(...) return instance:rectangle(...) end,
|
|
|
|
circle = function(...) return instance:circle(...) end,
|
|
|
|
point = function(...) return instance:point(...) end,
|
|
|
|
|
|
|
|
neighbors = function(...) return instance:neighbors(...) end,
|
|
|
|
collisions = function(...) return instance:collisions(...) end,
|
2018-04-08 12:35:15 +00:00
|
|
|
hash = function() return instance.hash() end,
|
2015-10-09 21:09:35 +00:00
|
|
|
}, {__call = function(_, ...) return common_local.instance(HC, ...) end})
|