Why do groups now have priority over category/mask bits?

Hello, I recently updated to the current version of Box2d and discovered that group filtering now has precedence over category and mask bits, is this right? Group indices were useful for avoiding collision between "related objects, like the parts of a bicycle" and I need that functionality. My game has snake-like creatures that I want to collide with each other, but I don't want an individual snake's parts to collide because they get snagged on each other.

What was wrong with category and mask bits having higher priority than group indices?
What version of Box2D are you using? As far as I can tell, group indices have always had higher priority than category filtering. (At least that's what it says in the documentation here, and I don't think that part of the documentation has been changed in a while.)

In any case, if the default behavior isn't what you want, you can install your own collision filter callback and interpret the group indices and category and mask bits in any way you want. For details, check out b2World::SetContactFilter() and b2ContactFilter in b2WorldCallbacks.h/cpp (ShouldCollide() is the function you'll want to override).
Thank you Jesse,
I was using 1.4.3 for a long time. Now I'm using the current SVN version (r60). I'm pretty sure category bits had priority over groups back then, but I could be wrong. Anyway, I will use some custom filtering callback if I can't use the default behavior.

I'd like to know if I can fulfill my needs with the default listener, and also what use the group filtering has. My game has multi-body characters (connected like a chain) that I want to collide with each other, but not with themselves. I used to give the character's bodies a group index of -1, and they would not collide with themselves, but with each other. Can I achieve that with the default listener, and how?

And for what purpose would I want to opt-in/out certain shapes using group indices regardless of category? That sounds a special-case use that custom filter should be used for.
Well, I guess I'm a little confused. I've never used the 'group' functionality myself, but it seems like it should do exactly what you're wanting. For example, say you have two 'chain' entities, A and B. If you assign all of the fixtures associated with entity A a group index of -1, and all of the fixtures associated with entity B a group index of -2, then the behavior should be exactly as you describe: the bodies within each multi-body entity will not interact with one another, but the multi-body entities themselves will still interact with each other. In other words, it seems that you would *want* group indices to take precedence over category and mask bits (and not the other way around).

If you find you're still stuck on this, maybe you could describe in more detail the sort of scenario you have in mind, and why the default behavior would give the wrong results for that particular scenario.
I think I'm the one that's confused. I guess I'm just not sure how to use the categories effectively.

Your example makes sense, but it means that the number of entities (that don't collide with themselves, and collide with others) is limited to 16 total, one for each negative group index. So if I have 17 creatures, two of them can't collide with each other at all, regardless of category/mask bits.

My situation is this: I have many multi-bodied creatures, and different types of creatures. I need to control how bodies collide at three different levels: intra-creature(the creature colliding with itself), inter-creature("group level" or creatures of the same type), and creatures of different types.

I was hoping to use the group index as a kind of sub-category (pretend scenario):
Entity A's fixtures would have category bit 0x0001 (and mask 0xFFFF), and a group index of -1. Entity B's fixtures would have category bit 0x0002 (and mask 0xFFFF), and group index -1. Now entity A and B would collide because of the mask bits, and their fixtures would collide with themselves too- but they don't because the group index prevents it.

I checked out ShouldCollide() and it looks like a simple matter to make category/masks a priority over group indices.
Quote:
I checked out ShouldCollide() and it looks like a simple matter to make category/masks a priority over group indices.

I'm still not convinced that you need to override the default behavior.

It sounds to my like you might be misunderstanding some things about how groups and category and mask bits are used. You mention '16 negative group indices', but there is no such limitation. Group indices aren't bit masks, but rather simple integer values; as such, there's more than 30,000 groups available for each sign (positive and negative). So in other words, there are almost certainly enough group indices available so that each creature can have its own index.

As for category bits, yes, there are only 16 of those available total (since the current version of Box2D uses a 16-bit integer for category and mask bits). But this would only be a limitation if you need to be able to represent more than 16 distinct types for the purpose of collision filtering.

Keep in mind that there doesn't have to be a one-to-one mapping from category bits to entity types; for example, you could have 100 different types of creatures and use the same category bit for all of them if you wanted. You don't need to assign a different category bit to each entity, and you don't even necessarily need to assign a different category bit to each entity type.

Based on what I've read in previous threads, I don't think you're the first person to have been confused by the filtering system, but once you get it figured out it's really pretty straightforward. If you need further help, perhaps you could do the following: post a list of all of the different 'creature types' that you have (or an abstraction thereof - letters would work fine), along with a collision matrix showing what should collide with what. For example, if there were 5 creature types, the collision matrix might look like this:

Code:
  A B C D E
A x x x   x
B x x x x x
C x x   x x
D   x x x 
E x x x   x

If you can provide that information, someone should be able to tell you whether it can be done using the default filtering behavior, and if so, how to do it.
Thank you Jesse, you've been a big help! I shouldn't have any more trouble with collision filtering. 😎

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