I am using box2d on the iphone to create a game. I have a body that is effected by gravity to move down and not right or left. It will get hit by another body and will then be moving right or left. I then have a reset button which moves the body back to its starting point. The only problem is that it is still moving right or left. How can I counteract the forces that a ball is already traveling? How can I get rid of this right and left movement when I reset my game?
Asked
Active
Viewed 1.2k times
1 Answers
27
Box2d automatically clears the forces each simulation step. I think you are just changing your body's position when resetting, but not its velocity. Add this code to your reset method:
body->SetLinearVelocity(b2Vec2(0,0));
body->SetAngularVelocity(0);
-
I've tried this way but doesn't work when two object close to each other http://stackoverflow.com/questions/39716111/cocos2dx-unable-to-set-velocity-0-0 – TomSawyer Sep 27 '16 at 08:04
-
@TomSawyer: they probably collide after you reset the velocities and that causes their velocity to change again – Andrew Sep 27 '16 at 18:36
-
I think resetting the velocity make one object stand still. it's impossible to collide to the other. Anyway to make sure one object has been stopped? – TomSawyer Sep 27 '16 at 20:39