Browse Source

Handle masks and categories in our contact filter

Turns out, setting our own contact filter bypasses the inbuilt mask and category handling,
this means, however, that masks and categories are processed before your contact filter is
called, so if you really want to do all the filtering manually, you should just make sure
you never change the default masks and category, or restore it.
Bart van Strien 13 years ago
parent
commit
0c9d8f3c41
1 changed files with 16 additions and 0 deletions
  1. 16 0
      src/modules/physics/box2d/World.cpp

+ 16 - 0
src/modules/physics/box2d/World.cpp

@@ -109,6 +109,22 @@ namespace box2d
 
 	bool World::ContactFilter::process(Fixture * a, Fixture * b)
 	{
+		// Handle masks, reimplemented from the manual
+		int filterA[3], filterB[3];
+		// [0] categoryBits
+		// [1] maskBits
+		// [2] groupIndex
+		a->getFilterData(filterA);
+		b->getFilterData(filterB);
+
+		if (filterA[2] != 0 && // 0 is the default group, so this does not count
+			filterA[2] == filterB[2]) // if they are in the same group
+			return filterA[2] > 0; // Negative indexes mean you don't collide
+
+		if ((filterA[1] & filterB[0]) == 0 ||
+			(filterB[1] & filterA[0]) == 0)
+			return false; // A and B aren't set to collide
+
 		if (ref != 0)
 		{
 			lua_State * L = ref->getL();