0

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

Leena
  • 111
  • 1
  • 10

2 Answers2

5

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
Lukman
  • 16,877
  • 5
  • 51
  • 60
  • I've tried it to set Velocity = 0.0 but didn't work when the object stick to each other http://stackoverflow.com/questions/39716111/cocos2dx-unable-to-set-velocity-0-0 – TomSawyer Sep 27 '16 at 06:44
0

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.

Community
  • 1
  • 1
Bijoy Thangaraj
  • 4,940
  • 3
  • 35
  • 64