平滑旋转炮塔并保持精度

时间:2014-09-28 23:19:36

标签: c# xna

我有一个炮塔可以预测一个移动的目标位置并发射一个射弹相交。

它使用的方法基于这篇文章中的检查答案(2d game : fire at a moving target by predicting intersection of projectile and unit

并返回一个用atan2计算的浮点角度,我还添加了TwoPI来删除底片。

如果我像这样翻转炮塔运动

currentAngle = MathHelper.Lerp(currentAngle, newAngle, speed)'

这意味着炮塔越接近所需的步数,降低旋转速度,精度就会下降。

如果我使用固定步骤做这样的事情

if (_angle < newAngle)
{
    _angle += fixedStep;
}
if (_angle > newAngle)
{
    _angle -= fixedStep;
}

稍微好一点的结果但是我失去了精确的精确度,其中角度不能比固定步长的尺寸​​更接近。

有没有更好的方法来做这样的事情?

理想的最终结果是在任何物体上都能平滑移动,炮塔能够从最高速度加速或减速,因为它需要达到正确的角度,当突然不得不旋转时,有一些额外的惯性。

希望我最终可以解决一些问题,但任何提示都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以通过查看是否超过&#34;来获得此准确性。使用固定步长的角度移动。例如:

if (_angle < newAngle)
{
    _angle += fixedStep;
    if(_angle > targetAngle)
    {
        _angle = targetAngle;
    }
}
if (_angle > newAngle)
{
    _angle -= fixedStep;
    if(_angle < targetAngle)
    {
        _angle = targetAngle;
    }
}

当然,您可能需要修改此代码以考虑缠绕角度(例如-1变为359),具体取决于您存储角度的方式。

相关问题