【发布时间】:2015-12-14 06:11:19
【问题描述】:
例如,我有一个关于将 Box2D 主体移动到特定位置而不使用它的问题。
body->SetTransform(targetVector,body->GetAngle())
我有一些适用于 applyForce (here) 的代码:
const float destinationControl = 0.3f;
b2Vec2 missilePosition = _physicalBody->GetPosition();
b2Vec2 diff = targetPosition - missilePosition;
float dist = diff.Length();
if (dist > 0)
{
// compute the aiming direction
b2Vec2 direction = b2Vec2(diff.x / dist, diff.y / dist);
// get the current missile velocity because we will apply a force to compensate this.
b2Vec2 currentVelocity = _physicalBody->GetLinearVelocity();
// the missile ideal velocity is the direction to the target multiplied by the max speed
b2Vec2 desireVelocity = b2Vec2(direction.x * maxSpeed, direction.y * maxSpeed);
// compensate the current missile velocity by the desired velocity, based on the control factor
b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);
// transform our velocity into an impulse (get rid of the time and mass factor)
float temp = (_physicalBody->GetMass() / normalDelta);
b2Vec2 finalForce = b2Vec2(finalVelocity.x * temp, finalVelocity.y * temp);
_physicalBody->ApplyForce(finalForce, _physicalBody->GetWorldCenter());
}
但是当maxSpeed高的时候,身体越过点越快。
那么有谁知道如何计算力 (ApplyForce) 或内冲 (ApplyLinearImpulse) 以在特定时间(非常准确地)将身体移动到目标位置。
或使用上述代码的解决方案。我的意思是计算maxSpeed 以在特定时间将身体移动到目标位置。
在我的谷歌搜索中,我发现了 iforce 关于投影轨迹的有趣文章 (here)。也许这也有帮助?
提前谢谢你
【问题讨论】:
标签: ios iphone math box2d physics