0
votes

So in my game I have Cocos2D + Box2D integrated. I know it is not recommended but I have my b2Bodys follow my CCSprites because I only need the collision part of Box2D. Anyway, using PhysicsEditor, I have created the most efficient body shapes I can and my game normally runs fine at 60FPS. My only issue is that, I want to prepare for the worst here. Lets say just under a really rare occurrence (like 1 in 10,000) the game lags for 5 seconds so that the FPS is around 10FPS. This would cause my main character to not collide with the platform under it. And in turn, it would make the character fall through the platform and the game would end unintentionally. So overall I just want to have a fallback plan when this happens.

So this is how I call the Box2D step:

world->Step(1/60.0f ,8, 8);

Then in my update method I do this to detect the collisions:

std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
    pos != _contactListener->_contacts.end(); ++pos) {
    MyContact contact = *pos;

    b2Body *bodyA = contact.fixtureA->GetBody();
    b2Body *bodyB = contact.fixtureB->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
        CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
        CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();

        if (spriteA.tag == 1 && spriteB.tag == 2) {
            toDestroy.push_back(bodyA);
        } else if (spriteA.tag == 2 && spriteB.tag == 1) {
            toDestroy.push_back(bodyB);
        }
    }
}

So how would I adjust what I am doing now to deal with the possible scenario I explained above? Thanks!

2

2 Answers

0
votes

You will not be having this problem because your Box2D step interval is fixed. Your simulation will simply slow down when the framerate decreases.

It might occur if the timestep were multiplied by the frame's delta time. But even then you ought to first verify that you'll be having these problems by forcing the game to run at 10 fps before making any code changes.

0
votes

Box2D have the option to define objects as bullets. This is to handle that fast moving objects don't pass through solid objects.

The syntax is something like this:

bodyDef.bullet = true;

This makes Box2D do a sweep from the old to the new position, and would catch those obstacle that you did not hit due to low frame rates.