0

How could I restrict an impulse ? I would like the body to jump fast, and restrict his jump after that.

I am looking for something like the following, a friction after the impulse, but this is not working (the player stays at its position in the y-axis, as vec2.y will equal "0")

//after a touch
body->ApplyLinearImpulse( b2Vec2(x,y), body->GetPosition() );
vec2 = body->GetLinearVelocity();

//in the tick method, called every step
vec2.y = vec2.y * 0.99;
CCLOG(@"vec2.y : %f", vec2.y);
body->SetLinearVelocity(vec2);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

0

I have been searching for this quiet along time but done it finally:

//call this after touch
       body->ApplyLinearImpulse(b2Vec2(0, 35000), body->GetPosition());

        [self schedule:@selector(CheckVelocity) interval:0.0001];
        [self scheduleOnce:@selector(toggleCheckForLanding) delay:.5];


-(void)CheckVelocity
{
set a max velocity according to your jump i have set it 13.....
    int velocitymax = MAX_VELOCITY;//13
    b2Vec2 vel = body->GetLinearVelocity();
    if(vel.y>velocitymax)
    {
        vel.x = 0;
        vel.y = MAX_VELOCITY;
        body->SetLinearVelocity( vel );
    }

}


-(void) toggleCheckForLanding
{
    [self unschedule:@selector(CheckVelocity)];
    canCheckForLanding_ = YES;
}
slfan
  • 8,950
  • 115
  • 65
  • 78