Handling collisions: attaching events to fixtures seems wrong

Topics: Developer Forum, User Forum
Dec 3, 2010 at 10:37 PM

Put simply, I would prefer to have a single "bool OnCollision(Fixture a, Fixture b)" event handler that handles _all_ collisions. (World.ContactManager.OnCollision maybe?)

I think this would be a much cleaner design for 2 reasons:

1) A collision event is a single concept which involves two fixtures.

As an example, in my game a point can be scored by shooting a ball into a goal. In the current system, I have to arbitrarily choose which fixture to handle the collision on and explicitly ignore the collision on the other. This is messy and over complicated.

2) Most of the time, collision detection is handled by classes of objects, not per object.

In my game, there are many ships and many balls and many goals. It is unnecessary duplication to attach an event handler to each fixture when it would be both smaller and cleaner to have 1 small 2-level switch statement (or even a nice pattern match over a tuple in F#).

If the level of granularity provided by per fixture event handlers is needed. It can easily be implemented yourself with a Dictionary (or even some helper class provided by Farseer).

Developer
Dec 3, 2010 at 11:22 PM
det wrote:

In my game, there are many ships and many balls and many goals. It is unnecessary duplication to attach an event handler to each fixture when it would be both smaller and cleaner to have 1 small 2-level switch statement (or even a nice pattern match over a tuple in F#).

If the level of granularity provided by per fixture event handlers is needed. It can easily be implemented yourself with a Dictionary (or even some helper class provided by Farseer).

It works the other way around, too ...if you don't need that level of granularity you can just define one collision handler and add that to all your fixtures on creation. You still get "duplicate" collisions, but those shouldn't be too hard to sort out.

Coordinator
Dec 4, 2010 at 2:05 AM

You could also use the World.ContactManager.BeginContact delegate.