Storing collision filter masks in b2DynamicTree?

This post describes a potential modification to Box2D I am considering making.

I have a game (an RTS) in which I have a lot of large sensors attached to almost every dynamic body in the game, and thus, a lot of contacts. While filtering simplifies my own code, it doesn't seem that the broadphase benefits at all from the filtering information. Pairs are created for every pair of touching/overlapping fixtures, and then (in my game, at least) the vast majority of these contacts are discarded when calling the default ShouldCollide() callback. This works really well for in many situations, but breaks down somewhere in the neighborhood of 100-200 tightly-packed units, as every unit ends up with ~40 useless contacts with other nearby units.

I think this situation could be sped up dramatically, as follows:
* Store category and mask bits in b2DynamicTree.
* Leaves take their bits from their fixture, internal nodes set their masks and categories as (child1->bits | child2->bits).
* Later updates to internal bit masks can be done in log time.
* b2DynamicTree::Query can be modified to accept category and mask bitmasks. It can then prune entire branches of the tree when searching, even when the AABBs overlap. If (query.mask & node.category == 0) || (query.category & node.mask == 0), then it is clear that no child of that tree node could possibly pass the filter test either.
* This would dramatically reduce the number of total pairs created. Additionally, it would become possible to do extremely cheap b2World::QueryAABB() calls when one wants to frequently query crowded environments for things that are usually not there.

Now, obviously, there are some drawbacks:
* Does not benefit 99% of Box2D users.
* 4 more bytes per tree node that will often go unused.
* Conflicts with the usage of collision group ids, as well as with the usage of custom ShouldCollide callbacks.

I have two questions:
* Have I missed any obvious problems with my approach?
* If I decide to undertake this modification, is there any way I could usefully share my work with Box2D? Given the limited applicability and conflicts with the existing API, it seems unlikely that it would be appropriate to merge such a change into Box2D mainline.

Thanks for reading!

-Mark
Some good ideas there. I made an RTS a while ago using Box2D in the same way you are doing, but I didn't have this problem due to the game only having 20 or so units in total.
I have sometimes wondered whether the idea you're talking about would be useful, but I never wondered enough to actually try it out 😀

About the drawbacks you mention, I doubt the extra 4 bytes per node (or 8 bytes if you use 32 bit masks) would be a problem for anyone.
The interference with group ids and ShouldCollide callbacks is more of a problem, and although both of those are not commonly used, imo making changes that will break existing user code is not a good plan.

If this was a more generically useful feature for the majority of users, I might suggest adding it to the 'mainline' with some kind of opt-in switch to enable it. But I don't think it's quite there really.
Perhaps you could make a patch available for others who might want to use it in future.
I agree, this seems useful. My TSLayer class added a mask to raycasts and aabb queries just like what you described. It is easy to update the collision mask each frame to reflect various object states that might change its collision types.

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