Simple, lightweight, general purpose collision detection

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by Robin »

Taehl wrote:... More discussion, please! I really want to know what I'm supposed to be making!
A forum is perhaps not the greatest place for such a discussion. Perhaps a wiki? It also helps to get a project up on GitHub or BitBucket so everyone can contribute some code if they want, having some code in public is a good way to kick start deeper discussion about architecture and API.
Taehl wrote:How feature-full will it be - just something that'll work for basic platformers, or do mass, friction, etc. enter into it?
We could do something similar to SECS: having multiple variants, with more basic ones and more advanced ones. That might prove to be unmaintainable, so it would be good to get some community consensus on it.

There is a huge difference between platformer physics and regular physics, so this is an important decision.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by kikito »

to me, a "collision detection lib" should handle just that - collisions. Friction is related with them, but it isn't "really" collisions. similarly, I think it should return the contact information (coordinates, angle) but it should be up to the library userto decide how the objects "react" - it should not attempt to set their velocities.

The shapes should move and rotate, at any speed. Box2d has the "is bullet" flag so you can saved up the extra cycles on the slow objects.

It would be useful to be able to move the objects around without intersecting - either with a "move" method or a "reset to not intersecting state" one.

It would also be nice to beable to say "these two shapes don't intersect", maybe with groups and masks, like box2d.

For shapes, I think we need at least rectangles and triangles. Circles and polygons would be nice. I personally don't like segments-too many special cases. But being able to trace rays - for bullets, sight, etc - would be nice but not core.
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by Taehl »

If shapes have to be able to rotate, then I'm out - that math is way beyond me.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by Robin »

kikito wrote:to me, a "collision detection lib" should handle just that - collisions. Friction is related with them, but it isn't "really" collisions. similarly, I think it should return the contact information (coordinates, angle) but it should be up to the library userto decide how the objects "react" - it should not attempt to set their velocities.
But what would a lover want, if not having objects bounce off each other? Too me, it sounds a bit like saying "To me, a "widgets library" should handle just that — widgets. It should be up to the user of the library to decide how to draw them."
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by vrld »

Robin wrote:But what would a lover want, if not having objects bounce off each other?
But how does she want that to happen? Should they bounce elastically? Inelastically? Or should the just stop moving?
Collision detection and collision Collision response are not the same.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by kikito »

Robin wrote:But what would a lover want, if not having objects bounce off each other?
Most of the collision detection applications I can think about *don't* involve bouncing. From the top of my head:
  • a "entityOnFloor" and "entityHitPlayer" functions for platformers
  • "projectileHit" function for shooters
  • "EntityOnZone" for strategy games
When I write def I mean function.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by vrld »

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).
Attachments
hardon.love
(15.36 KiB) Downloaded 138 times
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Simple, lightweight, general purpose collision detection

Post by thelinx »

hardonbug.png
hardonbug.png (11.25 KiB) Viewed 2008 times
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by vrld »

vrld wrote:The attached demo features some crappy collision response
No bug there... It detects the collision ;)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Simple, lightweight, general purpose collision detection

Post by TechnoCat »

Are the separation parameters in the callbacks the components of the result?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest