Fork me on GitHub
hardoncollider^ top
require "hardoncollider"

The main module.

HardonCollider will automatically handle - but not resolve - collisions. It uses search data structure - a spatial hash - to quickly find colliding shapes.

A spatial hash is simply a grid that is laid over the whole scene in which a shape can occupy several cells. To find shapes that may be colliding, you simply need to look which shapes occupy the same cell. You can specify the cell size in the init() function.

To get a less boring explanation on how to use this, see the tutorial (once it's there).

Module overview

init()
Initialize module.
setCallbacks()
Set callback functions.
update()
Update collision detection.
setAutoUpdate()
Update collision detection automatically.
setNoAutoUpdate()
Disable automatic updating.
addPolygon()
Add polygon to the scene.
addRectangle()
Add rectangle to the scene.
addCircle()
Add circle to the scene.
remove()
Remove shape from scene.
addToGroup()
Group shapes that should not collide.
removeFromGroup()
Remove shape from a group.
setGhost()
Stops shape from colliding.
setSolid()
Make shape subject to collision again.
function init(cell_size, callback_start, callback_persist, callback_stop)^ top

Initializes the library. Call this in love.load(). All the parameters can be omitted.

Parameters:
number cell_size (100)
Cell size for internal search structure.
function callback_start (empty function)
Called when two shapes start colliding.
function callback_persist (empty function)
Called when two continue to collide, i.e. if the collision lasts more than one frame.
function callback_stop (empty function)
Called when two shapes stop colliding.
Returns:
Nothing
Example:
function love.load()
    hardoncollider.init(150)
end
function setCallbacks(start,persist,stop)^ top
function setCallbacks{start = start,persist = persist,stop = stop}

Sets the different callbacks. The second calling style let's you specify the callbacks by name, see the example.

If nil is passed as any callback, the callback will not be changed.

Each callback has the following prototype:

function callback(dt, shape_one, shape_two, mtv_x, mtv_y)
The two shape parameters are the colliding shapes. The last two parameters, mtv_x and mtv_y define the minimum translation vector, i.e. the direction and magnitude shape_one has to be moved so that the collision will be resolved.

Parameters:
function start
Called when two shapes start colliding.
function persist
Called when two continue to collide, i.e. if the collision lasts more than one frame.
function stop
Called when two shapes stop colliding.
Returns:
Nothing
Example:
function start(dt, shape_one, shape_two, mtv_x, mtv_y)
    print('started colliding:', shape_one, shape_two)
	print('mtv:', mtv_x, mtv_y)
end

function persist_one(dt, shape_one, shape_two, mtv_x, mtv_y)
    print('still colliding:', shape_one, shape_two)
    -- move both shape_one and shape_two to resolve the collision
    shape_one:move(mtv_x/2, mtv_y/2)
    shape_two:move(-mtv_x/2, -mtv_y/2)
end

function persist_two(dt, shape_one, shape_two, mtv_x, mtv_y)
    print('still colliding:', shape_one, shape_two)
    -- move only shape_one to resolve the collision
    shape_one:move(mtv_x, mtv_y)
end

-- ignore the translation vector
function stop(dt, shape_one, shape_two)
    print('collision resolved')
end

function love.load()
    hardoncollider.init(100)
    -- set initial callbacks
    hardoncollider.setCallbacks(start, persist_one, stop)
    -- change persist callback
    hardoncollider.setCallbacks{persist = persist_two}
end
function update(dt)^ top

Checks for collisions and call callbacks. Use this in love.update(dt).

A maximum time delta can be specified. dt will be chopped up in slices not bigger than this maximum and the scene is updated for each time slice.

Note that the delta time has no effect on the collision detection itself, but will be passed to the callback functions.

Parameters:
number dt
The time since the last update.
Returns:
Nothing
Example:
function love.update(dt)
    hardoncollider.update(dt, .02)
end
function setAutoUpdate(max_step)^ top

Automatically call hardoncollider.update(dt) after each love.update(dt).

Basically overwrites love.update(dt) with the following function:

function love.update(dt)
    _old_love_update(dt)
    hardoncollider.update(dt)
end

You can define a maximum time step. If dt is bigger than this step, the new update function will be called multiple times with a dt smaller or equal to the maximum time step:

while dt > max_step do
    update(max_step)
    dt = dt - max_step
end
update(dt)

If max_step is bigger than 1, it is assumed to declare a minimum frame rate.

Once set, you can disable the auto update with setNoAutoUpdate(), but beware that this might break other libs that hook into love.update, e.g. hump.gamestate. To prevent unexpected side effects, it's best practice to call setAutoUpdate() after any other library hooked itself into love.update()

Parameters:
number max_step (optional)
Maximum time step (see above).
Returns:
Nothing
Example:
function love.load()
    HC.init(100, collision_start, collision_persist, collision_stop)
    game_init()
    HC.setAutoUpdate(30) -- maintain at least a update framerate of 30 FPS
end
function setNoAutoUpdate()^ top

Resets love.update() to the state before calling setAutoUpdate().

Parameters:
None
Returns:
Nothing
Example:
if game_over then
    hardoncollider.setNoAutoUpdate()
end
function addPolygon(x1,y1, ..., xn,yn)^ top

Add a polygon to the collision detection system. Any non-intersection polygon will work, even convex polygons.

Note that if three consecutive points lie on a line, the middle point will be discarded. This means you cannot construct polygon shapes out of lines.

Parameters:
numbers x1,y1, ..., xn,yn
The corners of the polygon. At least three corners (that do not lie on a line) are needed.
Returns:
Shape
The polygon shape added to the scene.
Example:
shape = hardoncollider.addPolygon(10,10, 40,50, 70,10, 40,30)
function addRectangle(x, y, w, h)^ top

Add a rectangle shape to the collision detection system.

Parameters:
numbers x, y
The upper left corner of the rectangle.
numbers w, h
The width and height of the rectangle.
Returns:
Shape
The rectangle added to the scene.
Example:
rect = hardoncollider.addRectangle(100,120, 200,40)
function addCircle(cx, cy, radius)^ top

Add a circle shape to the collision detection system.

Parameters:
numbers cx, cy
The circle center.
number radius
The circle radius.
Returns:
Shape
The circle added to the scene.
Example:
circle = hardoncollider.addCircle(400,300, 100)
function remove(shape)^ top

Remove a shape from the collision detection system. Note that if you remove a shape in the start or persist callback, other shapes might still have collided with it, so the shape will be argument to the other calls of start or persist. In any case, the stop callback will be called in the next call to update for each shape which the removed shape collided with.

Parameters:
Shape shape
The shape to be removed.
Returns:
Nothing
Example:
hardoncollider.remove(circle)
function addToGroup(group, shape, ...)^ top

Add shapes to a group. Shapes in the same group will not collide with each other.

Parameters:
string group
The name of the group where shapes should be added.
Shapes shape, ...
The shapes to be added to the group.
Returns:
Nothing
Example:
hardoncollider.addToGroup("platforms", platform1, platform2, platform3)
function removeFromGroup(group, shape, ...)^ top

Remove shapes from a group.

Parameters:
string group
The name of the group where shapes should be added.
Shapes shape, ...
The shapes to be removed from the group.
Returns:
Nothing
Example:
hardoncollider.removeFromGroup("platforms", not_a_platform)
function setGhost(shape, ...)^ top

Makes a shape permeable. Ghost shapes will not collide with any other shape.

Parameters:
Shapes shape, ...
The shapes to become permeable.
Returns:
Nothing
Example:
if cheat.set_ghost then
    hardoncollider.setGhost(player)
end
function setSolid(shape, ...)^ top

Makes a shape opaque. Shapes that were ghosts before will be subject to collision again.

Parameters:
Shapes shape, ...
The shapes to become opaque again.
Returns:
Nothing
Example:
if cheat.set_no_ghost then
    hardoncollider.setSolid(player_recording)
end
hardoncollider.shapes^ top
shapes = require "hardoncollider.shapes"

Shape classes with collision detection methods.

This defines methods to move, rotate and draw shapes created with hardoncollider.add*.

As each shape is at it's core a Lua table, you can attach values and add functions to it. Be careful though not to use keys that name a function or start with an underscore, e.g. move or _groups, since these are used internally. Everything else is fine.

If you don't want to use the full blown module, you can still use these classes to test for colliding shapes. They might also be useful for doing GUI stuff, e.g. when testing if the mouse hovers a button.

Some functions (getAxes and projectOn) are left undocumented, as they have little value outside the scope of collision detection.

Module overview

shapes.PolygonShape
A polygon shape.
shapes.CircleShape
A circle shape.
shape:move()
Move the shape.
shape:rotate()
Rotate the shape.
shape:center()
Get the shape's center.
shape:draw()
Draw the shape.
shape:collidesWith()
Test for collision.
class PolygonShape(x1,y1, ..., xn,yn)^ top
class PolygonShape(polygon)

Construct a shape using a non-intersecting ploygon.

You can either specify the coordinates as with hardoncollider.addPolygon() or use an instance of the Polygon class.

Parameters:
numbers x1,y1, ..., xn,yn
The corners of the polygon. At least three corners (that do not lie on a line) are needed.
Polygon polygon
Construct the shape from this polygon.
Returns:
Shape
The constructed shape.
Example:
shape = shapes.PolygonShape(100,100, 200,200, 300,100)
class CircleShape(cx,cy, radius)^ top

Construct a circular shape.

Parameters:
numbers cx, cy
The circle center.
number radius
The circle radius.
Returns:
Shape
The constructed circle shape.
Example:
shape = shapes.CircleShape(400,300, 100)
function shape:move(x, y)^ top

Move the shape.

Parameters:
numbers x, y
The direction to move the shape in.
Returns:
Nothing
Example:
circle:move(10,15) -- move the circle 10 units right and 15 units down
function shape:rotate(angle, cx,cy)^ top

Rotate the shape. A rotation center can be specified. If no center is given, the shape's center is used.

Parameters:
number angle
Amount to rotate the shape (in radians).
numbers cx, cy
Rotation center. Defaults to the shape's center if omitted.
Returns:
Nothing
Example:
rectangle:rotate(math.pi/4)
function shape:center()^ top

Get the center of the shape.

If the shape is a CircleShape, this is the circle center, else it's the polygon's centroid.

Parameters:
None
Returns:
numbers x, y
The center of the shape.
Example:
print("Circle at:", circle:center())
function shape:draw(mode)^ top

Draw the shape either filled or as outline.

Parameters:
DrawMode mode
How to draw the shape. Either 'line' or 'fill'.
Returns:
Nothing
Example:
circle:draw('fill')
function shape:collidesWith(other)^ top

Test if two shapes collide.

Parameters:
Shape other
Test for collision with this shape.
Returns:
boolean collide
true if the two shapes collide, false otherwise.
vector mtv
The minimum translation vector, or nil if the two shapes don't collide.
Example:
if circle:collidesWith(rectangle) then
    print("collision detected!")
end
hardoncollider.polygon^ top
polygon = require "hardoncollider.polygon"

Definition of a Polygon class and implementation of some handy algorithms.

On it's own, this class does not offer any collision detection. If you want that, use a PolygonShape instead.

Module overview

Polygon
The polygon class.
polygon:unpack()
Get coordinates.
polygon:clone()
Copy polygon.
polygon:getBBox()
Get bounding box.
polygon:isConvex()
Test if polygon is convex.
polygon:move()
Move polygon.
polygon:rotate()
Rotate polygon.
polygon:triangulate()
Split polygon in triangles.
polygon:splitConvex()
Split polygon into convex polygons.
polygon:mergedWith()
Merge polygon with other polygon.
class Polygon(x1,y1, ..., xn,yn)^ top

Construct a polygon.

At least three points that are not collinear (being on a straight line) are needed to construct the polygon. If there are collinear points, these points will be removed so that the overall shape of the polygon is not changed.

Parameters:
numbers x1,y1, ..., xn,yn
The corners of the polygon. At least three corners are needed.
Returns:
Polygon
The polygon object.
Example:
poly = polygon.Polygon(10,10, 40,50, 70,10, 40,30)

polygon.Polygon looks rather verbose - that is why you can actually call the module like a function to create an instance of the Polygon class:

poly = polygon(10,10, 40,50, 70,10, 40,30)
function polygon:unpack()^ top

Get the polygon's vertices. Useful for drawing with love.graphics.polygon().

Parameters:
None
Returns:
numbers x1,y1, ..., xn,yn
The vertices of the polygon.
Example:
love.graphics.draw('line', poly:unpack())
function polygon:clone()^ top

Get a copy of the polygon.

Since Lua uses references when simply assigning an existing polygon to a variable, unexpected things can happen when operating on the variable. Consider this code:

p1 = Polygon(10,10, 40,50, 70,10, 40,30)
p2 = p1
p3 = p1:clone()
p2:rotate(math.pi) -- p1 will be rotated, too!
p3:rotate(-math.pi) -- only p3 will be rotated
Parameters:
None
Returns:
Polygon polygon
A copy of the polygon.
Example:
copy = poly:clone()
copy:move(10,20)
function polygon:getBBox()^ top

Get axis aligned bounding box.

Parameters:
None
Returns:
numbers x1, y1
Upper left corner of the bounding box.
numbers x2, y2
Lower right corner of the bounding box.
Example:
x1,y1,x2,y2 = poly:getBBox()
-- draw bounding box
love.graphics.rectangle('line', x1,y2, x2-x1, y2-y1)
function polygon:isConvex()^ top

Test if a polygon is convex, i.e. a line line between any two points inside the polygon will lie in the interior of the polygon.

Parameters:
None
Returns:
boolean convex
true if the polygon is convex, false otherwise.
Example:
-- split into convex sub polygons
if not poly:isConvex() then
    list = poly:splitConvex()
else
    list = {poly:clone()}
end
function polygon:move(x,y)^ top
function polygon:move(direction)

Move a polygon in a direction. You can either use coordinates x,y or a hump vector.

Parameters:
numbers x, y
Coordinates of the direction to move.
vector direction
Direction to move.
Returns:
Nothing
Example:
poly:move(10,-5) -- move 10 units right and 5 units up
function polygon:rotate(angle)^ top
function polygon:rotate(angle, cx, cy)
function polygon:rotate(angle, center)

Rotate the polygon. You can define a rotation center. If it is omitted, the polygon will be rotated around it's centroid.

For defining a rotation center, you can either use coordinate form cx,cy or a hump vector.

Parameters:
number angle
The angle to rotate in radians.
numbers cx, cy
The rotation center.
vector center
The rotation center.
Returns:
Nothing
Example:
p1:rotate(math.pi/2)          -- rotate p1 by 90° around it's center
p2:rotate(math.pi/4, 100,100) -- rotate p2 by 45° around the point 100,100
function polygon:triangulate()^ top

Split the polygon into triangles.

Parameters:
None
Returns:
Array of Polygon triangles
Triangles that the polygon is composed of.
Example:
triangles = poly:triangulate()
for i,triangle in ipairs(triangles) do
    triangles.move(math.random(5,10), math.random(5,10))
end	
function polygon:splitConvex()^ top

Split the polygon into convex sub polygons.

Parameters:
None
Returns:
Array of Polygon convex_polygons
Convex polygons that form the original polygon.
Example:
convex = concave_polygon:splitConvex()
function love.draw()
    for i,poly in ipairs(convex) do
        love.graphics.polygon('fill', poly:unpack())
    end
end
function polygon:mergedWith(other)^ top

Create a merged polygon of two polygons if, and only if the two polygons share one edge. If the polygons share more than one edge, the result may be erroneous.

This function does not change either polygon, but rather create a new one.

Parameters:
Polygon other
The polygon to merge with.
Returns:
Polygon merged
The merged polygon, or nil if the two polygons don't share an edge.
Example:
merged = p1:mergedWith(p2)
hardoncollider.spatialhash^ top
spatialhash = require "hardoncollider.spatialhash"

A spatial hash implementation that supports scenes of arbitrary size. The hash is sparse, which means that cells will only be created when needed.

Module overview

Spatialhash
Spatial hash class.
hash:cellCoords()
Get cell coordinates of a given vector.
hash:cell()
Get cell for a given vector.
hash:insert()
Insert object.
hash:remove()
Remove object.
hash:update()
Update object's position.
hash:getNeighbors()
Query neighbors of an object.
Spatialhash(cell_size)^ top

Create a new spatial hash given a cell size.

Choosing a good cell size depends on your application. To get a decent speedup, the average cell should not contain too many objects, nor should a single object occupy too many cells. A good rule of thumb is to choose the cell size so that the average object will occupy one cell only.

Parameters:
number cell_size (100)
Width and height of a cell.
Returns:
Spatialhash
A fresh object instance.
Example:
hash = spatialhash.Spatialhash(150)

As with Polygon(), you can call the module as a shortcut to the above:

hash = spatialhash(150)
function hash:cellCoords(v)^ top

Get coordinates of a given value, i.e. the cell index in which a given vector would be placed.

Parameters:
vector v
The position to query.
Returns:
vector
Coordinates of the cell which would contain v.
Example:
coords = hash:cellCoords(vector(love.mouse.getPosition()))
function hash:cell(v)^ top

Get the cell a given vector would be placed in. This is an actual cell, not the index.

A cell is a table which's keys and value are the objects stored in the cell, i.e.:

cell = {
    [obj1] = obj1,
    [obj2] = obj2,
    ...
}
You can iterate over the objects in a cell using pairs():
for object,_ in pairs(cell) do stuff(object) end

Parameters:
vector v
The position to query
Returns:
table
Set of objects contained in the cell.
Example:
cell = hash:cell(vector(love.mouse.getPosition()))
function hash:insert(obj, ul, lr)^ top

Insert an object into the hash using a given bounding box.

Parameters:
mixed obj
Object to place in the hash. It can be of any type except nil.
vector ul
Upper left corner of the bounding box.
vector lr
Lower right corner of the bounding box.
Returns:
Nothing
Example:
hash:insert(shape, vector(-100,-100), vector(0,-30))
function hash:remove(obj, ul, lr)^ top
function hash:remove(obj)

Remove an object from the hash using a bounding box.

If no bounding box is given, search the whole hash to delete the object.

Parameters:
mixed obj
The object to delete
vector ul
Upper left corner of the bounding box.
vector lr
Lower right corner of the bounding box.
Returns:
Nothing
Example:
hash:remove(shape, vector(-100,-100), vector(0,-30))
hash:remove(object_with_unknown_position)
function hash:update(obj, ul_old, lr_old, ul_new, lr_new)^ top

Update an objects position given the old bounding box and the new bounding box.

Parameters:
mixed obj
The object to be updated.
vector ul_old
Upper left corner of the bounding box before the object was moved.
vector lr_old
Lower right corner of the bounding box before the object was moved.
vector ul_new
Upper left corner of the bounding box after the object was moved.
vector lr_new
Lower right corner of the bounding box after the object was moved.
Returns:
Nothing
Example:
hash:update(shape, vector(-100,-100), vector(0,-30),
                   vector(-100,-70), vector(0,0))
function hash:getNeighbors(obj, ul, lr)^ top

Query for neighbors of an object given it's bounding box.

Parameters:
mixed obj
The object to query neighbors.
vector ul
Upper left corner of the object's bounding box.
vector lr
Lower right corner of the object's bounding box.
Returns:
Array
An array of neighboring objects.
Example:
local others = hash:getNeighbors(obj, vector(-100,-70), vector(0,0))
for _,other in ipairs(others) do
    obj:pushAway(other)
end
hardoncollider.vector^ top
require "hardoncollider.vector"

See hump.vector

hardoncollider.class^ top
require "hardoncollider.class"

See hump.class