How can I use the ContactListener?

Topics: User Forum
May 20, 2013 at 9:55 AM
I downloaded Farseer Physics Engine 3.3.1 Samples XNA(Recommended Download), but I can not find a class named ContactListener in the Farseer folder. How can I use the ContactListener when there is no ContactListener class in the Farseer folder?
May 20, 2013 at 11:17 PM
Hello,
you might want to access the contactList of a certain body.
So here's an example in which I use the contactList to cycle through the contacts of myBody:
            Dynamics.Contacts.ContactEdge cEdge = myBody.ContactList;
                do
                {
                    //Do sth with the bodies in contact
                    //Use cEdge.Contact.FixtureA.Body or cEdge.Contact.FixtureB.Body to access individual bodies

                    cEdge  = cEdge.Next;
                }
                while (cEdge != null);
May 20, 2013 at 11:56 PM
I want to implement this ContactListener in my code, but I don't know how to do that because this is a box2d example and not a Farseer example. I want to use the PreSolve method in the ContactListener.
box2d manual(9.4 ContactListener): http://www.box2d.org/manual.html#_Toc258082975
class MyContactListener : public b2ContactListener

{

public:

  void BeginContact(b2Contact* contact)

  { /* handle begin event */ }

 

  void EndContact(b2Contact* contact)

  { /* handle end event */ }

 

  void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)

  { /* handle pre-solve event */ }

 

  void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)

  { /* handle post-solve event */ }

};
Is it possible to use a similar ContactListener in Farseer? How can I do that?
May 21, 2013 at 12:08 AM
Maybe you can solve your issue with the EventHandlers, that exist for Separation and Collision.
This one for example, fires on collision and allows you to accept or deny to collision by returning true or false.
In the EventHandler, you also have access to the contact.
        public void LoadContent()
        {
           //Add Event
           body.OnCollision += new OnCollisionEventHandler(body_OnCollision); //IntelliSense loves them
        }

        bool body_OnCollision(Fixture fixtureA, Fixture fixtureB, Dynamics.Contacts.Contact contact)
        {
            return true;
        }
May 21, 2013 at 9:49 AM
Here's how I do it:
    // do this on whatever constructor / init function you have
    void Start()
    {
        // get the world singleton somehow
        World world = FSWorldComponent.PhysicsWorld;

        // register
        world.ContactManager.BeginContact += onBeginContact;
        world.ContactManager.EndContact += onEndContact;
        world.ContactManager.PreSolve += onPreSolve;
        world.ContactManager.PostSolve += onPostSolve;

        // NOTE: you should probably unregister on destructor or wherever is relevant...
    }

    bool onBeginContact( Contact _contact )
    {
        // ...
        return true; // or false to ignore the contact
    }
    void onEndContact( Contact _contact )
    {
        // ...
    }

    void onPreSolve( Contact _contact, ref Manifold _oldManifold )
    {
        // ...
    }
    void onPostSolve( Contact _contact, ContactVelocityConstraint _impulse )
    {
        // ...
    }
I use the Unity port of Farseer so your mileage may vary.