Leena
Leena

Reputation: 109

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

Upvotes: 0

Views: 2712

Answers (2)

Lukman
Lukman

Reputation: 19164

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

Upvotes: 5

Bijoy Thangaraj
Bijoy Thangaraj

Reputation: 5546

You should check out the answer to this question: How to stop the forces acting on a body in box2d

On release of the joystick, you should reset the velocity of your box2d body.

Upvotes: 0

Related Questions