Help with design part II

Hello all! thanks for helping with the first post. Got things working and all, but I met another major roadblock.

The game engine I'm writing is scene based, so my first approach on to get Box2D working in it was to create a map and, for each scene, load every body, fixture etc and assign them to the Box2d's world. Then I realized that every single object was being computed in the actual scene, because I registered all the bodies within box2d's world.

here is a code snippet

Code:

bool PhysicsManager::init() {
.
.
.
bodies_map[ map_it->first ].push_back(createBody( *list_it ));
.
.
.
}


b2Body* PhysicsManager::createBody(EntityFactory* entity) {
b2BodyDef bodyDef;
   b2Body* body;

   bodyDef.type = b2_dynamicBody;
   bodyDef.position.Set(iniBodyXPos, iniBodyYPos);
   bodyDef.userData = entity;
   body = world.CreateBody(&bodyDef);

// Define another box shape for our dynamic body.
   b2PolygonShape dynamicBox;
   dynamicBox.SetAsBox(iniBodyW, iniBodyH);

   // Define the dynamic body fixture.
   b2FixtureDef fixtureDef;
   fixtureDef.shape = &dynamicBox;

   // Set the box density to be non-zero, so it will be dynamic.
   fixtureDef.density = 1.0f;

   // Override the default friction.
   fixtureDef.friction = 0.3f;

   // Add the shape to the body.
   body->CreateFixture(&fixtureDef);   

   return body;
}


Ok. Now, which is obvious, in the Box2d's update loop, "ghost blocks" are interacting with the actors.

My question is: what would be a good way to implement Box2d in a scene based way? I thought on implementing a swap() member function which would free all registered bodies in the world and init them again. But I don't like this approach very much. I would like to carry the actors+bodies info in the map and swap accordingly...
I'm not sure what exactly you're trying to do here. You want to create box2d bodies without putting them into a box2d world? Unfortunately it doesn't work that way.
Also I'm not sure what you mean by ghost bodies...?

fwiw in my code I have something kinda similar to what you have there, but instead of being in a PhysicsManager class and passing info about the entity to create, the class for each entity type implements its own createBody function. I have a class called GameWorld to store overall information about the world such as the list of entities you have above, only it holds objects of my entity class and not b2Body*s - each entity knows it's b2Body if I ever need to get it. When an entity creates a b2Body, it sets itself as the user data of that body, so I can tell in the collision code what collided, as discussed earlier. The box2d world itself is a member variable of the GameWorld class.

hmm... now that I've written that out, there isn't a great deal different from yours, it's all just technicalities really. Anyway back to the main course... I still didn't get whether your simulation not behaving as you expect in some way or not?
hi!
maybe I wasn't clear enough. The simulation is working correctly indeed.

Quote:
I'm not sure what exactly you're trying to do here. You want to create box2d bodies without putting them into a box2d world? Unfortunately it doesn't work that way.


Exactly! I wanted to have all the bodies in the memory and then get them loaded in the Box2d world accordingly to each scene.

Quote:
Also I'm not sure what you mean by ghost bodies...?


As i was adding bodies to the box2d world and linking to my actors, there was bodies in the box2d world that wouldn't have an actor at the current scene. So, in the scene, the bodies were colliding with 'nothing' and doing their physical movements. This 'nothing' was because the calculations where done with the bodies created in the box2d world. Therefore I realized my error of judgment!

I will think a bit more on the solution you have in you


The solution you had in your game engine is almost similar to mine. But the question still stands: how you do manage to 'recreate' the box2d world after the loading of each scene? I mean, after the player finishes a level, what do you do in order to load the next level?

BTW, are you really Japanese? Your mastering of the English language is top notch!

Thanks!
Whenever I create an entity, the entity creates a box2d body for itself, so the loading of my level and the setting up of the box2d world are one and the same, so there is no 'recreation' necessary for the box2d world after loading. The level itself is basically the GameWorld class that I mentioned before, which gets thrown away and recreated between levels. Since it has the b2World as a member variable, the box2d world is also thrown away between levels.

no, I just live here. 日本語で会話したいならいつでもいいけど 😉

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