Reputation:
if ((catA & maskB) != 0 && (catB & maskA) != 0)
It is in Box2d's manual: 6.2, and is used to check if two objects should collide (after filtering)
Upvotes: 2
Views: 141
Reputation: 8242
I know many C/C++ programmers love terseness but I find this type of code can be made much more readable by moving this test into a function (you can inline it if you want). They could also have gotten rid of the comment by moving the code to a well named method.
if (FixturesCanCollide() )
{
}
You can actually throw away the comparison to !=0 (although you might prefer it for clarity, either way it probably compiles down to the same code.
inline bool FixturesCanCollide()
{
return (catA & maskB) && (catB & maskA);
}
Upvotes: 0
Reputation: 141877
catA is a bit field of collision categories for object A maskA is a bit field of categories that object A can collide with.
For example:
catA = 100010000010010 // Object A is in 4 collision categories
maskA = 001010000000000 // Object A can collide with too different categories
catB = 000010001000001 // Object B is in 3 collision categories
maskB = 100000000000000 // Object B can only collide with the category represented by the highest bit
catA & maskB means the bits that are 1 in both catA and maskB, so 1000000000000000
. It's not 0, because Object B can collide with objects in the highest bit, and object A has that bit set.
catB & maskA means the bits that are 1 in both catB and maskA, so 0000100000000000
. It's also not zero since Object A can collide with objects in the 5th highest bit category, and Object B is in that category.
So the two objects can collide.
Upvotes: 0
Reputation: 7012
It checks that catA has at least one common '1' bit with maskB, and catB has at least one common '1' bit with maskA.
For example, if catA is 3 (binary 00000011) and maskB is 10101010), then (catA & maskB) != 0 is true because catA & maskB is 00000010.
This is called masking, which means only keeping bits of interest.
You frequently have this kind of construct :
#define READ 1
#define WRITE 2
#define READWRITE (READ|WRITE)
#define DIRECTORY 4
int i=getFileInfo("myfile");
if(i & READWRITE)puts("you can read or write in myfile");
if(i & DIRECTORY)puts("myfile is a directory");
BTW, "i & DIRECTORY" means the same as "(i & DIRECTORY) != 0"
Upvotes: 2
Reputation: 7347
the symbol &
is the bitwise AND operator. Therefore, the statement
(catA & maskB) != 0
checks to see if any bits overlap in both of those items. It looks at if it checks A against B first, then B against A.
Upvotes: 0