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
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