Imprecise Collision Detection With Manifolds

caiocsabino
Posts: 2
Joined: Fri Mar 12, 2010 10:21 pm

Imprecise Collision Detection With Manifolds

Post by caiocsabino »

Hi,

I am using Bullet just for collision detection and this article seemed to do exactly what I need: just detect if two shapes collide.

http://www.bulletphysics.org/mediawiki- ... nformation

Here is my code:

Code: Select all

//collision world initialization
void GameSceneFrame::CreateWorld()
{
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	btVector3 worldAabbMin(-1000,-1000,-1000);
	btVector3 worldAabbMax(1000,1000,1000);

	btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
		
	m_physicsWorld = new btCollisionWorld(dispatcher,broadphase,collisionConfiguration);
    
}

void GameSceneFrame::UpdateCollision()
{
	
    m_physicsWorld->performDiscreteCollisionDetection();
	

    int numManifolds = m_physicsWorld->getDispatcher()->getNumManifolds();
	for (int i=0;i<numManifolds;i++)
	{
		btPersistentManifold* contactManifold = m_physicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = const_cast<btCollisionObject*>(contactManifold->getBody0());
		btCollisionObject* obB = const_cast<btCollisionObject*>(contactManifold->getBody1());
        
        

		btTransform aTransform = obA->getWorldTransform();
		btTransform bTransform = obB->getWorldTransform();

		btSphereShape * aShape = static_cast<btSphereShape*> (obA->getCollisionShape());
		btSphereShape * bShape = static_cast<btSphereShape*> (obB->getCollisionShape());

		float aRadius = aShape->getRadius();
		float bRadius = bShape->getRadius();

		btVector3 diff = aTransform.getOrigin() - bTransform.getOrigin();

		float distance = diff.length();

		if(distance > aRadius + bRadius)
		{
			printf("\n buggy collision");
		}
        
		contactManifold->clearManifold();
	}

}

I made a simple test with two sphere shapes, and I update their position on every frame. But if you check the code above, the clause if(distance > aRadius + bRadius) should never hold true, since we are talking about two spheres, yet they happen about 90% of the time and the distance difference is quite big (about ~10%). My guess is that the manifolds are contact points for the broadphase collision, if that's the case, how can I check for collisions on all objects during the narrow phase? I checked the CollisionInterfaceDemo but it uses the contactTest method which requires me to pass an object, that wouldn't be very practical to my application...

Cheers,
jstolarz
Posts: 14
Joined: Thu Dec 09, 2010 9:32 pm
Location: Glendale, California

Re: Imprecise Collision Detection With Manifolds

Post by jstolarz »

As the wiki page you point to suggests, you want to loop over the number of contacts the manifolds return:

int numContacts = contactManifold->getNumContacts();
caiocsabino
Posts: 2
Joined: Fri Mar 12, 2010 10:21 pm

Re: Imprecise Collision Detection With Manifolds

Post by caiocsabino »

Thanks, mate.

I think when I copied the code I decided to try by only checking the manifolds and the result was quite ok, afterwards when I was debugging I noticed the discrepancies but forgot that I removed the contact iteration part.

Thanks!