停在某个点

发布于 2024-12-14 20:01:47 字数 683 浏览 2 评论 0原文

我有一艘太空飞船,我想将其转向目的地角度。目前,它在 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

I have a space ship that I want to turn to a destination angle. Currently it works like 90% of the time, but sometimes, it 'jumps' to the destination angle rather than moving smoothly. Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2024-12-21 20:01:47

我会这样做。首先,您需要一个函数来调整角度(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;
}

然后,在开始旋转时,您将 durationstoppingangle 作为您自己的参数。从您的对象中获取 startinganglestartingtime(以相当精确的毫秒为单位)并保存它们。然后旋转如下:

current_rotation = lerpangle(startingangle, stoppingangle,
                  (time.now - startingtime) / duration)

I would do it like this. First you need a function to lerp an angle (C code, port it yourself):

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;
}

Then, when starting the rotation you have the duration and stoppingangle as your own parameters. Get the startingangle from your object and startingtime (in something decently precise, milliseconds) and save them. The rotation then goes like this:

current_rotation = lerpangle(startingangle, stoppingangle,
                  (time.now - startingtime) / duration)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文