Skip to content

Instantly share code, notes, and snippets.

@seanh
Created November 12, 2009 11:09
Show Gist options
  • Save seanh/232820 to your computer and use it in GitHub Desktop.
Save seanh/232820 to your computer and use it in GitHub Desktop.
Panda3D's BitMask32 class (Python, Panda3D)
# BitMask32 is a class for creating and combining binary bitmasks.
In [5]: BitMask32.allOff()
Out[5]: 0000 0000 0000 0000 0000 0000 0000 0000
In [6]: BitMask32.bit(1)
Out[6]: 0000 0000 0000 0000 0000 0000 0000 0010
In [7]: BitMask32.bit(0)
Out[7]: 0000 0000 0000 0000 0000 0000 0000 0001
In [8]: BitMask32.bit(2)
Out[8]: 0000 0000 0000 0000 0000 0000 0000 0100
# It supports the operators &, <<, >>, ^ and |, which act like binary operators.
In [10]: BitMask32.bit(0) | BitMask32.bit(1) | BitMask32.bit(2)
Out[10]: 0000 0000 0000 0000 0000 0000 0000 0111
In [16]: BitMask32.bit(0) & BitMask32.bit(1) & BitMask32.bit(2)
Out[16]: 0000 0000 0000 0000 0000 0000 0000 0000
# So if you want a NodePath to match a new bitmask, while still matching all the bitmaps it previously
# matched, you can do:
np.setCollideMask(np.getCollideMask() | newBitMask)
# The invertInPlace() methods inverts each bit:
In [47]: b = BitMask32.bit(0)
In [48]: b
Out[48]: 0000 0000 0000 0000 0000 0000 0000 0001
In [49]: b.invertInPlace()
In [50]: b
Out[50]: 1111 1111 1111 1111 1111 1111 1111 1110
# You can use this if you want your NodePath to no longer match a particular bitmask, but to still match
# all the other bitmasks it already matches.
In [51]: b = BitMask32.bit(0)
In [52]: c = BitMask32.bit(0) | BitMask32.bit(1) | BitMask32.bit(2)
In [53]: c
Out[53]: 0000 0000 0000 0000 0000 0000 0000 0111
In [54]: b.invertInPlace()
In [55]: b
Out[55]: 1111 1111 1111 1111 1111 1111 1111 1110
In [56]: c = c & b
In [57]: c
Out[57]: 0000 0000 0000 0000 0000 0000 0000 0110
# An invert method would probably be more useful than invertInPlace. It has a lot of other methods too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment