This project has moved. For the latest updates, please go here.

How to use collision (Beginner)

Topics: Developer Forum
Mar 7, 2011 at 1:22 PM


Sorry. I'm new to the current version.
I can't figure out how to use collision detection.
I got some examples like:
void Sprite__OnCreation(object sender, EventArgs e)
           this._Fixture.OnCollision += Sprite_OnCollision;

 bool Sprite_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)

But I don't know: how to call it, What is a contact, how to create a contact...
Is there a full example on how to use it? 
Thank you all. 


Mar 7, 2011 at 5:51 PM
Edited Mar 7, 2011 at 5:52 PM

No no, you don't pass in a 'contact'; it's not how it works; and this has nothing to do with the version of Farseer.

Those two methods you pasted are used as event handlers and they are used as hooks so they can be fired when "something happens".  Now, in this case, this "something" is the event called OnCollision and its provided by the Body class.

The following is an example how it can be used:

var body = BodyFactory.CreateRectangle(world, width, height, 1f, position);
body.OnCollision += body_OnCollision;

// ...

bool body_OnCollision (Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact) {
    // will be called whenever some other body collides with 'body'

Mar 8, 2011 at 12:03 PM

I understand now how it works.

It's really simple.

Thank you.