Box2D Multiple Worlds for multiple game layers

I would appreciate a comment on how sane my approach is from someone who knows the library internals. 🙂

This is regarding the C++ library. I have a 2D simulation that has layers. There is a physical simulation on multiple layers. Objects on one layer never collide with another layer.

My implementation of Box2D for this environment is to have a b2World for each layer. This is especially nice because I can specify different Box2D simulation resolutions for different layers to make scene-specific perf tradeoffs.

A typical level in my game has 15 or so layers so I am only spawning b2Worlds for each layer that needs it in order to conserve resources.

There are times in the gameplay where an object jumps from one layer to another. In the b2World, I am making a copy of the object in one b2World and removing it from the other. I suspect I'll need to eke out what forces and torques are being applied to the object so that it is a clean transition.

Thanks for reading!

Michael Labbe
It seemed quite sane until you said 15 layers. I don't think 15 would be a problem, but I am wondering if you know about the possibilities for collision flags of fixtures. You can set the group id for fixtures so that entire groups of objects will never collide with each other, or more selectively with the collision category/mask bit values. These are 16 bit values but can safely be changed to 32 bit if you need more control. There is also the ShouldCollide callback you can set as well.

The only time you would really need to have a separate world is as you say, when you want them to use different step times/frequencies. Making objects jump between layers would be easier if all you had to do was change their fixture's collision settings.
Separate worlds may hurt memory usage slightly, but you'll reap performance gains if you have many objects. One SVG becomes one b2World, and separate worlds was the cleanest way to manage these separate layers.

I also, perhaps unusually, use Box2D for my UI system, including all dialog boxes. It was very easy to map WM_MOUSE_DOWN or UIView.touchesBegan and associated events to a Flash UI button behavior. UI is the main use case where I want to activate and deactivate b2Worlds throughout gameplay.
oh yeah...
if the worlds have a lot of calculation involved, separating them would be an easy way to get multi-threading for better performance on multi-core CPUs.
irresistible force wrote:
It seemed quite sane until you said 15 layers. I don't think 15 would be a problem, but I am wondering if you know about the possibilities for collision flags of fixtures.


Thanks for pointing this out. The most reasonable implementation for me seems to be a single b2World for the 4 layers that gameplay happens on. The rest of the layers are graphics-only. I burn a nybble on collision checks for the 4 gameplay layers.

I can spawn additional rbWorlds for effects layers that need physical simulation outside of the gameplay world.

irresistible force wrote:
if the worlds have a lot of calculation involved, separating them would be an easy way to get multi-threading for better performance on multi-core CPUs.


This will become important to me in a couple weeks because I have multiple, concurrent game levels simulating on the server. I tried looking around on the net for reports of people running multiple b2Worlds on concurrent threads but I did not see any. Have you witnessed anyone getting this to work?
I have not seen any either, but as long as the worlds are completely independent it should be just a matter of creating a thread in the normal way for whatever OS you're on.

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