Ios 计算将Box2D车身移动到特定位置的正确作用力-Box2D
我有一个关于将Box2D实体移动到特定位置而不使用此示例的问题Ios 计算将Box2D车身移动到特定位置的正确作用力-Box2D,ios,iphone,math,box2d,physics,Ios,Iphone,Math,Box2d,Physics,我有一个关于将Box2D实体移动到特定位置而不使用此示例的问题 body->SetTransform(targetVector,body->GetAngle()) 我有一些适用于applyForce()的代码: 但是,当maxSpeed设置为高位时,身体会越过该点快速移动 那么,是否有人知道如何计算一个力(ApplyForce)或一个impluse(ApplyLinearImpulse)来在特定时间内将身体移动到目标位置(非常精确) 或者使用上述代码的解决方案。我的意思是计算max
body->SetTransform(targetVector,body->GetAngle())
我有一些适用于applyForce()的代码:
但是,当maxSpeed
设置为高位时,身体会越过该点快速移动
那么,是否有人知道如何计算一个力(ApplyForce
)或一个impluse(ApplyLinearImpulse
)来在特定时间内将身体移动到目标位置(非常精确)
或者使用上述代码的解决方案。我的意思是计算maxSpeed
,在特定时间内将身体移动到目标位置
在我的谷歌搜索中,我找到了来自iforce的一篇关于预测轨迹的有趣文章
(). 也许这也能帮上忙
提前谢谢你我想你说得基本正确,但你没有检查身体是否会在下一个时间步超过目标。以下是对我有效的方法:
b2Vec2 targetPosition = ...;
float targetSpeed = ...;
b2Vec2 direction = targetPosition - body->GetPosition();
float distanceToTravel = direction.Normalize();
// For most of the movement, the target speed is ok
float speedToUse = targetSpeed;
// Check if this speed will cause overshoot in the next time step.
// If so, we need to scale the speed down to just enough to reach
// the target point. (Assuming here a step length based on 60 fps)
float distancePerTimestep = speedToUse / 60.0f;
if ( distancePerTimestep > distanceToTravel )
speedToUse *= ( distanceToTravel / distancePerTimestep );
// The rest is pretty much what you had already:
b2Vec2 desiredVelocity = speedToUse * direction;
b2Vec2 changeInVelocity = desiredVelocity - body->GetLinearVelocity();
b2Vec2 force = body->GetMass() * 60.0f * changeInVelocity;
body->ApplyForce( force, body->GetWorldCenter(), true );
有一种方法可以使单次施加的力移动给定的距离(前面的答案表明,您可以在未来的帧中通过附加力来纠正计算中的错误): 已知的限制:
1) LinearVelocity effect was not tested;
2) calculation was tested with body linear damping = 0.5f. Appreciate, if somebody know how to add it into formula;
3) magic number 30.45f - maybe this could be fixed by point 2 or by world frame delta.
public static void applyForceToMoveBy(float byX, float byY, Body body) {
force.set(byX, byY);
force.sub(body.getLinearVelocity());
float mass = body.getMass();
force.scl(mass * 30.45f);
body.applyForceToCenter(force, true);
}
1) LinearVelocity effect was not tested;
2) calculation was tested with body linear damping = 0.5f. Appreciate, if somebody know how to add it into formula;
3) magic number 30.45f - maybe this could be fixed by point 2 or by world frame delta.