How to use force that doesn't have to be continuous
I have a body that I move with a button, this is what I do:
-(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;
cpBodyApplyForce(body,
cpvmult(ccp( 10, 0), movementPadding), cpvzero);
}
else
cpBodyResetForces(body);
}
I just want to stop the movement of the body whenever the condition fails. I'm trying to reset all forces to 0 with using cpBodyResetForces(body)
, but that never works - it just keeps moving.
How can I stop body movement?
If you're reading some basic physics, be reminded that simply by dropping 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 the force in the opposite direction until the body stops - THEN kill ALL forces.
Using a physics engine can be a very efficient way to do this, but I'm just learning it all myself - hope it's helpful!
Hooray!
a source to share
The reason an object is moving is because of momentum. In order for the object to stop "automatically" you need to set the space fade to greater than 0. From the docs:
> cpFloat cpSpaceGetDamping(const cpSpace *space) void
> cpSpaceSetDamping(cpSpace *space, cpFloat value)
The amount of simple damping to apply to the space. A value of 0.9 means that each body will lose 10% of its speed per second. Default 1. Like gravity can be overridden based on the body.
a source to share