Suggestions for improving the contact callback system

Your ideas are great. They will make the library much easier for people to use, and improve the quality of simulation. Some more things to think about:

1) Updating the broadphase just flushes the pair buffer, which sort of confirms new contacts. If there's no manual access to contacts, then it would be relatively easy to lazily buffer pairs, if you want. I'm not sure if it will help, or how the new broadphase will change things.

Additionally, there are a couple of ways to do lazy contacts. Will you put the dirty bit on the shape or on the contact itself? Even if you have to iterate over the body's contact list for each change, putting it on the contact seems less likely to cause trouble.

3) Buffered remove events might become more confusing. If a user deallocates user data on OnContactStop immediately, a buffered version of an event that receives that user data may fail to work.

5) It seems like there are 2 different settings here: do buffering/no buffering, and also contacts evaluated only on the step, lazily, or eagerly. Without lazy or eager behavior, you'll need access to the primitive contact and ability to update manually in order to enable the same functionality.

6) Are the add/persist/remove methods still necessary without access to contacts and contact points? It's important to think about what you can remove from the API. Without them, and no access to primitive contacts, lazy behavior would be faster and produce the same result as eager behavior. In any scheme, the changes would have add/persist/remove called outside the step often. Maybe it would be better to call something explicitly before and after islands are solved.

7) Finally, there's the issue of additions to the library. If ghost bodies or soft bodies are added, how easy will it be to hook into contact reporting?
1) Yeah, true. I was expecting to put the dirty bits on the contacts themselves, as it's possible for some to be updated, but others dirty, even when connected to the same shape.

3) I'm talking about actually splitting events. So you'd (most likely) allocate on OnContactStart, and de-allocate on OnBufferedContactEnd, which will always be the strictly first and last events for a contact.

5) Splitting up the option is fair enough. People can understand binary decisions better. But, I'm not keen to give too many options though, this sounds like it would complicate the deal even further. I mean, the option to see dirty contacts itself is fine, but I'm concerned it would complicate the API, leaving users who have no intention of doing that wondering what all these extra methods are for.

6) I don't get this point, you'll have to elaborate.

7) I cannot see how soft bodies might be effected by this. It does make ghost bodies easy to use (add, update contacts and read, delete), but I think those would actually be implemented in a different way, avoiding putting them in the broadphase at all.
I haven't fully thought out (6). Add/Persist/Remove, and I just realized Start/Stop, will be called outside of the step at many different times, either when you change state with eager, or less predictably when you query it with lazy. If it's just Add/Persist/Remove, those calls could probably be restricted to the step, but the introduction of user data with Start/Stop makes that less appealing.
I'd like to divide up what I was saying into different segments - I realized I was making the whole thing sound all or nothing, while trying to show the thought process behind.

Firstly, there's lazy updating of the contact list (and the options to configure it). Viable alternatives include no contact list access, only dirty contact list access (as before), contacts which are updated at the end of the step but not again until the next step or manual updating only (as mayobutter suggests).

Secondly, there's contact buffering, and slightly introducing b2Contact user data. This is again optional, and in the past it's not been felt necessary for the engine, but I'm worried that updating contacts outside the step will complicated unbuffered events by another order of magnitude, or at least increase the necessity of users to write their own buffering.

Thirdly, it is possible to hide b2Contact while exposing it's information. I'm not even advocating this, I was just observing it's possible.
Thinking about this a bit more, it's important to reduce the complexity of this system as much as possible. Here's what I'd recommend:

A) There are three types of callbacks: (1) Query callbacks, used to report information during a query or raycast. (2) Predicate callbacks, used to decide things like which collisions should be considered or late filtering. (3) Event callbacks, which is what we're mainly talking about.

B) There should be no event callbacks outside the step. This means no per-contact or per-contact point data. No reporting of new or destroyed contacts outside the step. Predicate callbacks may have problems with lazy evaluation since they can be called unexpectedly during a query, but those are more rare.

C) Get rid of all the events right now and replace them with two: PreIsland and PostIsland. PreIsland would just report the island's contents before solving and PostIsland would again report the island's contents plus impulses, etc after solving.

D) Stick a standard decorator on top of PreIsland/PostIsland that reports ContactResults instead.

E) TOI islands might be reported differently.

F) It might be an interesting hook to be able to solve the island manually.

G) It's fine to buffer events; perhaps a different decorator on top of PreIsland/PostIsland.
has something like this feature mentioned at the beginning of this thread been implemented.

Quote:
chriswa: DisableCollisionResponses()


in box2dFlash or other versions?

-want to make sure I'm not missing out on something 🙂
Here's what I'm working on right now:

Code:
/// Implement this class to get contact information. You can use these results for
/// things like sounds and game logic. You can also get contact results by
/// traversing the contact lists after the time step. However, you might miss
/// some contacts because continuous physics leads to sub-stepping.
/// Additionally you may receive multiple callbacks for the same contact in a
/// single time step.
/// You should strive to make your callbacks efficient because there may be
/// many callbacks per time step.
/// @warning You cannot create/destroy Box2D entities inside these callbacks.
class b2ContactListener
{
public:
   virtual ~b2ContactListener() {}

   /// Called when two fixtures begin to overlap.
   virtual void BeginOverlap(const b2Contact* contact) { B2_NOT_USED(contact); }

   /// Called when two fixtures cease to overlap.
   virtual void EndOverlap(const b2Contact* contact) { B2_NOT_USED(contact); }

   /// This is called after a contact is updated. This allows you to inspect a
   /// contact before it goes to the solver. If you are careful, you can modify the
   /// contact manifold (e.g. disable contact).
   /// A copy of the old manifold is provided so that you can detect changes.
   /// Note: this is called only for awake bodies.
   /// Note: this is called even when the number of contact points is zero.
   /// Note: this is not called for sensors.
   virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
   {
      B2_NOT_USED(contact);
      B2_NOT_USED(oldManifold);
   }

   /// This lets you inspect a contact after the solver is finished. This is useful
   /// for inspecting impulses.
   virtual void PostSolve(const b2Contact* contact) { B2_NOT_USED(contact); }
};


Sensors will only get Begin/End events. Solid contacts get all possible callbacks: Begin/End/PreSolve/PostSolve. I'm trying to provide all the data here for maximal flexibility.

The PreSolve callback is the most interesting case. Please read the comment. Basically you can do anything with the contact manifold. The modification holds until the contact is updated (at which point you get another callback). This should allow for Mario style platforms.

Manifolds will no longer be simple points and normal vectors. Instead they will be planes and points. This will allow contact points to move. This is still approximate since the planes are infinite. You should be able to get the current position of the contact points.

I will also provide the ability to traverse all world and body contacts.

Please give your input ASAP. I'll be submitting this soon.
I see that these call backs get passed a contact rather than a contact point. Does this mean that plane contact (like 1 box resting on another) events will only get triggered once per step, rather than twice (once for each point in the manifold), like they are now? If that's the case I think that does make things much simpler.

Looks good to me. 🙂
Right, there will not be per-point callbacks anymore. Just one per contact manifold.

You can potentially get multiple contact manifold callbacks per step for the same pair of fixtures (shapes). This is due to sub-stepping for continuous physics.
sounds great

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