Search code examples
c++objective-ccocos2d-iphonebox2dobjective-c++

Box2d anti-gravity question


im using cocos2d with box2d.
Currently im trying to optimize a system that i have installed to make certain objects have 0 gravity.
In my tick method, if an objects property object.isGravitated = NO, an anti-gravity method will be called to use object.body->applyForce(b2Vec2(0,10)); to counteract the downward force. But this seems to be costly in terms of fps.
Have multiple (5 - 10) isGravitated = NO objects causes fps to drop about 10fps or so.

Does this seem right to you and any suggestions to improve this system?
Thanks


Solution

  • You could modify b2body directly, for it to contain a gravity factor. By default, set it to 1.0f, which means normal gravity. Set it to 0.0f for no gravity.

    Gravity is calculated using b2Island, so in b2Island.cpp look for the line (it's in the Solve method):

    b->m_linearVelocity += step.dt * (gravity + b->m_invMass * b->m_force);
    

    Modify it to multiply the gravity with the gravity factor.

    That should be fast, efficient, and not a lot of work.

    You'll have to do it in C or C++, not in Objective-C, as it's a C++ object.