I have three different kinds of rectangles. Black, white and blue rectangles. The rectangles are moving and can collide with each other. But with one exception, the blue rectangle can collide normally with the black rectangle, but the blue rectangle should move through the white rectangle if it's touching the white rectangle. The white and black rectangles can collide normally.

How can I do that? In the moment, the blue rectangle isn't moving through the white rectangle if it's touching the white rectangle. What can I do so that the blue rectangle can move through the white rectangle, but not through the black rectangle?

I solved the problem with CollisionWidth.

For expamle: Body.CollisionWidth = Categorie.Cat5

In this case, the body just collides with bodies which have the CollisionCategorie 5.


2 Answers 2


It's been a while since I've touched farseer, but if I remember right, it shares a very similar API to Box2d.

So you should have something like: Fixture.filter.categorybits; Fixture.filter.maskbits;

So let's say your blue box is setup: Fixture.filter.categorybits = 0x0001; Fixture.filter.maskbits = 0x003;

Your black box is setup: Fixture.filter.categorybits = 0x0002; Fixture.filter.maskbits = 0x0007;

And your white box is setup: Fixture.filter.categorybits = 0x0004; Fixture.filter.maskbits = 0x0007;

What this means is that each maskbits specifies what categories it collides with. So both black and white boxes collide with everything (0x0007 = 00000111), while blue boxes collide only with blue and black boxes.

  • \$\begingroup\$ I don't understand your example. Why is 0x0007 = 00000111 ? Why did you choose these numbers and not other numbers? Why did you choose Fixture.filter.categorybits = 0x0004; and not Fixture.filter.categorybits = 0x0003; for the white box? \$\endgroup\$ Commented Apr 3, 2014 at 22:26
  • \$\begingroup\$ These are 16-bit bitwise masks. I would suggest looking at how binary works. It means that a mask has to match the same bits as the category for a collision to work. \$\endgroup\$
    – user39686
    Commented Apr 4, 2014 at 2:58
  • \$\begingroup\$ I can not use your code in Farseer. How can I do this in Farsser? If I use your code, I get this error message: 'FarseerPhysics.Dynamics.Body' does not contain a definition for 'Filter' and no extension method 'Filter' accepting a first argument of type 'FarseerPhysics.Dynamics.Body' could be found (are you missing a using directive or an assembly reference?) \$\endgroup\$ Commented Apr 4, 2014 at 18:23

Lets say you have a player and a bunch of tiles.

Then you can set a category on each. This is done by a bit-value.

A bit contains 0000.0000 places to be assigned.

Best thing you can do is use values like 2, 4, 8, 16, 32, 64.

In between values may cause collision within the bit and cause it to drop a category or a mask.

So first create to categories.

short PLAYER_BIT = 2;
short BLOCK_BIT = 4;

Then when creating an object and you have made an FixtureDef() for this item.

    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;
    fdef.density = 1.062f;
    fdef.filter.categoryBits = PLAYER_BIT;//This is the catagory, which type it is in bits.
    fdef.filter.maskBits = BLOCK_BIT;//The mask, which it can collide with.

Now for the block(s) you the do the exact opposite.

fdef.filter.categoryBits = BLOCK_BIT;//Because it is a block.
fdef.filter.maskBits = PLAYER_BIT;//Because block can only collide with player.

You can also assign multiple masks or categories but, you need to know bit-operators. Here is some info on bit-operators. I hope this helps.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .