getPersistentManifold() Returns NULL

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

getPersistentManifold() Returns NULL

Post by Geometrian »

Hi,

I have a compound object and I'm trying to collide it against various things. I can't seem to figure out why, but in certain situations, my code crashes since getPersistentManifold() is returning NULL. My near callback is below:

Code: Select all

void _fz_near_callback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo) {
	btCollisionObject* obj0_col = (btCollisionObject*)(collisionPair.m_pProxy0->m_clientObject);
	btCollisionObject* obj1_col = (btCollisionObject*)(collisionPair.m_pProxy1->m_clientObject);

	if (dispatcher.needsCollision(obj0_col,obj1_col)) {
		btCollisionObjectWrapper obj0_wrapper(NULL, obj0_col->getCollisionShape(),obj0_col,obj0_col->getWorldTransform());
		btCollisionObjectWrapper obj1_wrapper(NULL, obj1_col->getCollisionShape(),obj1_col,obj1_col->getWorldTransform());

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

		if (collisionPair.m_algorithm) {
			btManifoldResult contact_point_result(&obj0_wrapper,&obj1_wrapper);

			if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE) {
				//discrete collision detection query
				collisionPair.m_algorithm->processCollision(&obj0_wrapper,&obj1_wrapper,dispatchInfo,&contact_point_result);
			} else {
				//continuous collision detection query, time of impact (toi)
				btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(obj0_col,obj1_col,dispatchInfo,&contact_point_result);
				if (dispatchInfo.m_timeOfImpact > toi) {
					dispatchInfo.m_timeOfImpact = toi;
				}
			}

			//########CRASHES ON NEXT LINE SOMETIMES########
			if (contact_point_result.getPersistentManifold()->getNumContacts()>0) {
				Objects::ObjectBase* obj0 = (Objects::ObjectBase*)(obj0_col->getUserPointer());
				Objects::ObjectBase* obj1 = (Objects::ObjectBase*)(obj1_col->getUserPointer());

				//printf("%p %p\n",obj0,obj1);
				//getchar();

				for (std::set<World*>::const_iterator iter=obj0->_fz_parent_worlds.begin(); iter!=obj0->_fz_parent_worlds.end(); ++iter) {
					World* world = *iter;
					if (world->collision_callback!=NULL) {
						world->collision_callback(obj0,obj1);
					}
				}
			}
		}
	}
}
I could obviously add a test to prevent it continuing if the manifold is NULL, but I need to know why, and if that's the correct solution. What's going on here? What does the manifold being NULL mean?

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

Re: getPersistentManifold() Returns NULL

Post by Erwin Coumans »

You are not supposed to access the getPersistentManifold like that, it can be NULL indeed by design, it is an internal method.

There are better ways: you could iterate over all manifolds after the stepSimulation (or during a tick callback), or use a contact added callback.
Check out the docs, Wiki or the demos,
Thanks,
Erwin