我正在尝试为我的物理引擎添加弹簧,虽然算法本身有效但它们非常僵硬(必须输入值<=约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);
答案 0 :(得分:1)
很难判断你的结果是否真实而没有更多的信息(什么是低约0.1?),但我看到一些看起来像错误的东西。