Near Callback Discard

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

Near Callback Discard

Post by Geometrian »

Hi,

I need to be able to test whether to let two objects collide after detecting whether they would. For example, if there are two separate cubes, the instant they actually start to intersect is when I decide whether to let Bullet continue processing the collision.

To this end, I have implemented a custom near callback, which I adapted directly from the default callback:

Code: Select all

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

	if (dispatcher.needsCollision(colObj0,colObj1)) {
		btCollisionObjectWrapper obj0Wrap(0,colObj0->getCollisionShape(),colObj0,colObj0->getWorldTransform());
		btCollisionObjectWrapper obj1Wrap(0,colObj1->getCollisionShape(),colObj1,colObj1->getWorldTransform());

		//dispatcher will keep algorithms persistent in the collision pair
		if (!collisionPair.m_algorithm) {
			collisionPair.m_algorithm = dispatcher.findAlgorithm(&obj0Wrap,&obj1Wrap);
		}

		if (collisionPair.m_algorithm) {
			btManifoldResult contactPointResult(&obj0Wrap,&obj1Wrap);

			if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE) {
				//discrete collision detection query
				collisionPair.m_algorithm->processCollision(&obj0Wrap,&obj1Wrap,dispatchInfo,&contactPointResult);
			} else {
				//continuous collision detection query, time of impact (toi)
				btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);
				if (dispatchInfo.m_timeOfImpact > toi) {
					dispatchInfo.m_timeOfImpact = toi;
				}
			}

			if (contactPointResult.getPersistentManifold()->getNumContacts()>0) {
				Objects::ObjectBase* obj0 = (Objects::ObjectBase*)(colObj0->getUserPointer());
				Objects::ObjectBase* obj1 = (Objects::ObjectBase*)(colObj1->getUserPointer());
				collision_happened(obj0,obj1);
			}
		}
	}
}
The if block beginning tested with "contactPointResult.getPersistentManifold()->getNumContacts()" I found experimentally to accurately determine whether a collision has happened. From there, the void-returning function "collision_happened" is called. What I would like instead is to make "collision_happened" return bool, and then continue processing or discard the collision based on that.

I probed a little bit, and it seems that the collision is already in the pipeline after "collisionPair.m_algorithm->processCollision(...)". Is there a way to calculate whether the objects overlap, and then let the decision to continue processing it happen within this near callback?

Thanks,
-G
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Near Callback Discard

Post by STTrife »

This is not a direct answer to your question, but I noticed in example code on the btGhostPair object, that you also had to check whether the distance was <0 in the contacts, so it seemed like you couldn't just assume that all the contact points that were in the manifold were actual collision points.
But it might be a superfluous check too, not sure, but you should probably look into that.
As for your main question: I have no idea, sorry :P