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. 您不应该在最后几行中将力除以二。(仅此一项就可以使您增加两倍。)
  2. 如果“阻尼”应该是一种耗散力(弹簧中的内部摩擦),那么它指向错误的方向。如果 __springDampening 很高,这可能会导致病态行为。
于 2009-10-29T17:27:27.360 回答