Search code examples
box2dcocos2d-x

cocos2d+box2d disable gravity to separate bodies


Now my project included gravity for the world. But now you want to disable gravity for certain objects. Now I'm doing this:

void GameScene::initPhysics()
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();

    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);
    world = new b2World(gravity);

    // Do we want to let bodies sleep?
    world->SetAllowSleeping(true);

    world->SetContinuousPhysics(true);
}

Prompt how to turn off gravity for a particular object?


Solution

  • The b2Body class has a gravityScale property. Call body->SetGravityScale(0.0) on every body that you don't want to be affected by gravity.