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

BeginContact vs PreSolve vs OnCollision

Feb 17, 2011 at 2:14 PM

Hi, I'm trying to mange some collisions and figure out the best way to cancel them when needed, and I need some advice/opinions.

The three mehtods I know for cancelling a collision (or a contact, as if there's no contact there's no collision) are World.BeginContact, World.PreSolve and Fixture.OnCollision

The problem I find with OnCollision is that it's called in both fixtures, so I have to cancel the collision in each one of them if I want to avoid it, adding the OnCollision delgate in both of them and checking the logic twice.

So this leaves me with BeginContact and PreSolve (by setting Contact.Enabled = false), and here's my doubt: Which one to use? Is there a reason why I should use one or another? I'm asking this because probably I'll move away all the code from my OnCollision delegates, and this means a lot of work, so I want to be sure :)

Mar 1, 2011 at 8:14 AM

One question about the OnCollision method. If I cancel the collision (returning false), I should cancel the collition on both fixtures, right? (meaning that I still should get a collision even if I cancel the collision in only one of the fixtures)

Mar 1, 2011 at 9:20 AM

i am  learning this myself right now

and i can say that i changed the onCollision to false for an entire body (all the list of fixtures) it no longer collided with anything. so i can say that one of them is probably enough.


Mar 1, 2011 at 9:39 AM

Thanks, for the answer, errdos, I'm assuming that you are using FPE 3.3, right? Can anyone confirm what errdos said? It seems it's not the same behaviour than in FPE 3.2, I'm still getting collision responses (the other fixture's OnCollision is still being called) :/

Mar 1, 2011 at 11:35 AM

i didnt mean that the function isnt being fired, but that if one of  the is false no collision will happen meaning that its not like on of the bounces while the other is not effected

im right now having the same problem myself ... the only workaround i could found is use ID and the one with the lower ID will act while the other will ignore.

hope that helps

Mar 1, 2011 at 12:34 PM

Thanks errdos, I see now... I think I'll move my code to the presolve method, where I can quickly cancel the collision on both objects with one check.