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?
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.
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.
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).