我正尝试在我的 XNA 游戏中实现转向行为,大部分功能都已经实现,但有些小问题需要帮助。 这些行为本身工作良好,问题在于更新的时机和幅度。
(我正在使用 SharpSteer (目前几乎完全使用),可以在这里找到它:
http://sharpsteer.codeplex.com/SourceControl/changeset/view/18102)
我正在努力解决 Update()
方法。 目前我正在使用这个方法(直接来自 SharpSteer):
// apply a given steering force to our momentum, // adjusting our orientation to maintain velocity-alignment. public void ApplySteeringForce(Vector3 force, float elapsedTime) { Vector3 adjustedForce = AdjustRawSteeringForce(force, elapsedTime); // enforce limit on magnitude of steering force Vector3 clippedForce = Vector3Helpers.TruncateLength(adjustedForce, MaxForce); // compute acceleration and velocity Vector3 newAcceleration = (clippedForce / Mass); Vector3 newVelocity = Velocity; // damp out abrupt changes and oscillations in steering acceleration // (rate is proportional to time step, then clipped into useful range) if (elapsedTime > 0) { float smoothRate = Utilities.Clip(9 * elapsedTime, 0.15f, 0.4f); Utilities.BlendIntoAccumulator(smoothRate, newAcceleration, ref acceleration); } // Euler integrate (per frame) acceleration into velocity newVelocity += acceleration * elapsedTime; // enforce speed limit newVelocity = Vector3Helpers.TruncateLength(newVelocity, MaxSpeed); // update Speed Speed = (newVelocity.Length()); // Euler integrate (per frame) velocity into position Position = (Position + (newVelocity * elapsedTime)); // regenerate local space (by default: align vehicle's forward axis with // new velocity, but this behavior may be overridden by derived classes.) RegenerateLocalSpace(newVelocity, elapsedTime); // maintain path curvature information MeasurePathCurvature(elapsedTime); // running average of recent positions Utilities.BlendIntoAccumulator(elapsedTime * 0.06f, // QQQ Position, ref smoothedPosition); }
这可以工作,但由于 elapsed time 乘数,帧与帧之间的变化非常大(我认为 SharpSteer 使用的是它自己的游戏时钟,而不是 gameTime,我正在使用 gameTime)。
我正在寻找一种方法来大幅减慢速度,并在大于 1.0f 的范围内使用速度值。
目前我使用的速度为 1.0f,最大力为 1.0f。 我注意到 Velocity 总是 (0,0,0),elapsedTime 是 16(这每秒调用 60 次),smoothRate 总是 0.4f,这也是因为 elapsedTime 太大了。
如果有人能帮我稍微平衡一下车辆的速度,我将不胜感激。
回答:
增加质量将抑制(减慢)加速度,对于进入该方法的相同大小的力。 你确定你设置了合适的质量值吗?
此外,你的速度是否代表单位/秒? 如果是这样,那么 16 毫秒的经过时间应该是 0.016;