[howto] handle only enter collision events

May 13, 2009 at 1:01 PM
Edited May 25, 2009 at 12:23 PM

in Farseer the OnCollsion() event is called everytime as long as two Geoms collide. to handle only enter collision events you have to manage a collection of colliding Geoms and ignore them in the next collision event.

class MyObject
{
    //private IDictionary<Geom, bool> CollidingGeoms = new Dictionary<Geom, bool>();
    private Set<Geom> Colliding = new Set<Geom>(); // NOTE: Set is a collection class from the "Power Collections" library

    ...

    private bool OnCollision( Geom self, Geom other, ContactList contactList )
    {
        if( !Colliding.Add( other ) )
        {
            return OnEnterCollision( self, other, contactList );
        }
       
        return self.CollisionResponseEnabled;
    }

    private bool OnEnterCollision( Geom self, Geom other, ContactList contactList )
    {
        // handle on enter ocllision
    }
   
    private void OnSeparation( Geom self, Geom other )
    {
        Colliding.Remove( other );
    }
}