Box2D Group Indices
20171220 02:38, 3 years and 1 month agoWhat are Group Indices?
The group index flag can be used to override the category or mask of a fixture. As the name implies it can be used to group certain fixtures together to describe if they should or should not collide, a good example would be Friendly Fire, friendly players don’t get hit by friendly bullets, vice versa.
A group index is an integer ranging from 32768 to 32767.
Fixture:setGroupIndex( index )
How they work
 if either fixture has a groupIndex of zero, use the category/mask rules as above
 if both groupIndex values are nonzero but different, use the category/mask rules as above
 if both groupIndex values are the same and positive, collide
 if both groupIndex values are the same and negative, don’t collide
What we’re really interested in are the last two points.
I’ll give an example, say we have two rectangles, A
and B
.
A.fixture:setGroupIndex(1)
B.fixture:setGroupIndex(1)
They both have the same index, 1
, which is a positive number, therefore A
and B
will collide.
A.fixture:setGroupIndex(1)
B.fixture:setGroupIndex(1)
Although A
and B
have the same absolute value, 1
, they are not the same number, thus they fit into the point of nonzero but different.
A.fixture:setGroupIndex(1)
B.fixture:setGroupIndex(1)
Finally, both fixtures have the same index and are negative, therefore there is no collision between the fixtures.
References
http://www.iforce2d.net/b2dtut/collisionfiltering
https://love2d.org/wiki/Fixture:setGroupIndex
Comments

#
Wow thank you for the explanation on group indices, very useful

#69fd187e15b4c4dab828101f7238cfb6
you’re welcome 😇