摩擦制限加速を使用して最大速度を正確に設定する
前の回答は、摩擦が速度に制限を設定できることは正しいですが、制限の設定に関する詳細がいくつか抜けていました。
最初に速度変数
vel = 0; //in pixels per frame. Start at 0 (or wherever you want)
そして、フレームあたりのピクセル単位で特定の最大速度が必要です
maxVelocity = 100; // set the maximum speed
そして、フレーム2あたりのピクセル数で特定の最大加速度があります。
maxAccel = 1; // The max acceleration will occur when accelerating from 0
ここで、速度が 100 に達したときに加速を停止するために必要な摩擦を取得する必要があります。未知の摩擦を呼び出します。
friction = null; // what we need to find out
方程式を使用します
vel = vel + maxAccel; // accelerate
vel = vel * friction; // add friction
をfriction超えないように、加速後に速度を下げる必要がありmaxVelocityます。したがって、 is equal tofrictionに適用される場合、その値が必要です。maxVelocity + maxAccelmaxVelocity
maxVelocity = (maxVelocity + maxAccel) * friction;
方程式を並べ替えて、未知数を解くだけfrictionです。
friction = maxVelocity / (maxVelocity + maxAccel);
frictionこれで、速度が を超えないようにするための値が得られましたmaxVelocity。
少し拡張するには、ブーストを追加して短期的に速度を上げたいと思うかもしれませんが、そのブースト速度が限界を超えないようにする必要があります。friction新しいものに再計算することもできmaxVelocityますが、ベロシティ カーブの上端の加速度が低いため、必要なキックが得られない可能性があります。
もう 1 つの方法は、必要な追加の加速を計算することで、同じ摩擦を維持しながら追加の加速を提供することです。新しい変数が必要です。
maxBoostVelocity = 120; // max boost velocity
boostAccel = null; // this will have to be calculated using the friction we have
上記と同じロジックを使用しますが、新しい最大速度と既知の摩擦に関して摩擦ソリューションを再配置します。
boostAccel = ((maxBoostVelocity / friction )- maxBoostVelocity);
ブーストには追加の加速のみを使用するのが好きなので、既存の加速値に触れる必要はありません。したがって、計算されたブーストから元の加速度を差し引いて、必要なものを取得します。
boostAccel = boostAccel - maxAccel;
したがって、一部のコードでは
// the known limits
vel = 0; //in pixels per frame. Start at 0 (or wherever you want)
maxVelocity = 100; // set the maximum speed
maxAccel = 1; // The max acceleration will occur when accelerating from 0
maxBoostVelocity = 120; // max boost velocity
accelerate = false; // flag to indicate that we want to accelerate
boost = false; // flag to indicate we want to boost
// the unknown friction and boost accel
boostAccel = null; // this will have to be calculated using the friction we
friction = null; // what we need to find out
function applyAcceleration()
if(boost){ // is boosr flag true then add boost
vel += boostAccel ; // boost
}
if(accelerate || boost){ // is accelerate or boost flag true
vel += maxAccel; // accelerate
}
// always apply the friction after the accelerations
vel *= friction; // apply friction
}
// in the init function calculate the friction and boost acceleration
function init(){
friction = maxVelocity / (maxVelocity + maxAccel);
boostAccel = ((maxBoostVelocity / friction )- maxBoostVelocity) - maxAccel;
}