Guaranteed Collision Callback

Geometrian
Posts: 25
Joined: Sat Dec 03, 2011 8:37 pm

Guaranteed Collision Callback

Post by Geometrian »

Hi,

I am attempting to figure out when two objects collide.

What I want:
Ideally, whenever a collision actually occurs, Bullet would call a callback, giving me the option to ignore the collision or allow Bullet to continue with collision response (restitution, reaction, etc.).

Some things close and my analysis thereof:

I found two pages of interest: Both are quite outdated as of May 2013, but I eventually was able to cobble together a method that I can use, working nicely in my revision of Bullet 2.81 (mostly from Page 1):

Code: Select all

//dynamics->stepSimulation above

int numof_manifolds = collision_dispatcher->getNumManifolds();
for (int i=0;i<numof_manifolds;++i)
{
	btPersistentManifold* contactManifold =  collision_dispatcher->getManifoldByIndexInternal(i);
	const btCollisionObject* obA = contactManifold->getBody0();
	const btCollisionObject* obB = contactManifold->getBody1();

	int numof_contacts = contactManifold->getNumContacts();
	for (int j=0;j<numof_contacts;++j) {
		btManifoldPoint& pt = contactManifold->getContactPoint(j);
		if (pt.getDistance()<=0.0f) {
			//->setUserPointer can be called when rigid bodies are created with an id that allows for their identification here
			printf("Collided: %p %p\n",obA->getUserPointer(),obB->getUserPointer());
			//getchar();

			//const btVector3& ptA = pt.getPositionWorldOnA();
			//const btVector3& ptB = pt.getPositionWorldOnB();
			//const btVector3& normalOnB = pt.m_normalWorldOnB;
		}
	}
}
The major problem with this is that a collision is not guaranteed to be detected. If two objects collide but are then completely restored all in a single stepSimulation call above, then the if-block might never be called. This can be largely resolved in practice by using some epsilon > 0.0f instead if 0.0f as the test, but I don't think it's really a fix.

The other options on Page 1 would suffer from the same problem, I think. Maybe the contact method with gContactProcessedCallback could do what I want? On Page 2, I tried the last suggestion of deriving btCollisionDispatcher and overriding needsResponse, but this seems to be called at times when the two objects do not collide.

Suggestions, tips, comments, etc.?
Ian
User avatar
pccsoares
Posts: 13
Joined: Mon Jan 20, 2014 11:57 am

Re: Guaranteed Collision Callback

Post by pccsoares »

I'm having the same problem.
Did you find a solution?
c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: Guaranteed Collision Callback

Post by c6burns »

Geometrian wrote: The major problem with this is that a collision is not guaranteed to be detected. If two objects collide but are then completely restored all in a single stepSimulation call above, then the if-block might never be called.
Isn't that the exact case for which CCD (continuous collision detection) is designed? There's a CCD demo that ships with the library.
User avatar
pccsoares
Posts: 13
Joined: Mon Jan 20, 2014 11:57 am

Re: Guaranteed Collision Callback

Post by pccsoares »

It's not a problem for CCD. I'm missing collisions even with slow moving objects.
The only way I found to get all the collisions is with the callback gContactProcessedCallback.