Return to Snippet

Revision: 6557
at May 30, 2008 11:55 by chombee


Updated Code
# 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.

Revision: 6556
at May 30, 2008 11:35 by chombee


Initial Code
# 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

# It has a lot of other methods too.

Initial URL


Initial Description


Initial Title
Panda3D's BitMask32 class (collision masks)

Initial Tags


Initial Language
Python