Page 1 of 2

Collision response libraries

Posted: Sun Feb 05, 2012 9:55 pm
by AaronWizard
I've seen libraries here for collision detection, but has anyone put something together for collision response?

I'm envisioning a library that you can feed objects to and, after an update function call, all moving objects are updated with sensical positions and you are allowed to give any objects involved in a collision a new velocity through a callback.

I've been trying the built-in physics engine, but I was hitting the limit on the number of bodies just for the wall tiles (game will be a top-down shooter). Even when I only make bodies for the edge wall tiles I'm only left with a limit of 500 or so for enemies and bullets (the total limit is 2048 apparently).

Re: Collision response libraries

Posted: Sun Feb 05, 2012 11:37 pm
by Taehl
Fizz and Fizz X are like that. Box2D (Love's native physics module) does resolving, too, though that may not be the tool you want.

Hardon Collider is popular, but I think it doesn't do its own resolving.

Re: Collision response libraries

Posted: Mon Feb 06, 2012 3:20 am
by TechnoCat
Taehl wrote:Hardon Collider is popular, but I think it doesn't do its own resolving.
It sort of does: http://vrld.github.com/HardonCollider/r ... tCallbacks
vrld wrote: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.
But you can of course ignore it and roll your own.

Re: Collision response libraries

Posted: Mon Feb 06, 2012 3:25 am
by MarekkPie
Response is a bit more specific per game than detection is. Do I want two asteroids to bounce off one another on collision, or do I want them to shatter? Do I want a projectile to destroy a whole plane, or do I want it to change its trajectory? I think that's why there isn't as much in the response category.

Re: Collision response libraries

Posted: Mon Feb 06, 2012 5:39 am
by tentus
Making objects respond as if they're solid in HardonCollider is about this hard:

Code: Select all

function love.load()
	collider = require("hardoncollider")(128, on_collide)
	shapes = {
		collider:addCircle(100, 100, 25),
		collider:addCircle(200, 200, 50),
	}
end
function love.update(dt)
	shapes[1]:moveTo(love.mouse.getPosition())
	collider:update(dt)
end
function love.draw()
	for i=1, #shapes do 
		shapes[i]:draw("line")
	end
end
function on_collide(dt, a, b, x,y)
	a:move(x,y)
end
Move the mouse over the circle to see it work.

Re: Collision response libraries

Posted: Mon Feb 06, 2012 10:09 pm
by AaronWizard
Taehl wrote:Box2D (Love's native physics module) does resolving, too, though that may not be the tool you want.
I'd love to use Box2D if it wasn't for that physics bodies limit. :?
MarekkPie wrote:Response is a bit more specific per game than detection is. Do I want two asteroids to bounce off one another on collision, or do I want them to shatter? Do I want a projectile to destroy a whole plane, or do I want it to change its trajectory? I think that's why there isn't as much in the response category.
For me the hard part isn't the "what's their new velocities" part. It's the "how do I separate these overlapping objects" part. The part where I have to handle edge cases like an object bumping against two walls at the same time. Or cases like objects A, B, and C are overlapping each other, and I can't just move them backwards because object D just moved to where B used to be. My dream library would make sure all these objects are separated, and when collisions did occur it would allow me to set new velocities for the colliders. Or allow me to destroy one or both colliders to handle Asteroids.

How does HardonCollider's minimum translation vector work. Is it just for the two objects involved in the collision, ignoring all other objects around them?

Re: Collision response libraries

Posted: Tue Feb 07, 2012 1:21 am
by TechnoCat
Only for the 2 objects colliding passed into the callback. If you would like, you can collect all the shapes colliding every frame and then do your own resolution. Effectively ignoring mtv_y and mtv_x.

Re: Collision response libraries

Posted: Tue Feb 07, 2012 1:54 am
by AaronWizard
TechnoCat wrote:If you would like, you can collect all the shapes colliding every frame and then do your own resolution.
But...that's the part I always suck at. :cry:

Re: Collision response libraries

Posted: Tue Feb 07, 2012 10:13 am
by MarekkPie
Again, going off the assumption that Hardon uses SAT, (which it looks like it does with all the references to convex polygons), here's a good visual tutorial that shows how SAT provides a minimum translation vector. Look at the arrows formed, and how one arrow gets highlighted when there is a collision.

Re: Collision response libraries

Posted: Tue Feb 07, 2012 9:47 pm
by AaronWizard
The minimum translation vector thing makes sense when it's just two objects that are colliding, or if objects only collide with the immobile terrain. But handling collisions between three or more objects is something I can't get my head around. And something I've never seen explained well on the Internet.