私は、キーボードの矢印を使用して球に力を加える「物理シミュレーション」(いわば) を書いています。この最新のイテレーションでは、球体が空中にあるようにドラッグを追加しました。次に、特定の値に対して、抗力計算が爆発し始めます! 数が大きくなりすぎて、次に Infinity とその直後の NaN (Infinity / Infinity 分割の原因)。
http://gool.jit.su/the-dragでそれが起こっているのを見ることができます。コンソールを開き、左または右に移動を開始します。数秒で壊れます。ドラッグ値が NaN になる直前まで記録されていることをコンソールで確認できます。(私たちは大きな力と非常に小さな質量を持っています)
何が起こっているのか、なぜそれが起こっているのかを本当に理解しようとしていますが、おそらく、私が見つけることができなかった同様の問題に関する情報、または数を制御するための何らかのベストプラクティスに関する情報がすでにあるでしょう...おそらく値をMAXにして、毎回それをテストします...
どんなアイデア、助け、提案も大歓迎です。私は何か重要なことを学ぼうとしていると思いますが、それでも正しい方向へのプッシュが必要です. :)
UPDATE 2:@Betaのテスト(ちょっと)
@Beta のコメントの後、私はこのテストを行いましたが、実際に彼の計算は私のシミュレーションがどこで壊れているかを示しているようです。(テストが true を返した場合は x 軸のコンソール ログ速度、それ以外の場合は false)
更新 1 : いくつかのコード
ほとんどの「物理」はここで発生します。
update: function update(k, dt) {
var up = k.UP,
right = k.RIGHT,
down = k.DOWN,
left = k.LEFT;
up = up && -this.output;
right = right && this.output;
down = down && this.output;
left = left && -this.output;
this.force.x = left + right;
this.force.y = up + down;
this.calculate_drag(this.velocity);
if (!isNaN(this.drag.x)) {
console.log(this.drag);
}
// this.net_force.x = this.force.x;
// this.net_force.y = this.force.y;
this.net_force.x = this.force.x + this.drag.x;
this.net_force.y = this.force.y + this.drag.y;
this.acceleration.x = this.net_force.x / this.mass;
this.acceleration.y = this.net_force.y / this.mass;
this.velocity.x += this.acceleration.x / (1000 / dt);
this.velocity.y += this.acceleration.y / (1000 / dt);
this.momentum.x = this.mass * this.velocity.x;
this.momentum.y = this.mass * this.velocity.y;
this.position.x += (this.velocity.x * global.METRE) / (1000 / dt);
this.position.y += (this.velocity.y * global.METRE) / (1000 / dt);
this.energy += (abs(this.net_force.x) + abs(this.net_force.y)) / (1000 / dt);
}
そしてここ:
calculate_drag: function calculate_drag() {
var c = 0.47,
a = PI * this.radius * this.radius,
rho = 1.22,
direction = function direction(velocity) {
if (velocity === 0) {
return 1;
}
return velocity / abs(velocity);
};
this.drag.x = -0.5 * c * a * rho * this.velocity.x * this.velocity.x * direction(this.velocity.x);
this.drag.y = -0.5 * c * a * rho * this.velocity.y * this.velocity.y * direction(this.velocity.y);
}
gee.js の gPrototype の両方のメソッド。