HardonCollider^ top

HardonCollider is a Lua module to simplify one important aspect in computer games: Collision detection.

It can detect collisions between arbitrary positioned and rotated shapes. Those shapes can either be circles or polygons. Any non-intersecting polygons are supported, even concave ones.

The main interface is simple:

Download

You can download the latest packaged version as zip- or tar-archive directly from github. In that archive there will be a directory called vrld-HardonCollider-<numbers> - you probably want to rename that to something shorter (hardoncollider comes to mind).

You can also have a look at the sourcecode online here.

If you use the Git command line client, you can clone the repository by running:

git clone git://github.com/vrld/HardonCollider.git

Once done, you can check for updates by running:

git pull

from inside the directory.

First steps

This is an example on how to use HardonCollider. One shape will stick to the mouse position, while the other will stay in the same place:

HC = require 'hardoncollider'

-- array to hold collision messages
local text = {}

-- this is called when two shapes collide
function on_collision(dt, shape_a, shape_b, mtv_x, mtv_y)
    text[#text+1] = string.format("Colliding. mtv = (%s,%s)", 
                                    mtv_x, mtv_y)
end

-- this is called when two shapes stop colliding
function collision_stop(dt, shape_a, shape_b)
    text[#text+1] = "Stopped colliding"
end

function love.load()
    -- initialize library
    Collider = HC(100, on_collision, collision_stop)

    -- add a rectangle to the scene
    rect = Collider:addRectangle(200,400,400,20)

    -- add a circle to the scene
    mouse = Collider:addCircle(400,300,20)
    mouse:moveTo(love.mouse.getPosition())
end

function love.update(dt)
    -- move circle to mouse position
    mouse:moveTo(love.mouse.getPosition())

    -- rotate rectangle
    rect:rotate(dt)

    -- check for collisions
    Collider:update(dt)

    while #text > 40 do
        table.remove(text, 1)
    end
end

function love.draw()
    -- print messages
    for i = 1,#text do
        love.graphics.setColor(255,255,255, 255 - (i-1) * 6)
        love.graphics.print(text[#text - (i-1)], 10, i * 15)
    end

    -- shapes can be drawn to the screen
    love.graphics.setColor(255,255,255)
    rect:draw('fill')
    mouse:draw('fill')
end

License

Copyright (c) 2010-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.