开发者

How to apply a force which should not be continuos

I have a body which I move with the help of a button, here is what I'm doing:

-(void) step: (ccTime) delta
{
  int steps = 2;
  CGFloat dt = delta/(CGFloat)steps;

  for(int i=0; i<steps; i++){
    cpSpaceStep(space, dt);
  }
  cpSpaceHashEach(space->activeShapes, &eachShape, nil);
  cpSpaceHashEach(space->staticShapes, &eachShape, nil);


  if(MoveBody)  
  {     
    cpFloat movementPadding = 0.1;
    cpBodyApplyFo开发者_高级运维rce(body, 
    cpvmult(ccp( 10, 0), movementPadding), cpvzero);   
  }
  else
    cpBodyResetForces(body);
}

I just want to stop the body moving whenever the condition fails. I am trying to reset all forces to 0 with cpBodyResetForces(body), but this never works - it just keep on moving.

How can I stop the body moving?


If you read up on some basic physics, you will recall that by simply resetting all forces to zero - and if there is no friction - the body will continue to move forever. What you need to do is either add friction to the space the body is moving in, or kill the original force and apply force in the opposite direction until the body comes to a stop - THEN kill ALL forces.

Using a physics engine there may be a very efficient way to do this but im just learning all of this myself - hope this is helpful!

cheers!


The reason that the object is moving on is due to it's momentum. To let the object stop "automaticaly" you need to set damping of space larger than 0. From docs:

> cpFloat cpSpaceGetDamping(const cpSpace *space) void
> cpSpaceSetDamping(cpSpace *space, cpFloat value) 

Amount of simple damping to apply to the space. A value of 0.9 means that each body will lose 10% of it’s velocity per second. Defaults to 1. Like gravity can be overridden on a per body basis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜