Page 1 of 1

Collision notification

Posted: Sat Oct 14, 2006 9:09 am
by Jack
Is there an ability to get notification (callback) when collision with particular object occurs?

Re: Collision notification

Posted: Sat Oct 14, 2006 7:03 pm
by Erwin Coumans
Jack wrote:Is there an ability to get notification (callback) when collision with particular object occurs?
It depends what you want to do with this:

If you only want to know the contacts/bodies overlapping with a certain body, you can use the btDynamicsWorld (btCollisionWorld) and iterate over all contact points (btPersistentManifolds) and intercept the objects that you are interested in, and check wether there are at least one contact point. The details are in CollisionInterfaceDemo:

Code: Select all

int numManifolds = collisionWorld->getDispatcher()->getNumManifolds();
	for (i=0;i<numManifolds;i++)
	{
		btPersistentManifold* contactManifold = collisionWorld->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
		btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
		contactManifold->refreshContactPoints(obA->m_worldTransform,obB->m_worldTransform);

		int numContacts = contactManifold->getNumContacts();
		for (int j=0;j<numContacts;j++)
		{
			btManifoldPoint& pt = contactManifold->getContactPoint(j);

			glBegin(GL_LINES);
			glColor3f(1, 0, 1);
			
			btVector3 ptA = pt.getPositionWorldOnA();
			btVector3 ptB = pt.getPositionWorldOnB();

			glVertex3d(ptA.x(),ptA.y(),ptA.z());
			glVertex3d(ptB.x(),ptB.y(),ptB.z());
			glEnd();
		}

		//you can un-comment out this line, and then all points are removed
		//contactManifold->clearManifold();	
	}

If you want to modify the friction/restitution of the contact points with that particular object, you can do that by enabling the CF_CUSTOM_MATERIAL_CALLBACK for the rigidbody, see ConcavePhysicsDemo for details:

Code: Select all

staticBody->m_collisionFlags |= btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK;
Notice that there is only a callback for new contacts, not for existing contacts ( so a resting box on a surface gets 4 contact callbacks, unless contacts gets removed and added again when it is sliding). A more user-friendly interface will probably be added, another todo.

Thanks,
Erwin