Suggestions for improving the contact callback system

I'm not making any promises, there's a lot of new code needing to be ported at the moment.
You can always port things yourself if desperate, that would certainly save me some effort. This particular code is not yet in the C++ version either yet, so I'd hold off until it's finalized.

Of course as no one seems to be commenting on this or using it, it's a bit hard for me to know what to improve or if it is done.
A few things:

1) The changes you've made look good to me, though I haven't had a chance to try them out very much yet so I can't say for certain if it's "done" or not. But feature-wise, it definitely looks good, I'll let you know when I have a chance to actually test it out, maybe with a testbed example (jump through floors would be a good one, I think, speaking of which, we could probably use a generic platformer demo...if nobody beats me to it, once I'm done with my current contract I'll look into putting one together).

2) Don't bother with the late filtering stuff; late sensors covers what people have been asking for, and anyone that knows enough to know the difference between that and late filtering probably knows enough to implement it themselves if they're so sure that they really need it. 🙂

3) Perhaps at some point in the future we should try to put together a porting guide to try and increase involvement in spreading feature changes between the ports. I know in Java a lot of people have been requesting features from either C++ or AS3, and I just haven't had the time to port them myself. But I suspect that people are mostly hesitant to give it a go themselves because once it gets even a little bit non-trivial you quickly run into unexpected problems that you might not have thought to look for initially (for one, copy-on-assign vs. reference semantics when going C++ to a reference-based language can be a real killer, for one, as it's impossible to notice visually, and unless you've spent hours tracking down those types of bugs or been pre-warned about it, it's highly likely that you'll make the mistake). So if we could point people towards maybe a wiki page that listed some of the common pitfalls and gave a basic step-by-step approach, it might help.
2) Filtering's actually much more complicated. Which is why I haven't just gone straight ahead. Filtering would involve editing box2d's structure while iterating over it, while sensors are just a flag.

3) Porting does have a few caveats, to the point I do not like the idea of putting up a primer on how to do it easily. I tell people to port stuff mainly in the expectation most people will refuse. But it only takes one in ten users to do a bit of work like that to make a large load a small one, and I'm sure there are talented multi-platform programmers out there, or at least competent enough to understand the porting process.

I do try now not to provide features in AS3 but not port them myself to C++.
I have moved from Blitzmax (basic language variant) to AS3, and while I can port Java stuff reasonably well, C++ can have a lot of gotchas that need an experienced coder to do his job.

I would love to help, I just lack the experience to do so at the moment. I will often get stuck not understanding how to translate a couple of things.

Back to the question posed by Boris:

The solution should allow a higher level wrapper or interface where a developer can go:

if (shape1.collided(shape2,optional index)... directly check a collision between two
if (shape1.collidedCategory(0x0002,optional index)... return true if collided with this category

...And have a bunch of data.GetNormals(index) etc features for tracking. Index here is dynamic, and is just the number of objects it's colliding with so you can loop through if you wish.

The second one is the most important thing for developers and even if you don't impliment the rest, the ability to check just if a shape is interacting with a CATEGORY will vastly, vastly ease the game development process. Collision filtering + simple checks is the way forward.

These are just issues I run into making my game, which isn't at all simple or small, but I believe typical.
hippocoder wrote:
I would love to help, I just lack the experience to do so at the moment. I will often get stuck not understanding how to translate a couple of things.

No worries.

hippocoder wrote:
if (shape1.collided(shape2,optional index)... directly check a collision between two
if (shape1.collidedCategory(0x0002,optional index)... return true if collided with this category.

Oh yes, I was forgetting this request, this definitely needs sorting out before finalizing.

I see a few issues that need to be addressed, some with your proposal.
1)
"Collided" suggests that the method returns true if the shapes collided at any point in the previous step. This is actually quite a lot of work to store, I think, which is why it's been avoided in the first place. Would "IsTouching" work just as well?

2)
Even in that case, such functions are really just convenience wrappers for the contact list, which must be linearly iterated over. Currently Box2D doesn't store any data structures to quickly access this, and it could be difficult to do so efficiently. I'm not sure what sort of index you had in mind, but I think that sort of thing is ugly, people should not have to do manual caching. On the other hand, it is rare to have many contacts on a single shape, so perhaps linear iteration is fine as is.

Box2D doesn't use the STL, which makes a lot of caching annoyingly hard to do, as otherwise I'd just say put in a
Map<Tuple<b2Shape,ContactID>,b2ContactPoint>
in.

3)
Exposing the contact list. Having a binary test function is one thing, but I think it is equally important to be able to loop over all currently touching shapes. I can see a few ways of doing this:
a) Expose the underlying Box2D contact list. This is how it used to be, with GetContactList / m_contactList. This is dandy, but it kept tripping people up that it had speculative contacts in, and had this manifold/contact point structure that most people didn't want or understand. Perhaps better documentation would help, but is always going to have some usability issues.

b)Expose an iterator to the list. This can eliminate speculative contacts, and provide some help in browsing the list. The down side is that Box2D makes do without explicit iterator objects at the moment, preferring linked lists, so it'd be a bit of a depature.

c) Do a), but provide a GetNext() method that skips speculative contacts.

d)A function, or family of functions, to fill an array with info. I don't like this, but I don't like providing just a few options in any list of things 😉.

4)
Events are currently called during the world step, when everything is locked. People in general don't like this, despite the utility of pre-collision state being available for inspection.
I think Box2D could do with a canonical buffering event listener class, and possibly some API to smooth over it's use. Or alternatively, we could expand the event system further, having equivalent events that are buffered and not. The latter could be neater as certain things, such as the late filtering implemented above, cannot be buffered, while other things, like contact results, have little utility in not buffering.
Or we could try to lift the global ban on editing the world. Certainly, a lot of things are safe, such as setting velocities. With work, it is plausible that most world operations could be acceptable.
So many options, I feel like a child in a candy store!

Some thoughts...

I don't think its necessary to know if it was a first contact, or a persistent contact because anyone can easily track this for themselves in code each loop with a flag of their own design.

So just knowing that things are touching is always good enough.

How its managed isn't as important as managing it fast. Tracking collisions will potentially be as expensive as solving collisions and this just will not do. Making it easier to use, is probably a matter of actually documenting it better, have more examples and so on...

Implimenting a check against categorybits is a good idea if it can be done globally and very fast. Most of the time people have simple issues such as "is this sensor colliding with my level?" and categorybit based checks (if fast) will solve a vast number of collision problems.

Therefore the main thing you should base your decisions on is keeping things simpler and faster. I believe its OK for people to optionally "roll their own" but there should be a simpler built-in and fast way to just check if shapes are touching other shapes, and if shapes are touching categories.

Let me know if I can do some testing for you along these lines as I am making my game now, and its a good time to get messy with collision stuff (working on my own collisions code as we speak).
Ugh, porting. I've been gnashing my teeth on the breakable body demo, trying to port TriangleMeshes to AS3 because that's the next change to be ported from C++ to AS3. Grumble grumble pointer arithmetic. I'm dying to get this patch into the AS3 branch along with Edge Shapes.
hippocoder wrote:
if (shape1.collided(shape2,optional index)... directly check a collision between two
if (shape1.collidedCategory(0x0002,optional index)... return true if collided with this category.

It would be trivial to write a ContactListener to store this information, but a waste of performance and memory if we forced everyone to store it. If this is such a common API request, I could write one up for the Wiki. I don't think it's a good idea to force everyone to store all that data, as you can achieve greater efficiency by reacting to collisions as they are detected.
This patch is awesome; I can't think of any changes I'd make to the interface.

Found a bug in b2SingleManifoldContact::Evaluate:
Code:
if (listener)
{
    return;
}

The condition got inverted; was originally listener == NULL.

Also, ContactStop and ContactRemove don't get called upon contact destruction, even if the number of manifold points is greater than 0. Is this by design?
Thanks for that, Hangar, that's really useful.

No, I intended for Remove and Stop to be always called, or else they are somewhat useless. I hadn't tested it empirically, but the inserted code in b2ContactManager::Destroy should ensure they still get called if the contact is destroyed. Are you saying this is not working?
Oh, I see that now. I haven't tested ContactPointRemove, but ContactStop doesn't appear to be working. I think you forgot to add a call to ContactStop in b2ContactManager::Destroy. Or maybe I'm using an old version of the patch or something.

Original thread: https://box2d.org/forum/viewtopic.php?t=1784