This project has moved and is read-only. For the latest updates, please go here.

A call for help regarding collision detection.

Topics: Project Management Forum, User Forum
Sep 1, 2009 at 3:14 PM

Firstly, I'd like to express my utmost praise for the physics engine. I think it (and the dedication towards it by the people developing it) is great. Kudos to you all.

Secondly, I've got to say: I'm not great at programming (I don't enjoy it very much) yet I want to get the hang of using Farseer so I can use it as a sort of proof-of-concept for any other games.
So that brings me to my next query: I don't understand - and can't master - how the OnCollision event (and accompanying happenings) actually works. I've searched around the forum and seen lots of pseudocode examples but as they don't usually help me to the level I want. I was wondering if anybody would be able to use one of the samples provided in the simple samples demo (let's say Demo 2, for good measure) to show me how OnCollision should function..?

I don't quite understand the proper syntax for the OnCollision event and whenever I try and test it using the examples here I end up getting (seemingly) silly errors. So yeah, that's why I was asking if I could have it layed out to me through the use of Demo 2. Perhaps (just to set some parameters) the game might end when the box collides with the circle. Whenever I've tried using examples provided here I've run into errors simply in the syntax where apparently I've done something wrong yet with nothing to compare it to I can't quite remedy the situation (right now I can't provide a list of said bugs, sorry).

 It does seem a bit much to get a custom example but it sure would be mighty handy, heh. Thanks in advance for any support provided!

Sep 1, 2009 at 7:36 PM

Thanks for the kinds words. I'll try to explain it in details here:

The OnCollision member of the Geom class is what is called a delegate. A delegate can be seen as a list of methods that needs to be called whenever the delegate is invoked. To use the OnCollision delegate, you simply subscribe (add your method to the list) to it by doing the following:

myPlayerGeom.OnCollision += MyCollisionMethod;

Inside the engine it runs some modules to make it all work. One of the modules (called narrow phase in this case) invokes the delegate and thus calls all the methods that are subscribed to the delegate. In the example code provided above, the delegate calls MyCollisionMethod.

But that method needs to have a specific signature (specific return value and arguments) and it looks like this:

public bool MyCollisionMethod(Geom geomA, Geom geomB, ContactList contactList)
    return true;

So each time "myPlayerGeom" collides with something else, your method called MyCollisionMethod gets called. The geomA argument contains a reference to myPlayerGeom and the geomB argument contains the other geometry (the ground, another player or something else). The contactList argument contains the contacts between the two geometries. In simple games you don't even have to use the contactList argument.

As you see, I return true inside the method. This is needed to tell the engine if you want a collision response or not. If you return true, the two geometries will collide and get separated. Return false will make the two geometries pass each other (collision does not get registered).

Tell me if you still need an example and I will edit Demo2 to show how it works. It is really more simple than it sounds.

Sep 2, 2009 at 8:30 AM
Edited Sep 2, 2009 at 3:45 PM

Thanks for the nifty and in-depth reply, genbox.

See, I do understand the principles behind the OnCollision delegate (as I now know to call it, heh) as I've been treating the Farseer manual as a Bible recently but I still don't quite understand how to actually use it. It's frustrating as I do understand the logic but in terms of execution I can't make it work properly. If it isn't too much to ask, I might take you up on the offer to see a modified Demo2. I just want to be entirely sure of how the delegate should be used, you see?


EDIT: I forgot to express my thanks for the second time (as I actually feel really guilty for asking for such dedicated help when the problem is probably stupidly obvious and sigh-worthy): thanks again for the help!!

Sep 2, 2009 at 9:57 PM

Okay, just to state some of the problems and queries I have (in the case that somebody else can also help me):

  • I'm not even entirely sure how to treat the initial OnCollision code. I'm often told that it won't work because the + operator cannot be applied to the method group or collision event handler.
    _rectangleGeom.OnCollision += OnCollision; 
  • The above code, placed in a class called entity causes some problems in the code (causing the above error). Why is this..?
  • Does it matter entirely where it is placed? If so, how does the placement change the execution of the code?
  • Are there any other components that are necessary for the use of the OnCollision event?

See, this is the last piece to do before I could use this as a proof-of-concept (the engine, that is) and it's really frustrating me as I know it's such a simple code (I just can't get it right or working in my code when applied as examples suggest).

Sep 2, 2009 at 10:11 PM

Sorry for not getting back to you sooner.

I created a quick sample using Demo2 (From simple samples) to demonstrate the use of the OnCollision delegate.

You can fetch it from my SkyDrive here.

Sep 2, 2009 at 10:48 PM

Ohhh-ho-ho, wow. I sure was close to getting this - it was even worse than I thought, heh. It makes all the difference to be able to see how the code operates though.

Thanks a lot for this genbox. Your help will not be ill-spent and neither will your generosity!