Limiting collision notifications

sipickles
Posts: 44
Joined: Thu Aug 07, 2008 6:57 am

Limiting collision notifications

Post by sipickles »

Hello,

I am detecting collision events by iterating over manifolds. Trouble is I get A LOT of collisions. In my test case, I have a single box which drops onto some terrain. Despite my best effort I still get 330 collisions detected by the following code

Code: Select all

    // Iterate over contact manifolds / points:
	int numManifolds = m_world->getDispatcher()->getNumManifolds();
	for ( int i=0; i < numManifolds; ++i)
	{
		btPersistentManifold* contactManifold = m_world->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
		btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

		int numContacts = contactManifold->getNumContacts();
        if ( numContacts )
        {
            btManifoldPoint& pt = contactManifold->getContactPoint(0);
            if ( pt.getLifeTime() > 1 )
                continue;
            
            int idA = 0;
            int idB = 0;
            if ( obA->getUserPointer() )
                idA = *(int*)obA->getUserPointer();
            if ( obB->getUserPointer() )
                idB = *(int*)obB->getUserPointer();

            // ACT ON COLLISION NOTIFICATION HERE

        }

		// Clear contact points to prevent new collisions
		contactManifold->clearManifold();	
	}
I appreciate the object is bouncing as it lands, but how can I pare down the number of notifications I receive?

Thanks

Simon
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Limiting collision notifications

Post by Flix »

sipickles wrote:Despite my best effort I still get 330 collisions detected
Strange thing ....

Some time ago I posted a modified "Basic Demo" (with source code) that forwards collision detection events (here: http://www.bulletphysics.org/Bullet/php ... php?id=343, in this forum thread: http://www.bulletphysics.org/Bullet/php ... 91&start=0).

You may try it and by modifying the initPhysics() method to reflect your physic environment ("a single box which drops onto some terrain", you said) you can check if the number of reported collisions is so high.

Hope it helps.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Limiting collision notifications

Post by Flix »

Ragarding the code you posted in the first thread, maybe you can try commenting out:
sipickles wrote:// Clear contact points to prevent new collisions
contactManifold->clearManifold();
and see what happens (the idea is that when 2 bodies collide, if you keep clearing the manifold, the contact points are recreated always with a lifeTime= 1).

You can also try the following modification, but I think it's probably useless:

Code: Select all

if ( numContacts )
        {
            bool fireStartCollisionEvent = true;
	    for (int i = 0;i<numContacts;i++)	{		
		    btManifoldPoint& pt = contactManifold->getContactPoint(i);
        	    if ( pt.getLifeTime() > 1 )	{fireStartCollisionEvent=false;break;}
	    }
	    if (!fireStartCollisionEvent) continue;
	    // Fire event here
	    [...]
	}
I haven't tested this code myself, so please report your results here, as the "lifetime" approach is probably faster than the one I used in my modified "Basic Demo", when you only need "starting collision" events.

On the other hand, my solution can fire "continuing collision" and "stop collision" events, can forward an "average contact point", and, by keeping collision lists internally, can be queried at runtime for collisions with specified bodies (much like ghost objects do); but this of course makes the code slower, more complicated and prone to errors...

Hope it helps.
sipickles
Posts: 44
Joined: Thu Aug 07, 2008 6:57 am

Re: Limiting collision notifications

Post by sipickles »

Hi Flix, thanks for your input.

I havent had time to try your example code yet, but unfortunately, not clearing the manifold doenst really affect the flow of contacts.

I get a continous flow of contacts all with a life time of 1.

If I change my test to

Code: Select all

            if ( pt.getLifeTime() == 2 )
I get no contact detected. So my collisions are not surviving from one frame to the next. I promise I definately have commented out

Code: Select all

//contactManifold->clearManifold();	
sipickles
Posts: 44
Joined: Thu Aug 07, 2008 6:57 am

Re: Limiting collision notifications

Post by sipickles »

Sorry, my mistake. My falling cube generates 12 new contacts per contact point, with clearManifold() active.

I guess thats the slight bouncing and is manageable.

Could I use the impulse to only acknowledge large impacts?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Limiting collision notifications

Post by Flix »

sipickles wrote:Could I use the impulse to only acknowledge large impacts?
I haven't tried it (my solution fires start and stop collision events based on a comparison between the "last collision list" and the "new collision list", so I don't use the impulse and I can have a limited number of start collision events due to bouncing).

You may try it, but I guess that it's not easy to estimate an "impulse threshold" that is good for objects of different shape/mass and for all the collision they make.

In my code, I simply "average" the contact points, so I don't get an event for every new contact point ( I mean, one thing is a contact point callback system, another thing is a colliding objects callback system, in my opinion ).

Maybe other users have tried it and can give you a better feedback about it.