Bitmask Example

Don't mind the mess!

We're currently in the process of migrating the Panda3D Manual to a new service. This is a temporary layout in the meantime.

Here is a short example of using bitmasks to selectively test collisions against different object in the scene:

from direct.showbase.ShowBase import ShowBase
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText, TextNode
from panda3d.core import CollisionTraverser
from panda3d.core import CollisionHandlerQueue, CollisionNode, BitMask32
from panda3d.core import CollisionPlane, CollisionSphere, CollisionRay
from panda3d.core import Plane, Vec3, Point3
class World(DirectObject):
    def __init__(self):
        # Create a traverser that Panda3D will automatically use every frame.
        base.cTrav = CollisionTraverser()
        # Create a handler for the events.
        self.collHandler = CollisionHandlerQueue()
        # Define a few bitmasks for use.
        # Teaching the concepts of bitmasks is out of the scope of this sample.
        # This just shows a practical application of bitmasks.
        goodMask = BitMask32(0x1)
        badMask = BitMask32(0x2)
        floorMask = BitMask32(0x4)
        # Make a list of different combinations of the masks for later use.
        # We will switch between these masks later on.
        self.maskList = [
            ["floor", floorMask],
            ["smiley", goodMask],
            ["frowney", badMask],
            ["characters", goodMask | badMask],
            ["smiley and floor", goodMask | floorMask],
            ["frowney and floor", badMask | floorMask],
            ["all", floorMask | goodMask | badMask]
        # This keeps track of where we are in the dictionary.
        self.maskPosition = 0
        # First we create a floor collision plane.
        floorNode = render.attachNewNode("Floor NodePath")
        # Create a collision plane solid.
        collPlane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0)))
        # Call our function that creates a nodepath with a collision node.
        floorCollisionNP = self.makeCollisionNodePath(floorNode, collPlane)
        # Get the collision node the Nodepath is referring to.
        floorCollisionNode = floorCollisionNP.node()
        # The floor is only an into object, so just need to set its into mask.
        # Create a collision sphere. Since the models we'll be colliding
        # are basically the same we can get away with just creating one
        # collision solid and adding the same solid to both collision nodes.
        collSphere = CollisionSphere(0, 0, 0, 1.5)
        # Make a smiley.
        smiley = loader.loadModel('smiley')
        smiley.setPos(-3, 3, 3)
        smileyCollisionNP = self.makeCollisionNodePath(smiley, collSphere)
        # Like with the floor plane we need to set the into mask.
        # Here we shortcut getting the actual collision node.
        # Make a frowney.
        frowney = loader.loadModel('frowney')
        frowney.setPos(-3, 3, 7)
        frowneyCollisionNP = self.makeCollisionNodePath(frowney, collSphere)
        # Use the the Nodepath.setCollideMask() function to set the into mask.
        # setCollideMask() sets the into mask of all child nodes to the given mask.
        # Note that we don't call setCollideMask() from frowney because this
        # will turn the frowney mesh into a collision mesh which is unwanted.
        # Note that we didn't set a from collide mask for previous objects
        # since we're not adding them to the traverser as from objects.
        # Make a collision ray that passes through all of the objects.
        self.pointerNode = render.attachNewNode("Main Collider")
        self.pointerNode.setPos(-3, 3, 10)
        # Create a ray collision solid that points downwards.
        raySolid = CollisionRay(0, 0, 0, 0, 0, -1)
        mainCollisionNP = self.makeCollisionNodePath(self.pointerNode, raySolid)
        self.mainCollisionNode = mainCollisionNP.node()
        # Set a from collide mask for this ray so that we can selectively
        # collide against the other objects.
        base.cTrav.addCollider(mainCollisionNP, self.collHandler)
        # Set up the camera.
        base.disableMouse(), -20, 5), 0, 5)
        # Debug mode for collision traversers; shows collisions visually.
        # Setup the title text.
        collideText = self.maskList[self.maskPosition][0]
        self.title = OnscreenText(text="Colliding with %s" % (collideText),
                                  pos=(0.3, 0),
                                  fg=(1, 1, 1, 1))
        OnscreenText(text="Press space to change collision mask",
                     pos=(0, 0.8),
        # Set space to change the from collision mask of the collision ray.
        base.accept("space", self.switchCollisionMask)
    def makeCollisionNodePath(self, nodepath, solid):
        Creates a collision node and attaches the collision solid to the
        supplied NodePath. Returns the nodepath of the collision node.
        # Creates a collision node named after the name of the NodePath.
        collNode = CollisionNode("%s c_node" % nodepath.getName()) 
        collisionNodepath = nodepath.attachNewNode(collNode)
        # Show the collision node, which makes the solids show up.
        return collisionNodepath
    def switchCollisionMask(self):
        if self.maskPosition == len(self.maskList) - 1:
            self.maskPosition = 0
            self.maskPosition += 1
        # Changing the from collide mask of objects allows you to selectively
        # test collisions against different objects.
        self.title.setText("Colliding with %s" % (self.maskList[self.maskPosition][0]))
world = World()


Previous Top Next