Suggestions for improving the contact callback system

@chriswa: Yes, this is the logic I was purposing. I suggested a typed attribute instead of UserData to avoid the typecast and to make it more explicit for folks using it. My thought is if that is the common way (seems like pretty standard event dispatch logic) to handle why not just give it to folks out of the box. Sounds like there may be enough folks who want to do something different to not make it useful. Perhaps that could be the standard impl that folks could "replace" by assigning their own global contact listener if they didn't want it?

@ewjordan: I definitely like the ingenuity behind this 🙂 Looks to be a bit much for my current app though. But something to keep in mind down the road.

That being said, I realize I'm out gunned by several veterans here so I know when to quit (I think). An article on contact listeners is a good idea. While I didn't plan to do the "iterate over the shapes" approach going forward, I could see where a lot of newer folks would, at least at first until they come to their senses. Doc should help.
Chriswa's approach (of dispatching to shapes) is not nearly universal. One might also want to dispatch to bodies, as my own event handler does (i.e. shape.GetBody().GetUserData().MyEventHandler(...) ). Or you might want a hash map a la ewjordan. Or there are some people who assign their shapes into more complicated taxonomies, and dispatch based on that, e.g. I might want to register an event for all collisions between "players" and "enemies", and not have to list every pair exhaustively, but still have a non-OO approach like ewjordan.

I'm not keen on the idea of typing UserData, but I'm not sure I can articulate why. Typing it to a class is nasty, as the user might not want to have his class inherit from the Box2D one (e.g. if I'm meshing two frameworks), and it raises the question of is that class inside Box2D or outside? And an interface seems doable, but there are no interfaces at the moment, and I don't know if it would restrict the platforms Box2D is available on in some way.
BorisTheBrave wrote:
Or there are some people who assign their shapes into more complicated taxonomies, and dispatch based on that, e.g. I might want to register an event for all collisions between "players" and "enemies", and not have to list every pair exhaustively, but still have a non-OO approach like ewjordan.

Yeah - getting group-group interaction listeners is a very common desire (player vs. scenery, player vs. enemy, player vs. weapon, player vs. pickup, etc.), and one of the issues there is setting it up so that a single object may belong to more than one group, which makes things a bit more complicated. It's doable, but requires a lot more thought. I ended up holding on to my own group membership flags (separate from Box2d groups) and using a similar hash map approach, which worked well, but was definitely not simple.

Quote:
I'm not keen on the idea of typing UserData, but I'm not sure I can articulate why. Typing it to a class is nasty, as the user might not want to have his class inherit from the Box2D one (e.g. if I'm meshing two frameworks), and it raises the question of is that class inside Box2D or outside? And an interface seems doable, but there are no interfaces at the moment, and I don't know if it would restrict the platforms Box2D is available on in some way.

Typed user data would be a problem once you leave C++, since most other languages won't allow multiple inheritance (I'm pretty sure this is the case in Flash, and since that seems to be the most used version of Box2d, we probably shouldn't do something it can't support). Interfaces might be better, but to be honest, I currently don't see any major problems with the void* approach (or the translation in other languages), you can do pretty much anything you want with that by convention within your app even if it's not pretty.

However, I definitely agree that when the dust settles on all of this we should probably put together a dedicated contact handling document that demonstrates in detail how to do most of the common things in a fairly standard way, since it's far from clear right now (we all appear to handle it differently, and that's probably not only because our needs are different, it's because the engine doesn't push you towards any particular solution). Maybe these should even be baked into a few testbed examples, since they are so universally important in games...it would be a lot easier for people if they could just run the examples and see which one matched what they wanted to do and then cut and paste the code to do it.

It might even be worth having a contact handling library contribution that makes all of this as simple as possible (and may impose further restraints on user code that we don't want to impose from within the engine), come to think of it. Let's see how things look after the Box2d internals settle down and these changes are in for a while...
ewjordan wrote:
I do it a bit differently, running shape id pairs through a 2->1 hash to create a pair key and fill a hash map to mark the pairs that I care about, then when I get the "add" event, I only store (in a hash map with the same key) or respond to the pair if its hash matches an entry already in the "I care about these" hash table. This has better complexity behavior: you have to run the hash and check the hash map for every contact, and then run once through all the pairs that you care about to handle them, so it's O(M+N) instead of O(M*N) (where M is the number of contacts and N is the number of pairs you're interested in), assuming the hash map lookups happen in average O(1) time, which should be the case if you have a good 2->1 hash function and you use a decent hash map implementation.

I'm interested in your 2-to-1 hashing. How did you decide on this algorithm and did you work out what the chances are that you'll get a hash collision? I considered this approach but assumed they'd be too high - but then I'm afraid of UUIDs colliding 😕
chriswa wrote:
I'm interested in your 2-to-1 hashing. How did you decide on this algorithm and did you work out what the chances are that you'll get a hash collision? I considered this approach but assumed they'd be too high - but then I'm afraid of UUIDs colliding 😕

Frankly, I have no idea where I got that from. It appears to be a version of one of Thomas Wang's integer hash functions (http://www.concentric.net/~Ttwang/tech/inthash.htm), just modified to take two 16 bit integers by shifting them together (collisions will be more likely for integers that are too big, but if that's an issue for you here you've probably got too many bodies anyways!).

But honestly, I didn't look into it in too much detail, I think I just Googled and picked the first one I found. Collisions aren't too critical, since I'm fairly certain the data structure handles them, just at a speed hit, so I didn't dig any deeper. By all means continue looking around if you need something better!

Edit: no collisions between any pair of integers less than 5000, which should make it good enough for keeping track of physics objects!
I'm confused as to your hash too, now I inspect it. What's the point in all the bit shift operations if you are expecting shorts, key seems already sufficient as a hash after the first line.
BorisTheBrave wrote:
I'm confused as to your hash too, now I inspect it. What's the point in all the bit shift operations if you are expecting shorts, key seems already sufficient as a hash after the first line.

Yup, you're right, it's altogether unnecessary, and after the first line it would do just fine. Don't know what I was thinking at the time, I probably cut and pasted that code without thinking about what it did after deciding that I needed some 2->1 integer map that was unique for the range I was interested in (despite what I said in the last post, HashMap can't handle collisions if the keys are identical, it can only handle them if the hashes are identical but the keys are different).

So in summary, don't pay attention to that, I don't know why I did it that way, and I didn't need to; unfortunately these types of not-quite-bugs never present themselves as important during testing because things work just fine, they're just a bit inefficient.
I suppose the complexity with your approach will go away when we have access to the b2Contact and can map 1-to-1 with its pointer.
OK, here's a prototype that includes late filtering. Yay 😀. It's also cleaned up the contact code a bit, there are no huge tracts of duplicate contact matching code any more, at the cost of an extra indirect function call.

Attachment:
events.zip [5.76 KiB]
Downloaded 55 times


Basically, you return a boolean from the ContactStart event, which sets the solidity of the contact. Returning false makes it behave like a sensor (i.e. no collision resolution, only ContactStart and ContactStop events).

There's no TestBed example at the moment, please consider writing one. In particular, I'd like to see filtering conditional on speed of impact, and on which edge the contact is with. If you want to quickly play around, I recommend changing the line
return contact->IsSolid();
(which keeps the solidity at the Box2D default) to
return b2Random()>0;
which enables 50% of all collisions. You can see that it is re-evaluated every time the shapes cease touching and re-touch, which I think is the most intuitive (as opposed to re-evaluation every time a contact is added, or when the AABBs start overlapping).

Note this makes sensors basically pointless, except as a built in default.

I like the interface of returning a value (I will eventually mark the event arguments as const, i think), but I'm not sure everyone does. Do you prefer calling something like "contact->Disable()" as originally planned? Is there any performance difference?

EDIT: I don't think I made clear that the above is essentially late sensoring. Late filtering would be when you actually delete the contact. I think I can code that in as well, but would people actually use it? The only advantage it has over the above is performance, while it is significantly less flexible (you'd get no further events, re-evaluation would only occur when the AABBs cease to overlap). Can anyone imagine a useful circumstance where the performance difference would actually matter?
will all this stuff be coming to the AS3 port?

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