Been playing with bullet a little after a post by Erin got me thinking. Callbacks are the wrong way to deal with contacts. Instead, you should just ask for all the contacts on a shape or body and have access to the contact and result for each.
Right now a step works (almost) like this:
What I'd like is this:
No-longer-valid contacts are cached, and exist until cleared at the start of the next Step() call. Outside of a step, it might be best if adds/moves created new contacts, moves cached old ones and deletes got rid of contacts.
ContactResult data is saved along with the contact itself. If a body has multiple results due to multiple manifolds or TOI, perhaps multiple results will be stored per contact.
There's no Add/Persist/Remove required because the contacts are cached and the user can figure this out by checking whether a contact didn't exist the previous step or if it's marked for removal or doesn't exist now.
The special return value on Add could remain as a separate call, perhaps in b2ContactFilter.
As for speed and memory, I'd expect speed to increase because there are no callbacks interrupting contacts, though more memory usage by default because the engine would buffer b2ContactResults. But I'm willing to bet most users right now buffer b2ContactResults at least, and perhaps Adds/Persists/Removes as well.
Edit: Took a shot at making these changes. It's imperfect, but enough to play around with.
Right now a step works (almost) like this:
- User calls Step()
- Solve non-TOI (calling Add/Persist)
- Report
- Update broadphase (adding/deleting contacts, calling Remove)
- Solve TOI (calling Add/Persist)
- Report
- Update broadphase (adding/deleting contacts, calling Remove)
- If TOI events still exist goto (5)
- End of Step()
- User unbuffers events to make changes
What I'd like is this:
- User calls Step()
- Reset contact results
- Clear contacts marked for deletion
- Solve non-TOI
- Store b2ContactResult info with contact
- Update broadphase (adding contacts, marking others for removal next step)
- Solve TOI
- Store b2ContactResult info with contact
- Update broadphase (adding contacts, marking others for removal next step)
- If TOI events still exist goto (7)
- User checks a shape's or body's contact list for contacts, keeping track of adds/removes manually
No-longer-valid contacts are cached, and exist until cleared at the start of the next Step() call. Outside of a step, it might be best if adds/moves created new contacts, moves cached old ones and deletes got rid of contacts.
ContactResult data is saved along with the contact itself. If a body has multiple results due to multiple manifolds or TOI, perhaps multiple results will be stored per contact.
There's no Add/Persist/Remove required because the contacts are cached and the user can figure this out by checking whether a contact didn't exist the previous step or if it's marked for removal or doesn't exist now.
The special return value on Add could remain as a separate call, perhaps in b2ContactFilter.
As for speed and memory, I'd expect speed to increase because there are no callbacks interrupting contacts, though more memory usage by default because the engine would buffer b2ContactResults. But I'm willing to bet most users right now buffer b2ContactResults at least, and perhaps Adds/Persists/Removes as well.
Edit: Took a shot at making these changes. It's imperfect, but enough to play around with.