setCollisionFilter clarification

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

setCollisionFilter clarification

Post by imtrobin »

Hi,

I like to clarify the behaviour of setCollisionFilter in more detail. I used from PhysX so I assume it's similar to their ShapeGroup behavior. So, how does you set which group the Entity belongs to? I assume it's .collisionFilter, but if so, the sphere and box should collide.

Code: Select all

sphere_ = new Sphere (new Vector3 (100, 200, 100), 3.5f, 20);
space_.add (sphere_);
sphere_.collisionFilter = 1;
sphere_.setCollisionFilter (2, true);

Entity box = new Box (Vector3.Zero, 10, 10, 20);
space_.add (box);
box.collisionFilter = 2;
box.setCollisionFilter (1, true);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: setCollisionFilter clarification

Post by Norbo »

The collisionFilter works by comparing the bit strings of each entity involved in a collision. When using setCollisionFilter, it's actually just setting a particular indexed bit in the collisionFilter bit string. For example, if the collision filter (for now assumed to be only 8 bits for simplicity) was set to 255, the bit string would be:

1111 1111

Using setCollisionFilter(1, false) would make it:

1011 1111

which is equivalent to settings collisionField to 253.

When the a pair of objects is under consideration for narrow phase collision detection, the collisionFilters are compared. Using an example collider A's filter of 1101 0011 and B's filter of 0000 1101:

A: 1101 0011
B: 0000 1101
Binary AND operation: 0000 0001

Since the result of the binary AND was not 0, the two objects will continue on to narrow phase collision detection. If it had been zero (no overlapping bits), the pair would be skipped.

In the example code you gave, the collision filter for the sphere is 5, and 2 for the box. These are 101 and 010 respectively, so the binary AND operation evaluates to 0.

I'd like to add in a more explicit grouping mechanism in the next version where each entity can have a group defined (or left null) and and interaction rules can be created between groups.
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: setCollisionFilter clarification

Post by imtrobin »

Yes, that would be much clearer. I will describe how PhysX works. Each shape has a shapeGroup ID, which is 0 to 31. Internally the physics system maintain a mapping of which group collision are enabled, and it validates the shapeGroup of the colliding shapes. This is from their docs which explains clearer.

Code: Select all

  (a->getActor()->isDynamic() || b->getActor()->isDynamic())        && NxScene::getGroupCollisionFlag(a->getGroup(), b->getGroup())        && (!(NxScene::getShapePairFlags(a,b) & NX_IGNORE_PAIR))
The default shapegroup for all objects are 0, and group 0 collides with group 0.

So each shape is assigned a shapegroup, whiile the system maintains the collision between groups. To me, this is much clearer to setup colliding groups between objects.
Post Reply