# Collision Filtering in Box2D

I have four bodies in my Box2D World. Each are polygon shape, with some physics parameters.

Now when the game start, I need one of the bodies from the World (BodyA) to not be allowed to collide with another (BodyB). It should be allowed to collide with others.

To clarify, BodyA and BodyB cannot collide with each other, but can collide with rest of the bodies in the Box2D world.

How could make this kind of mechanism in Box2d ?

In Box2D, this is called Collision Filtering.

Collision filtering allows you to prevent collision between fixtures. For example, say you make a character that rides a bicycle. You want the bicycle to collide with the terrain and the character to collide with the terrain, but you don't want the character to collide with the bicycle (because they must overlap). Box2D supports such collision filtering using categories and groups.

Take a look at the Box2D manual, and search for Collision Filtering. It will explain to you everything you need to know to get started.

http://box2d.org/manual.html

Edit

Box2D supports 16 collision categories. For each fixture you can specify which category it belongs to. You also specify what other categories this fixture can collide with. For example, you could specify in a multiplayer game that all players don't collide with each other and monsters don't collide with each other, but players and monsters should collide. This is done with masking bits. For example:

//code
playerFixtureDef.filter.categoryBits = 0x0002;
monsterFixtureDef.filter.categoryBits = 0x0004;



Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index). Group indices are usually used for things that are somehow related, like the parts of a bicycle. In the following example, fixture1 and fixture2 always collide, but fixture3 and fixture4 never collide.

//Code
fixture1Def.filter.groupIndex = 2;
fixture2Def.filter.groupIndex = 2;

fixture3Def.filter.groupIndex = -8;
fixture4Def.filter.groupIndex = -8;

• i like your answer. But if you have post or some thing helpful me to collision filtering. please tell me.. Commented Feb 13, 2013 at 11:52
• @Jagdish I could quote the entire section of the manual if you prefer? Commented Feb 13, 2013 at 11:59
• i have read it. But still it collide, So do any other extra stuff there.. b2Fixture or b2FixtureDef in use ? please explain.. Commented Feb 13, 2013 at 12:16
• Thanks a lot for explaining this things.. Now Done.... This is main line. "Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)." Commented Feb 13, 2013 at 12:54
• @jgallant the link you posted is dead (404). However, based on your copy/pasted documentation, I can still figure it out. thanks. Commented Jan 29, 2021 at 4:07

I am implementing a 2.5D app, so I need layers. For example this horse has about 5 layers.

I'm not sure, but I think that given two bodies with mask and category (m1,c1) and (m2,c2), they will collide only if the m1&c2!=0 AND m2&c1!=0.

By default I want a body to be very thick so that it occupies all layers, so I set the default m = c = 65535. If I want the body to only occupy layer 1, then I set m = c = 1. If I want the body to only occupy layer 5, then I set m = c = 32. If I want the body to be a little thick so that it occupies layers 1+2+3 then I will set m = c = 7.

I don't use the group index at all. It is always 0.