Search code examples
cocos2d-iphonebox2d-iphonegravity

Stop movement of body in zero gravity


I have one issue regarding box2d and cocos2d. My world have zero gravity and i am working in tile base game. I am using sneak joystick for movement of sprite and its move perfect but when i release point to joystick my sprite body can not stop because of some force. I want to stop that movement of sprite when joystick release.

-(void)update:(ccTime)dt :(b2Body *)ballBody :(CCSprite *)player
{
    CGPoint scaledVelocity=ccpMult(joysticks.velocity, 2);

    NSLog(@"Joystick Velocity X: %f",joysticks.velocity.x);
    NSLog(@"Joystick Velocity Y: %f",joysticks.velocity.y);

b2Vec2 force=b2Vec2(scaledVelocity.x/PTM_RATIO,scaledVelocity.y/PTM_RATIO);
       ballBody->ApplyLinearImpulse(force, ballBody->GetWorldCenter());    

}

Here scaledVelocity value is approximate 0 to 1. When i release joystick that time value of joystick is 0.0

Please help me i am stuck since last 5 days. Please help me.

Thanks in advance


Solution

  • Do you want the b2Body to immediately stop or to slow down (and eventually stop)?

    To make it stop immediately:

    ballBody->SetLinearVelocity(b2Vec2(0,0));
    

    To make it slow down:

    ballBody->SetLinearDamping(10.0); // experiment with the damping factor value until you get the right deceleration