A safe way to check of two objects have separated.

Post Reply
Wil McV
Posts: 18
Joined: Tue Aug 26, 2014 5:46 am

A safe way to check of two objects have separated.

Post by Wil McV »

Hi All,

I'm currently writing some code to deal with collisions. Ideally I want to know if two objects have just collided, are still colliding or have just separated.

What i've currently done is registered for a post step callback so that I can check the state of the overlapping pairs. I iterate over them all and push them into a container. Its fairly straight forward to check for objects that are have just collided or are currently colliding by iterating over each manifold and seeing if it was in the container from the previous frame, if so its still colliding other wise its a new collision.

The thing I am concerned about how ever is detecting which objects were colliding previously but have separated in the last simulation step. From what I have gathered when two objects are no longer overlapping within the broadphase they wont have a manifold within the worlds collision dispatcher. I'd like to be able to check the manifolds from the previous frame against the ones in the current frame, if a manifold was present in the previous frame but isn't in the current frame then there was a separation.

So my question is : Is it safe to store a list of manifolds from a given frame and compare it with the list of manifolds from the the next frame?

below is some code similar to what im doing to illustrate my point

Code: Select all

const int num_manifolds = world->getDispatcher()->getNumManifolds();
for( int i = 0; i < num_manifolds; ++i )
{
     btPersistentManifold* current_manifold = world->getDispatcher()->getManifoldByIndexInternal(i);

    if( current_manifold->getNumContacts() > 0 )
    {
      currently_colliding_pairs->insert(current_manifold);
    }

    if( previously_colliding_pairs.contains(current_manifold) )
    {
         //do something related to pairs that are still colliding
    }
    else
    {
        //do something related to pairs that are new collisions
    }
}

for( int i = 0; i < previously_colliding_pairs; ++i )
{
    btPersistentManifold* current_manifold = previously_colliding_pairs[i];
    
   if( !currently_colliding_pairs.contains(current_manifold) )
   {
       //Do something related to pairs separating
   }
}

////////////////////////////////////////////////////////////////////////
///// Will it be safe to check pairs in this list next frame??   //////
////////////////////////////////////////////////////////////////////////
previously_colliding_pairs = currently_colliding_pairs;
currently_colliding_pairs->clear();
Thanks,
Wil McV
Wil McV
Posts: 18
Joined: Tue Aug 26, 2014 5:46 am

Re: A safe way to check of two objects have separated.

Post by Wil McV »

Bump,

How have other people done this?
Post Reply