Multi-layered physics

Hi,

I'm having trouble working out the best way to implement multi-layered physics, and I could do with some advice. I want to make something a bit like Paper Moon (http://blurst.com/paper-moon/) in which things can "pop" in and out of the screen. So there might be 3 layers, with the main character and gameplay happening in the middle layer, and a control mechanic for moving bodies between the layers. What Paper Moon doesn't do, that I'd like to, is have stuff interact with each other on more than just the middle layer (so, if you push an enemy into the background, he'll fall off his platform (because it stays in the "mid-ground", but will now be able to interact with other things in the background.

What's the best way to go about doing this in Box2D? I initially thought that I could just create 3 seperate b2Worlds, and step all of them each frame, and when something moves between layers just remove it from one world and add it to the other world... But I don't know how efficient that will be.

I imagine that this sort of thing might be possible using just one b2world, and using collision filtering to flag which bodies are on which layers, but I have no idea where to begin with something like that. For all of Box2D's awesome user-friendliness, I've tried time and time again to understand how the collision filtering works and I find it incomprehensible every time I try.
Collision filtering should give you what you want.

You can set the filter on the fixture in the same layer have the same categoryBits, and their maskBits would be set to only accept collisions from their same category.

So something like this (pseudo-code):

Fixture foregroundFixture;
foregroundFixture.filter.categoryBits = 1;
foregroundFixture.filter.maskBits = 1;

Fixture backgroundFixture;
backgroundFixture.filter.categoryBits = 2;
backgroundFixutre.filter.maskBits = 2;

Fixture gameplayFixture;
gameplayFixture.filter.categoryBits = 4;
gameplayFixture.filter.maskBits = 4;

This would ensure that each of these fixtures does not interact with each other, because they have different category bits and their mask bits only accept collisions from their same category.

If you then made a fixture like:

Fixture foregroundOrBackgroundFixture;
foregroundOrBackgroundFixture.categoryBits = 3;
foregroundOrBackgroundFixture.maskBits = 3;

then this fixture would collide with things on the foreground or backgorund layer, but not the gameplay layer.

Note that I haven't tested this example, but you can see how these bits work by looking at b2ContactFilter::ShouldCollide in WorldCallbacks.cpp.
I think I get this a bit better now... There are 16 collision categories, each of which is represented as a bit in the categoryBits bitfield. You set up the maskBit bitfield to flag which of the collision categories a given shape will collide with. Is that about right? And then there's groupIndex, which is a headache for a different time...

So, I've sort of got this working. I can "move" shapes between layers by setting their category and mask bits, and then calling b2World::Refilter on the shape. This kind of works, but I have run into some oddities: If a character is stood on a platform in the midground, and the platform is moved to the background, the player falls down. But then if that platform is moved from the background back to the midground, the character and platform end up occupying the same space, and Box2D doesn't always react well. Sometimes it pushes the character out of the platform, but sometimes it doesn't. The direction the character gets pushed isn't always desirable. I'd like to have more control over this.

So, how would I detect that a platform is about to move into "occupied" space in a different layer, so I can inform the occupants and get them to do something appropriate (move smoothly out of the way in a direction of my choosing, or play a crushed-to-death animation, or whatever)? Should I defer the refiltering for a frame whilst I create a dummy sensor shape to see what it's going to collide with? Or some kind of AABB test, or what?
You should be able to use world.QueryAABB to query the world for those fixtures that intersect with the given AABB. With that you can do your processing to animate them out of the way or whatever handling you would like to do.
Thinking about it, an AABB query isn't quite what I want, or at least isn't the whole solution, even if it's part of the solution. If the shape that's moving between layers is anything other than an unrotated box, for instance, I'm going to get false positives. Even if the shape IS an unrotated box, the shapes it could potentially collide with might not be (since the AABB query only gives potential overlaps, not actual overlaps, so more filtering is needed). I'm only expecting to have to run tests like this once a frame (at most), so I'm more interested in something accurate than something super-fast. What's the best way to go about this? Create a sensor shape that's filtered appropriately, run the test and then destroy the filter shape?

Original thread: https://box2d.org/forum/viewtopic.php?t=4145