平滑旋转到一个角度

时间:2015-10-24 05:37:00

标签: javascript c++ algorithm lua rotation

我正在以160度/秒的速度旋转物体,并以预先指定的角度减速到完全停止。例如,如果选择的角度是30度,它将非常快速地旋转并且减速,最终在30度处停止。我在提出算法时遇到了麻烦,这就是我要求的。

目前,假设你需要做的就是设置旋转是object.Rotation = 30(度)。随意用Java / Lua / C ++ / JavaScript编写。

到目前为止(基本上什么都没有):

//Assume that wait(1) waits 1 second
int angle = 70;//Fast rotations at first but slow down as time goes on
for (int i = 140; i > .1; i = i - 5)//Must work for every angle
{
    for (int a = 0; a < i; a = a + 10)
    {
        object.Rotation = a;
        wait(.05);
    }
}

1 个答案:

答案 0 :(得分:2)

伪代码:

int max_speed = 10
int target_angle = 30

while (target_angle != object.Rotation) do
  int delta = target_angle - object.Rotation
  delta = max(min(delta / 5, max_speed), -max_speed) + max(min(delta, 1), -1)
  object.Rotation = object.Rotation + delta
  wait(.05)
end while
相关问题