Quote:
BorisTheBrave: "How about a second set of events, corresponding to contact, rather than contact points? ..."
Quote:
Erin Catto: "BorisTheBrave: good idea. Would you care to sketch up the entire contact callback/query interface in pseudo code?"
Okay. I've been thinking about this a lot. Here is my suggestion:
Code:
class ContactCallback
{
void ContactStart(b2Contact* contact);
void ContactStop(b2Contact* contact);
void ContactPointAdd(b2Contact* contact, b2ContactPoint* point);
void ContactPointPersist(b2Contact* contact, b2ContactPoint* point);
void ContactPointRemove(b2Contact* contact, b2ContactPoint* point);
void ContactPointResponse(b2Contact* contact, b2ContactPointResult* result);
};
{
void ContactStart(b2Contact* contact);
void ContactStop(b2Contact* contact);
void ContactPointAdd(b2Contact* contact, b2ContactPoint* point);
void ContactPointPersist(b2Contact* contact, b2ContactPoint* point);
void ContactPointRemove(b2Contact* contact, b2ContactPoint* point);
void ContactPointResponse(b2Contact* contact, b2ContactPointResult* result);
};
Immediately before the first contact point is created, ContactStart would be called. Immediately after the last contact point is removed, ContactStop would be called. Any contact involving at least one sensor would only get these two callbacks and none of the others. The other callbacks are our current callbacks, renamed and with one major change:
The b2Contact object would be exposed to every callback to help users in tracking a contact over its lifetime as well as allowing users to dynamically disable collisions for Mario-style platforms and breakable objects. This would primarily be done in ContactPointAdd callbacks and could be accomplished by changing the b2Contact's nonSolid flag, but having a method to call on b2Contact would be preferred (e.g. b2Contact->DisableCollisionResponses()). Optimally, the collision responses would be automatically re-enabled after the ContactStop callback so that the user wouldn't have to worry about whether a contact is "reused" (i.e. if the AABBs remain overlapping and a contact point appears again, triggering another ContactStart.)
I also suggest adding a UserData field to b2Contact so that users can track information specific to a contact shape-pair over its lifetime; although, this would be easy for users to do themselves with a map<b2Contact*, MyContactInfo*>.
I'm in favour of re-exposing the contact list to shapes. I think a caveat in the docs that "you shouldn't do anything that might affect a list you're iterating through or very bad things will happen" is more than enough. It's just so useful to provide this information, it's already available, and the alternative is to let users track these things themselves and run into the exact same problem while iterating over their own lists.
P.S. Another use case for collision filtering is "down jumping" through a platform you've been standing on. A simple solution would be to iterate through your shape(s)'s contacts and call ->DisableCollisionResponses() on any player-platform contacts.
P.P.S. You two are both brilliant.