1

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.

4

2 回答 2

0

就我而言,我为这样的下落物体设置了恒定速度。

#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);
}
于 2013-06-12T15:18:39.600 回答
0

检查位置并使用 SetGravityScale() :

b2Vec2 pos = body->GetPosition();

if (pos.x > minPosX && pos.x < maxPosX
    && pos.y > minPosY && pos.y < maxPosY) {
        body->SetGravityScale(theScalingFactor);
}
于 2013-06-19T09:19:53.880 回答