Collision Bitmasks explanation

The manual says:

Sorry but I don’t get this. Does that mean if the decimal number in binary has at least one 1 or 0 and so does the other number, then the objects will be tested? How many number choices do you have then? And is there an easy way to check if a decimal number shares a bit in binary representation, compared to another number?

[A question about BitMask.bit)

Seems to only answer first part of my question.
Here numbers in range 0,32:

0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 1000
0000 0000 0000 0000 0000 0000 0001 0000
0000 0000 0000 0000 0000 0000 0010 0000
0000 0000 0000 0000 0000 0000 0100 0000
0000 0000 0000 0000 0000 0000 1000 0000
0000 0000 0000 0000 0000 0001 0000 0000
0000 0000 0000 0000 0000 0010 0000 0000
0000 0000 0000 0000 0000 0100 0000 0000
0000 0000 0000 0000 0000 1000 0000 0000
0000 0000 0000 0000 0001 0000 0000 0000
0000 0000 0000 0000 0010 0000 0000 0000
0000 0000 0000 0000 0100 0000 0000 0000
0000 0000 0000 0000 1000 0000 0000 0000
0000 0000 0000 0001 0000 0000 0000 0000
0000 0000 0000 0010 0000 0000 0000 0000
0000 0000 0000 0100 0000 0000 0000 0000
0000 0000 0000 1000 0000 0000 0000 0000
0000 0000 0001 0000 0000 0000 0000 0000
0000 0000 0010 0000 0000 0000 0000 0000
0000 0000 0100 0000 0000 0000 0000 0000
0000 0000 1000 0000 0000 0000 0000 0000
0000 0001 0000 0000 0000 0000 0000 0000
0000 0010 0000 0000 0000 0000 0000 0000
0000 0100 0000 0000 0000 0000 0000 0000
0000 1000 0000 0000 0000 0000 0000 0000
0001 0000 0000 0000 0000 0000 0000 0000
0010 0000 0000 0000 0000 0000 0000 0000
0100 0000 0000 0000 0000 0000 0000 0000
1000 0000 0000 0000 0000 0000 0000 0000

None match.

I thought you could have some values which are not the same, but still have similar bit and so will return True.

Lets say you want your players to collide with each other, so they are both ‘from’ and ‘into’ objects for each other. And then there are NPCs which can be both ‘from’ and ‘into’ for each other, but only ‘from’ for players. And yet you want both players and NPCs to be a ‘from’ for the walls.

The quote is making a statement about the RESULT (of comparing two bitmaks: mask1 & mask2). The RESULT is only one number. No “other” number involved in this statement.

The statement is that if this RESULT is non-zero (and only then) then two objects are checked for collision.

The result is non-zero if it has at least one bit set.

Example:

m1       = 0000 0000 0000 0000 0000 0000 1111 0001
m2       = 0000 0000 0000 0000 0000 1111 0000 0010 
m1 &  m2 = 0000 0000 0000 0000 0000 0000 0000 0000
---> value of (m1 & m2) = 0, so NO COLLISION CHECK

m1       = 0000 0000 0000 0000 0000 0000 1111 0101
m2       = 0000 0000 0000 0000 0000 1111 0000 0110 
m1 &  m2 = 0000 0000 0000 0000 0000 0000 0000 0100
---> value of (m1 & m2) = 4, so DO COLLISION CHECK

And yes, there is an easy way to see if two numbers share a bit, it’s “number1 & number2”, duh.

Yes you can :
BitMask32(0): 0000 0000 0000 0000 0000 0000 0000 0000
BitMask32(1): 0000 0000 0000 0000 0000 0000 0000 0001
BitMask32(2): 0000 0000 0000 0000 0000 0000 0000 0010
BitMask32(3): 0000 0000 0000 0000 0000 0000 0000 0011
BitMask32(4): 0000 0000 0000 0000 0000 0000 0000 0100
BitMask32(5): 0000 0000 0000 0000 0000 0000 0000 0101
BitMask32(6): 0000 0000 0000 0000 0000 0000 0000 0110
BitMask32(7): 0000 0000 0000 0000 0000 0000 0000 0111
BitMask32(8): 0000 0000 0000 0000 0000 0000 0000 1000
BitMask32(9): 0000 0000 0000 0000 0000 0000 0000 1001
BitMask32(10): 0000 0000 0000 0000 0000 0000 0000 1010

Hm, OK this must be whats confusing me:

BitMask32(int)
Bitmask32.bit(int)

What’s the difference?

And yes, there is an easy way to see if two numbers share a bit, it's "number1 & number2", duh.

Yeah, I didn’t word that very well. I mean without having to actually check if the two numbers don’t return 0 and then memorizing that. I mean is there maybe some kind of pattern? (like 1, 10, 20,… or 2, 4, 8, 16,…). Don’t want the other person reading my code to actually & all the numbers to find out what I intended to collide with what (could put that info in a comment, but if there is some pattern like above then thats a needless comment).

There is a pattern indeed, and you’re right to suggest powers of two. I use constants and the binary or to make my code more clear to the user.

Here’s an example of what I’d do.

COL_FLOOR = 1
COL_WALL = 2
COL_CREATURES = 4
COL_MOUSEPICK = 8 # You can click on these.

self.player.setCollisionMask(COL_FLOOR | COL_WALL | COL_CREATURES)
self.evil_ghost.setCollisionMask(COL_FLOOR | COL_MOUSPICK) # Ghosts never fall through the floor in movies.

Not the smartest example ever, but that gives the idea.

>>> a=BitMask32(10)
>>> b=BitMask32.bit(10)
>>> print a
 0000 0000 0000 0000 0000 0000 0000 1010
>>> print b
 0000 0000 0000 0000 0000 0100 0000 0000

BitMask32(10) will set the bitmask to the binary representation of 10, BitMask32.bit(10) will set the 10-th bit of the mask to 1.

For everyday use I suggest using only Bitmask32.bit(int) and Bitmask32(int) only if you really, really need more then 32 different masks.

Um, that’s a bit misleading. Bitmasks with more than one bit enabled are commonly used when you want more complex collision behaviour between objects, that cannot be simply described using single bit checks. It doesn’t give you more than 32 different combinations.

BitMask32.bit(n) is the same as BitMask32(1 << n), or BitMask32(2 ** n). BitMask32.bit(n) returns a bitmask with the n’th bit enabled, whereas BitMask32(n) just returns a bitmask representing the number n (ie the binary form of a decimal number).

OK