From 317cce31c6594673f8337df2e9919613b96de2c1 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Tue, 10 Jul 2012 16:29:59 +0200 Subject: [PATCH] Add HC:addShape(shape). Shape must have at least two functions: - shape:bbox() must return an axis aligned bounding box of the shape - shape:collidesWith(other) must return false, ... if the two shapes do not collide must return true, sx, sy if the two shapes collide, where sx,sy is the separation vector from from shape to other. Convex shapes can use the supplied GJK algorithm. In that case the shape must implement shape:support(dx,dy) which returns the point of the shape that lies furthest in the direction of dx,dy. --- init.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index 7da3102..809e980 100644 --- a/init.lua +++ b/init.lua @@ -86,13 +86,14 @@ function HC:setCallbacks(collide, stop) return self end -local function new_shape(self, shape) - local x1,y1,x2,y2 = shape:bbox() +function HC:addShape(shape) + assert(shape.bbox and shape.collidesWith, + "Cannot add custom shape: Incompatible shape.") 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, x1,y1, x2,y2) + self._hash:insert(shape, shape:bbox()) shape._groups = {} local hash = self._hash @@ -129,7 +130,7 @@ function HC:activeShapes() end function HC:addPolygon(...) - return new_shape(self, newPolygonShape(...)) + return self:addShape(newPolygonShape(...)) end function HC:addRectangle(x,y,w,h) @@ -137,11 +138,11 @@ function HC:addRectangle(x,y,w,h) end function HC:addCircle(cx, cy, radius) - return new_shape(self, newCircleShape(cx,cy, radius)) + return self:addShape(newCircleShape(cx,cy, radius)) end function HC:addPoint(x,y) - return new_shape(self, newPointShape(x,y)) + return self:addShape(newPointShape(x,y)) end function HC:share_group(shape, other) @@ -165,7 +166,7 @@ function HC:update(dt) -- collect colliding shapes for shape in self:activeShapes() do tested[shape] = {} - for other in hash:rangeIter(shape:bbox()) do + for other in self._hash:rangeIter(shape:bbox()) do if not may_skip_test(shape, other) then local collide, sx,sy = shape:collidesWith(other) if collide then