我有一個關於如何將Box2D主體移動到特定位置的問題,而不使用此舉例。計算正確的衝突或力量將Box2D主體移動到特定位置 - 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)的有趣文章。也許這也可能是幫助?
預先感謝您