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);
}
}
}
}
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