onCollision, how to use exactly..? please help a beginner

Feb 28, 2011 at 9:45 AM

im tryin to program a small game

i which i need to do certing things on collision

for example on collision with a body (circle) and another body(square for example)  i need to destroy the circle


on mouse click on circle i need to duplicate the circle


i managed to take a body (from the demo) and iterate over its fixture to register onCollision. but in the function i cant recognize what is hitting what...

for example when the body hits the border i have no idea how to check that this is the border.

moreover that way seems a bit weird.


and another small question

it seems the demo has some sort of a bug that when u click on an object u have to click a bit down of it to get the right point (when u click under it for example u catch it and when u click on the top u can miss it)


i would realllly realllly apprecciate any help!

thanks in advance


Feb 28, 2011 at 6:12 PM

To use OnCollision, hook to the body's event and then make use of the data that's available in the handler:

Body.OnCollision += Body_OnCollision; // or use a lambda

// ...

bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
    // fixtureA: The fixture of 'Body'
    // fixtureB: The fixture of the body that collided with 'Body'

    // return true : acknowledge the collision
    // return false : ignore the collision the collision


To find out which entity the fixture belongs to, I suggest you make use of the UserData property during construction.  That way, you can retrieve the data from the fixtures once inside the handler:

bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
    if (fixtureB.UserData is IBorder) {
      // your entity has collided with a border
      return true; // allow the collision to happen
   return false; // disallow the collision

The above example assumes that you previously set a concrete instance of IBorder when constructing the fixture.

Did that answer your question or did I misunderstand something? 

Feb 28, 2011 at 8:41 PM
Edited Feb 28, 2011 at 9:05 PM

you did answer my question but rose another one

i thought about it but couldnt find where to load userdata

for example the border which is build in the demo like this

            _border = new Border(World, gameWorld.X, gameWorld.Y, 1f);


couldnt find how to load USERDATA to it afterwards...


and can i bother you with another few small questions?

1) i have an object which should duplicate and have oncollision.. meaning that when they hit eachother the function will be called twice... is there away around it?

2) do i have to bypass all the fixture of a body in order to change the oncoliison? isnt there a onCollision for the entire body?


i hope im not asking too much

thanks alot for all the help!!


EDIT: ok i think i got the idea of the border now!!! XD its a warp for soemthing else!!! blah idiot me! but can you help me with the other question as well? and again do you as well have the bug in the demo where the selection catch is a bit lower than expected?

thanks again ~! :)

Feb 28, 2011 at 9:14 PM
Edited Feb 28, 2011 at 9:17 PM

As to what and when to set the UserData, I would suggest keeping consistency and not vary between the objects i.e. if you want to pass strings, always pass strings; ideally, you would want to pass in an instance of your game entity object.

And in regards to the question about setting an OnCollision handler for an entire body, checkout a fairly recent copy of the engine (the road to 3.3) and you'll be glad to hear that now you can use BodyFactory instead of FixtureFactory which thankfully returns a single Body rather than a collection of Fixtures (reference).

Here's an example:


var body = BodyFactory.CreateRectangle(world, width, height, density, position);

body.OnCollision += body_OnCollision; // and that's it!

You can also access the corresponding bodies of the fixtures from the the two fixture arguments that are passed in your event handler (hooked to OnCollision).

Feb 28, 2011 at 9:47 PM

first of all AWESOME! i starting to really love this engine! cant wait for the update

and about question 1 is there away to fix it? i thought about given a unique index to each and whoever is lower will do the action while the other ignores (in userdata ofcourse) but still both will trigger the function.. seems kind of weird

meanin when item A and another instance of item A hits... both will call the same onCollision function

and thanks again!~ :) im using this engine as part of a course in the university where we are currently trying to learn the basics of what makes a "good game" heh XD i hope this course will be as good as it sounds!

Feb 28, 2011 at 10:13 PM
Edited Feb 28, 2011 at 10:14 PM

If you don't want to wait for the official release, you can grab a copy of the source now and start playing around with it; it's very stable.

Oops, I forgot about your first question, sorry about that.

So, if you don't want certain bodies to collide with each other (in this case, maybe instances of A?), set the CollisionCategories to be a specific number and use CollidesWith to specify that you don't want bodies which are Cat9 (Collision Category 9) to not collide with other bodies with Cat9 as their category.

That way, you won't have collisions between bodies of the same collision category.  There is also a CollisionGroup you can make use of.

If on the other hand you still want them to collide, you can identify the instances by using the UserData of the fixtures passed in to the handler.


Good luck on your course mate ;-) 

Feb 28, 2011 at 10:24 PM

heh thanks

ok about the collision category, the problem is i want to do much more than true or false, i want to create an action (like duplication or fusion) or anything else.

so the problem i need to choose one of them to do the action, but they are the same instance and should act exactly the same. do you think i should break the symmetry with a simple unique id (the smaller one acts) or is there a smarter way to do it built in the system?

and again i asked it above im not sure if you saw it: is there a known issue with the mouse clicking of a body? it seems that in the DEMO all the selection are a bit lower than what expected.. meaning if you want to catch a dot you have to click a bit lower than where it is . or maybe i did something wrong (although i don't think i changed anything)

and thanks for the quick responses!