Partitioning the Simulation

Greetings all!

My name is Raymond Jacobs, owner of Ethereal Darkness Interactive (makers of Morning's Wrath and The Lost City of Malathedra)

We're currently working on a new game engine and have chosen Box2D for our physics engine. Overall it's worked great, but I am having one issue.

In our games we have various separate 'rooms' which are considered to be completely isolated from one another.

Box2D seems to present us with only a single large 'simulation pool', I've read the documentation and it says you can create multiple 'worlds'
which seem to essentially be simulation contexts, but there doesn't seem to be a mechanism for moving bodies from one world to the next.

Short of using multiple worlds there are lots of tricks I can think of to get the multi-room functionality i'm looking for but none seem ideal.

1. move unused objects ouside of simulation area, move back in later (hackish, plus seems like a lot of objects would remain in the simulation)
2. remove all bodies from simulation, create new ones, rinse and repeat for each room change (box2D makes up our positional model and i'd rather not store the transient data of moving, plus still a lot of objects in simulation at once)
3. contact filterer (tons of processing overhead)

So the question is what is the 'right' way to do this?

Thanks in advance for any help 🙂

-Raymond
Raymond,

I've recently been dealing with a very similar problem to yours. Although in my case the "rooms" are more like "worlds," I still wanted to be able to take the entire player body (which can contain many shapes and change due to interaction) and just plop it into a new world. Box2d doesn't let you do this.

So what I ended up doing is creating higher-level classes that wrap all of the Box2d entities (worlds, bodies & shapes) and provide me with the methods I wish were built in (and also control display). So, for example if I want to move a body to a new world, I just do something like so:

myBody.destroy();
newWorld.addChild(myBody);
myBody.create();

The "myBody" class keeps track of all the box2d information, like sub-shapes, and is able to recreate it.

I found that their is very little overhead in destroying and creating a new world (in your case "rooms") when the player is transported between the two. In fact I suspect that is much faster than any of the methods you described. To detect when the player leaves / enters a new world, I use sensors to build "doors."

Anyway that's the approach I took. I don't think there's a "right" or even a "standard" way. 🙂
How about a variation on 2:
Destroy all bodies associated with the old room that will not be carried over to the new room, translate all remaining bodies over to their new position in the new room, create all new bodies in the new room.

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