Since I had a library like this in mind for some time now, and learned a lot programming my dynamic light demo. This thread got me to finally pull myself together and write something. Thus I present you:
HardonCollider(name shamelessly stolen from tentus)
What it does: Detect collisions.
What it does not: Respond to collisions (but it gives you some advice on how to respond).
How it works:
Like with love.physics, you
- set up some callbacks, one for the start of a collision, one for the duration of a collision and one for the end of a collision,
- define shapes that can collide (currently polygons and circles),
- update the state once in a while.
Unlike with love.physics, you don't have "bodies", so you have to take care of moving and spinning the shapes around yourself. Also unlike love.physics, your polygon shapes can have more than 8 vertices and even be
concave (that was actually pretty hard to do, but the result is a Polygon class that can triangulate polygons among other things).
The process would look like this (see the attachment for a real example):
Code: Select all
hc = require 'hardoncollider'
-- shape_a and shape_b are the two shapes that are colliding
-- sep_x, sep_y is a "separation vector", i.e. the direction you will have to move
-- the shapes that they will not collide anymore
function callback_start(dt, shape_a, shape_b, sep_x, sep_y)
print("start collision", shape_a.id, shape_b.id, sep_x, sep_y)
end
function callback_persist(dt, a,b, dx,dy)
print("still colliding...", a.id, b.id)
end
-- no separation vector here, since the shapes stopped colliding
function callback_stop(dt, a,b)
print("stopped colliding", a.id, b.id)
end
function love.load()
-- the first parameter is the grid size used internally for speeding things up
hc.init(100, callback_start, callback_persist, callback_end)
shape_a = hc.newCircleShape(100,100, 50)
shape_b = hc.newPolygonShape(0,0, 100,0, 100,100, 0,100)
end
function love.update(dt)
-- move shapes around
hc.update(dt)
end
function love.draw()
-- draw stuff
end
Future work
I will continue to work on this thing. Feel free to participate in writing code, commenting on idiotic functions or requesting features. Anyway, here are some things that will be done:
- Documentation of the whole thing. That probably includes a technical explanation of how the library works in addition to a tutorial and a reference.
- Add groups - a shape can be in different groups. Shapes in the same group won't collide with each other.
- Include point-in-shape and ray-intersection tests (for bullets and stuff).
- Refactor the code.
Finally...
You can view and download the library at github:
https://github.com/vrld/HardonCollider
The attached demo features some crappy collision response, but also shows the various features (like the concave rotating stars). You can create new shapes by placing points with the left mouse button and placing them by clicking the right mouse button. This will not always work though (for example, the shape is not allowed to intersect itself).