Z Axis depth question

Hi,
This is my first time posting here, so hopefully I'm in the right area to ask this. Basically, I'm curious if it's possible to do a 2.5D game with Box2D. Something where all the physics is 2D, yet there is a depth that sprites can go and still get all their transformations, etc.

I'm working on a 2.5D sprite engine and I've been stumped on what physics engine to use. I really like the design of Box2D from what I've seen, plus it seems actively developed on a regular basis, also it's very small compared to other libraries I've looked at (space is somewhat of an issue), and still has excellent features. I don't want to go with something huge where I only use a small portion of it (a 3D physics engine mostly clamped to 2D except for some sprite depths).

I'm mostly thinking about games that have a "virtual" 3D background. For example, Double Dragon type games where it's a fighting/platformer except they can travel forward and back a bit as well. Or Star Ocean where the battle system was basically 2D sprites on a plane. Hopefully this explains what I'd like to do.

I thought about using some kind of 2D physics "slice" where each sprite plane had it's own collisions, etc, but I have no idea if that's possible (or if it is, if it's efficient). If it were, would it be possible to move a dynamic body around manually without messing up it's internal data? Or any other suggestion someone might have. The engine is in OpenGL so sprites are fast and it's easy to use (or even use the 3D aspect of it to emulate a "mode 7" style effect).

Anyway, if anyone has any suggestions or help, I'd really appreciate it! Great work btw on this library!

P.S. Completely random thing I just thought of: is it possible to have gravitational nodes where all things fall toward its center? Like a planet or something? Not necessary, but thought I'd bring it up while I remembered.
Box2D has support for something like "layers", yes. However, it is not designed to warn you about collisions along the "Z" axis, so, in the Double Dragon example, you can rely on Box2D to stop a character from walking into a wall that is parallel to the "Z" axis (e.g. the character was walking left or right), but you can't rely on Box2D to stop a character from walking into a wall that is perpendicular to the "Z" axis (e.g. the character was walking to the foreground or background). There are probably workarounds, though, wherein you would move the character along the "Z" axis, test for collisions with a timestep of zero, and then move back if there was a collision.

Anyway, to achieve a "layer" effect, you have to use collision filters. Collision filters are called right before Box2D collides two shapes, and if the filter returns false, no collision occurs. You can create a custom collision filter, but the one that ships with Box2D is frequently good enough. It has provides two variables per shape called "categoryBits" and "maskBits". You can set a shape's "categoryBits" to 0x01 to indicate that it is in layer one, and set its "maskBits" to 0x01 to indicate that it collides with other bodies in layer one. By default, Box2D uses a binary AND operator to compare category and mask bits, so you're limited to a small number of bits (16?).

Box2D doesn't particularly care how you render your shapes, so parallax scrolling should work.

Box2D does not support planetary gravity, though you can roughly simulate it by applying a force towards a point at every timestep.
Thanks for replying, I appreciate it. The solution for the planetary gravity seems simple enough. I thought about what you said about layers, and it does seem a bit limited of a solution for what I want to do (the layers idea wasn't my first choice for solving this, just a potential solution that popped up). So I started thinking of other ways to get around it. Then it hit me, why not use a completely different collision system? Would it be possible to use box2D for just the physics simulation but leave collision to another separate part of the engine?

I read about shapeless bodies, though I'm not sure if that's the way to do this. For example, I could use a library like coldet or something else to do my collisions, but handle the physics simulation in box2D. To the simulator, the objects would all be in the same plane and be affected equally, but in reality have different Z depths.

Not entirely sure if that would work (I guess with joints they would have to be at the same Z depth?), but it seems like it could. Any thoughts?

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