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
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...
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...