0

物理エンジンにばねを追加しようとしていますが、アルゴリズム自体は機能しますが、非常に硬いです(目に見える動きを引き出すには、値<=約0.1を入力する必要があり、非常に小さく設定する必要があります)。正しく動作するために)。次のコードを改善して、0.1から1の範囲の値でそれほど硬くないようにするためのヒントはありますか?

Vector2 posDiff = _a1.Position - _a2.Position;
Vector2 diffNormal = posDiff;
diffNormal.Normalize();
Vector2 relativeVelocity = _a1.Velocity - _a2.Velocity;
float diffLength = posDiff.Length();

float springError = diffLength - _restLength;
float springStrength = springError * _springStrength;
if (!float.IsPositiveInfinity(_breakpoint) && (springError > _breakpoint || springError < -1 * _breakpoint))
{
    _isBroken = true;
}
float temp = Vector2.Dot(posDiff, relativeVelocity);
float dampening = _springDampening * temp / diffLength;

Vector2 _force = Vector2.Multiply(diffNormal, -(springStrength + dampening));
_a1.ApplyForce(_force / 2);
_a2.ApplyForce(-_force / 2);
4

1 に答える 1

1

これ以上の情報がなければ、結果が現実的であるかどうかを判断するのは困難ですが (0.1 について何がそんなに低いのでしょうか?)、バグのように見えるものがいくつか見られます。

  1. 最後の数行で force を 2 で割ってはいけません。(それだけで 2 倍になります。)
  2. 「減衰」が消散力 (バネの内部摩擦) であると想定されている場合、それは間違った方向を指しています。__springDampening が高い場合、これは異常な動作につながる可能性があります。
于 2009-10-29T17:27:27.360 に答える