Having finally got my hands on 2.0, I was pretty psyched to try out the new contact listeners feature. But... after figuring out how they work, it seems like in many cases the old way of parsing contacts (looping through a body's contact list) seems easier.
I can see one advantage of listeners - detecting if a contact is added and destroyed between time steps. Very useful for fast moving bodies. I'm hoping someone can enlighten me if there are other advantages. Is there anything wrong with doing things the old way?
Yes, well, if you really don't care about those contacts... are there other advantages?
Ok, well, if you really don't care about "clean consistent code" then, are there other advantages?
Just kidding. 😀
When I first heard about "contact listeners" this is how I assumed they would work: Each body would be a contact-event-dispatcher, and other objects (or the body itself) could register themselves as contact-event-listeners. The listeners could implement body-specific collision response code. They don't care about other collisions in the world - they only care about collisions happening on the specific body they're listening to.
From what I've gathered so far (please correct me if I'm wrong) there's only one (all-mighty) listener, and it handles every collision in the world. It seems like this sort of breaks encapsulation... you've got to depend on this one all-mighty listener to dispatch the correct collision-response code.
In actionscript, for bodies I found it convenient to implement an "enterFrame" function that synced with the time step. Within this function, the body could loop through it's contact list and respond to each collision however it liked. It didn't depend on anything else to get the job done.
Now that I think of it... it seems like it would be rather easy to implement a body-specific dispatcher / listener over the all-mighty one.
There aren't significant advantages to a contact listener over a contact list, in my opinion. It's just a natural result of implementing CCD. It does represent whats going on much nicer.
The problem is, though it seems much neater to me to handle collisions as they come, via a listener, you cannot effect the game world in the listener callbacks*, so you end up buffering something regardless of what you try and do. The manual explicitly suggests you should manually buffer all collisions, which seems barely different from saying "everyone should do their own implementations of GetContactList, as box2d is too lazy to do it for you". It says we should "only buffer the points you care about", but in practise I do not want to write code in two different places, one to determine if I should be doing something with a given contact, and another to do it.
I don't consider having a single listener object to be "breaking encapsulation". If you care about that sort of thing (and you should), then the listener should just have some short code that dispatches further events to each game object involved in that collision. This is much nicer than having every body loop seperately through the contact list, which is probably a significant overhead in Actionscript.
I think of the contact listener more as a callback than a listener 😕
*Though I'd bet you can actually do what you like, as long as you don't delete anything, despite what the manual says.
One nice thing about a central listener is that you only process the contact once, not for each body. This can simplify some game logic.
I could buffer all the contacts for you, but that would take some potential memory/speed optimizations off the table.
Box2D keeps b2Contact objects even when the point count is zero. This aspect is used for continous physics. It's used to keep track of swept AABBs that overlap even when the shapes don't touch (yet).