Setting up object collision filtering
Object collision might not be always desirable. For example, if your game allows multiplayer, it can be frustrating when another player blocks your movement right in front of doors and you can't get past them. The Box2D library solves this problem with the binary mask for collisions.
Getting ready
This recipe will require a basic understanding of bitwise operations. You'll need at least two FixtureDef
or Fixture
objects.
How to do it…
The Box2D library uses a pair of two 16-bit binary masks to determine whether two objects should collide. The first one is called categoryBits
and it indicates the object category. The second one uses the name maskBits
and it indicates what object categories are allowed to collide with the current object. The following sample code shows the typical uses of these bit-masks in the FixtureDef
object:
local fixture_def = box2d.FixtureDef() fixture_def.shape = shape fixture_def.density = 1.5 fixture_def.friction = 0.3 fixture_def...