Panda3D Manual: Bullet Collision Filtering

By default all Bullet collision objects collide with all other Bullet collision objects. Here the term "collision objects" referns to objects which are derived from BulletBodyNode, namely BulletRigidBodyNode, BulletGhostNode, BulletSoftBodyNode.

Bullet collision objects won't collide with visible geometry, that is objects of type GeomNode!

Sometime we need more control over who collides with whom. This can be achieved by setting up collision filtering properly. Collision filtering is done using bitmasks, which get assigned to every collision object. Two objects collide if the two masks have at least one bit in common.


Bit Masks

Bullet makes use of the regular Panda3D collide masks, which are instances of BitMask32. The following example shows a selection of common ways to set up a bit mask. For more information please refer to the manual page on Collision Bitmasks.

#include "panda3d/bitMask.h"
 
BitMask32 mask1 = BitMask32::all_on();
BitMask32 mask2 = BitMask32::all_off();
BitMask32 mask3 = BitMask32::bit(2);
BitMask32 mask4 = BitMask32::bit(5);
BitMask32 mask5 = BitMask32(0x3);

Given the above bit masks we would get the following results for collision:

mask1 and mask2 = false
mask1 vs. mask3 = true
mask3 vs. mask4 = false
mask3 vs. mask5 = true
mask4 vs. mask5 = false


Assignment

The example below shows a typical setup for a rigid body. Only the last line of the code block is new. Here we set the collide mask which specifies what other objects this rigid body collides with.

BulletBoxShape *box_shape = new BulletBoxShape(LVecBase3f(0.5, 0.5, 0.5));
BulletRigidBodyNode *body_rigid_node = new BulletRigidBodyNode("Body");
body_rigid_node->add_shape(box_shape);
physics_world->attach_rigid_body(box_rigid_node);
 
NodePath np_body = window->get_render().attach_new_node(box_rigid_node);
np_body.set_pos(0, 0, 2);
np_body.set_collide_mask(mask1); // Here we set the collision mask

PandaNodes have two kinds of collide masks, a "from" collide mask and an "into" collide mask. Panda3D's internal collision system requires both masks set, but when using Bullet physics only the "into" collide mask is used. The following line is an alternate way to set the collide mask:

np_box.node()->set_into_collide_mask(mask);

This way of setting collide masks can be used for rigid bodies and ghost objects. Soft body collisions (and soft body vs. rigid body collisions) are more complex. Please see the manual pages about soft body configuration for details.