Getting point of collision?

andrewaah
Posts: 6
Joined: Tue Sep 25, 2007 1:56 pm

Getting point of collision?

Post by andrewaah »

I'm using spheres, and I want to know the point of collision.

I'm using this code in the callback,

Code: Select all

void Physics::defaultNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, btDispatcherInfo& dispatchInfo) {
	btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
	btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;

	if(dispatcher.needsCollision(colObj0,colObj1)) {
		if(!collisionPair.m_algorithm) {
			collisionPair.m_algorithm = dispatcher.findAlgorithm(colObj0,colObj1);
		}

		if(collisionPair.m_algorithm) {
			btManifoldResult contactPointResult(colObj0,colObj1);

			if(dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE) {
				collisionPair.m_algorithm->processCollision(colObj0,colObj1,dispatchInfo,&contactPointResult);
			} else {
				float toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);

				if(dispatchInfo.m_timeOfImpact > toi) {
					dispatchInfo.m_timeOfImpact = toi;
				}
			}
		}
	}
}
Where can I get the collision point?

Thanks
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Getting point of collision?

Post by Erwin Coumans »

andrewaah wrote:I'm using spheres, and I want to know the point of collision.

I'm using this code in the defaultNearCallback [...]

Where can I get the collision point?

Thanks
You can use contactPointResult to get to the contact point.

One way is to use

Code: Select all

btPersistentManifold* contactManifold = contactPointResult.getPersistentManifold();
if (contactManifold)
{
   int numContacts = contactPointResult.getPersistentManifold()->getNumContacts();
   for (int i=0;i<numContacts;i++)
   {
      contactPointResult.getContactPoint(i);
      btManifoldPoint& point = getContactPoint(i);
   }
}
For certain collision shapes (btGImpactShape for example) there might be multiple contact manifolds, so you won't get all points using above method.

Another way, if you are familiar enough with C++, is to derive your own class from btManifoldResult, and override the virtual void addContactPoint to get instant access to the contact point while it is added.

However, in general you can better avoid interleaving huge amount of user code/logic interrupting the collision detection. Instead of adding logic to the nearCallback, you can also get all contact points by iterating over all contact manifolds after the collision/simulation step. See Demos\CollisionInterfaceDemo\CollisionInterfaceDemo.cpp.
[/code]

Hope this helps,
Erwin