box2d many static object

302 views Asked by At

I'm on stage a lot of static objects - obstacles. I add them as follows:

 b2BodyDef groundBodyDef;
                    groundBodyDef.position.Set(i/PTM_RATIO, j/PTM_RATIO);

                    b2Body *groundBody = world->CreateBody(&groundBodyDef);
                    b2PolygonShape groundEdge;
                    b2FixtureDef boxShapeDef;
                    boxShapeDef.shape = &groundEdge;

                    groundEdge.SetAsBox(64/PTM_RATIO, 64/PTM_RATIO);
                    groundBody->CreateFixture(&boxShapeDef);

And it turns out that such objects around 2000 and eventually begins to slow down the application .. How can I add a simple square girlfriend static objects just for the Coliseum?

1

There are 1 answers

2
Losiowaty On

You should remove or reuse bodies that leave the screen. For example, in a side scrolling game, when an obstacle leaves throught he left screen edge it is most likely to not return, so it is safe to either remove it (so that box2d won't take it into account when checking collision, and also you win;t have to take them into account in your moethods) or reuse them - you could change the position of such body, so that it will be again on the right.

Either way, you shouldn't create a lot of bodies, as this is a sure and fast way to hog down your performance.