1
votes

I set the gravity scale property of a Box2d body. I would like to change the gravity scale when the body reaches a specific position. Can this be done? If yes, how can it be achieved.

2

2 Answers

0
votes

In my case I set constant speed for falling objects like this.

#define MIN_SPEED 2.0f

-(void)update:(ccTime) dt
{
    b2Vec2 vel = self.body->GetLinearVelocity();

    if( ABS(vel.x) > MIN_SPEED )
    {
        if(vel.x>0)
            vel.x = MIN_SPEED;
        else
            vel.x = -(MIN_SPEED);
    }
    if( ABS(vel.y) > MIN_SPEED )
    {
        if(vel.y>0)
            vel.y = MIN_SPEED;
        else
            vel.y = -(MIN_SPEED);
    }

    self.body->SetLinearVelocity(vel);
}
0
votes

Check the position and use SetGravityScale() :

b2Vec2 pos = body->GetPosition();

if (pos.x > minPosX && pos.x < maxPosX
    && pos.y > minPosY && pos.y < maxPosY) {
        body->SetGravityScale(theScalingFactor);
}