adding b2world to a CCLayer

104 views Asked by At

I have a CCScene that has many CCLayers on it .

1. I would like to add a specific layer that has a b2world in it (box2d) . I need it to be a completely separate layer that i add above the current scene,than remove . Can i just create a world in a layer class and add that class as a layer ? or i need to have a scene?

2. How you define the gravity vector to be 90 degrees to the iPhone screen ? (its usually parallel to the screen and point down to the home button,and not "3d" . ) .

Thanks a lot .

1

There are 1 answers

2
FuzzyBunnySlippers On BEST ANSWER

For Question 1:

That is a very odd use of CCLayer. The "Scene" is a container for "Layers", where each layer is composited to form the overall scene.

Putting the physics "world" into the Scene makes (some) sense, since you may be displaying debug information in one layer, sprites for something in the physics world in another, hud information in a third, sprites for other physics objects in a fourth layer, etc.

However, putting the b2World* into the layer itself means you have to jump through hoops to share any of the physics information from the model (physics) with any other layers.

Since you will be removing the layer from the scene in your CCScene derived class anyway, let it destroy the physics then as well:

void MyScene::DestroyPhysicsLayer()
{
    removeChild(_physicsLayer);
    delete _world;
    _world = NULL;
}

I once had this (maybe not so) crazy idea for running two physics models at the same time, so that you could have something "in the background" and something "in the foreground" (like a distant battle and near battle) and move between them by jumping the objects from one physics "world" to another when the user pressed a key. In this case, you would still put the physics in the scene...the layers would just have to know which physics world they were using. Are you adding a "physics layer" just so you can do something like this?

Being able to throw a "Box2D Debug Layer" into the scene at any time is really handy for debugging, figuring out why sprites don't line up, etc...you make this much harder when you put the physics into the layer.

For Question 2:

"Down" is usually something like: b2Vec2 gravity(0,-9.8f); // 9.8 m/s

I usually set up the application so that it only has one orientation (landscape or portrait, but not both). The screen should auto-rotate so "down" still works as expected.

Was this helpful?