如何施加不应连续的力
我有一个可以在按钮的帮助下移动的身体,这就是我正在做的事情:
-(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);
}
我只想在条件失败时停止身体移动。我试图用 cpBodyResetForces(body) 将所有力重置为 0,但这永远不会起作用 - 它只是继续移动。
怎样才能让身体停止移动呢?
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;
cpBodyApplyForce(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
物体之所以继续运动,是因为它的动量。要让对象“自动”停止,您需要将空间阻尼设置为大于 0。来自文档:
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:
如果您读过一些基础物理学,您会记得,只需将所有力重置为零 - 如果没有摩擦 - 身体将继续永远移动。您需要做的就是向身体移动的空间添加摩擦力,或者消除原始力并向相反方向施加力,直到身体停止 - 然后消除所有力。
使用物理引擎可能有一种非常有效的方法来做到这一点,但我只是自己学习所有这些 - 希望这有帮助!
干杯!
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!