我有一艘太空船,我想转向目的地角度。目前它在90%的情况下起作用,但有时,它会“跳跃”到目标角度而不是平滑移动。这是我的代码:
a = System.Math.Sin(.destStoppingAngle + System.Math.PI)
b = System.Math.Cos(.destStoppingAngle + System.Math.PI)
c = System.Math.Sin(.msngFacing)
d = System.Math.Cos(.msngFacing)
det = a * d - b * c
If det > 0 Then
.msngFacing = .msngFacing - .ROTATION_RATE * TV.TimeElapsed
If det < 0.1 Then
.msngFacing = .destStoppingAngle
.turning = False
End If
Else
.msngFacing = .msngFacing + .ROTATION_RATE * TV.TimeElapsed
If det > 0.1 Then
.msngFacing = .destStoppingAngle
.turning = False
End If
End If
答案 0 :(得分:0)
我会这样做。首先,你需要一个函数来渲染角度(C代码,自己移植):
float lerpangle(float from, float to, float frac) {
float a;
if ( to - from > 180 ) {
to -= 360;
}
if ( to - from < -180 ) {
to += 360;
}
a = from + frac * (to - from);
return a;
}
然后,在开始旋转时,您将duration
和stoppingangle
作为自己的参数。从你的对象和startingangle
获取startingtime
(以精确的精确度,毫秒)并保存它们。然后轮换如下:
current_rotation = lerpangle(startingangle, stoppingangle,
(time.now - startingtime) / duration)