Panda3D's BitMask32 class (collision masks)


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # BitMask32 is a class for creating and combining binary bitmasks.
  2.  
  3. In [5]: BitMask32.allOff()
  4. Out[5]: 0000 0000 0000 0000 0000 0000 0000 0000
  5.  
  6. In [6]: BitMask32.bit(1)
  7. Out[6]: 0000 0000 0000 0000 0000 0000 0000 0010
  8.  
  9. In [7]: BitMask32.bit(0)
  10. Out[7]: 0000 0000 0000 0000 0000 0000 0000 0001
  11.  
  12. In [8]: BitMask32.bit(2)
  13. Out[8]: 0000 0000 0000 0000 0000 0000 0000 0100
  14.  
  15. # It supports the operators &, <<, >>, ^ and |, which act like binary operators.
  16.  
  17. In [10]: BitMask32.bit(0) | BitMask32.bit(1) | BitMask32.bit(2)
  18. Out[10]: 0000 0000 0000 0000 0000 0000 0000 0111
  19.  
  20. In [16]: BitMask32.bit(0) & BitMask32.bit(1) & BitMask32.bit(2)
  21. Out[16]: 0000 0000 0000 0000 0000 0000 0000 0000
  22.  
  23. # So if you want a NodePath to match a new bitmask, while still matching all the bitmaps it previously
  24. # matched, you can do:
  25. np.setCollideMask(np.getCollideMask() | newBitMask)
  26.  
  27. # The invertInPlace() methods inverts each bit:
  28. In [47]: b = BitMask32.bit(0)
  29.  
  30. In [48]: b
  31. Out[48]: 0000 0000 0000 0000 0000 0000 0000 0001
  32.  
  33. In [49]: b.invertInPlace()
  34.  
  35. In [50]: b
  36. Out[50]: 1111 1111 1111 1111 1111 1111 1111 1110
  37.  
  38. # You can use this if you want your NodePath to no longer match a particular bitmask, but to still match
  39. # all the other bitmasks it already matches.
  40. In [51]: b = BitMask32.bit(0)
  41.  
  42. In [52]: c = BitMask32.bit(0) | BitMask32.bit(1) | BitMask32.bit(2)
  43.  
  44. In [53]: c
  45. Out[53]: 0000 0000 0000 0000 0000 0000 0000 0111
  46.  
  47. In [54]: b.invertInPlace()
  48.  
  49. In [55]: b
  50. Out[55]: 1111 1111 1111 1111 1111 1111 1111 1110
  51.  
  52. In [56]: c = c & b
  53.  
  54. In [57]: c
  55. Out[57]: 0000 0000 0000 0000 0000 0000 0000 0110
  56.  
  57. # An invert method would probably be more useful than invertInPlace. It has a lot of other methods too.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.