Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将精灵旋转到一个点?_C++_Algorithm_Sfml_Trigonometry - Fatal编程技术网

C++ 如何将精灵旋转到一个点?

C++ 如何将精灵旋转到一个点?,c++,algorithm,sfml,trigonometry,C++,Algorithm,Sfml,Trigonometry,我试着旋转一个精灵,比如说它是一把枪,到一个点,通过在360°上走尽可能小的路线(向右或向左,取决于点靠近哪一侧),我遇到了一些问题 在一个圆上,它从359°跳到0°,所以我不能直接使用我的目标角度-当前角度 请记住,我使用的是SFML,所以它会在isRotating为true时每帧执行一次函数 以下是我可以获得的信息: //The angle in degrees of my sprite float currentAngle = pSprite.getRotation(); //The an

我试着旋转一个精灵,比如说它是一把枪,到一个点,通过在360°上走尽可能小的路线(向右或向左,取决于点靠近哪一侧),我遇到了一些问题

在一个圆上,它从359°跳到0°,所以我不能直接使用我的目标角度-当前角度

请记住,我使用的是SFML,所以它会在isRotating为true时每帧执行一次函数

以下是我可以获得的信息:

//The angle in degrees of my sprite
float currentAngle = pSprite.getRotation();
//The angle is needs to be once the rotation is over
float targetAngle = float(atan2(deltaY,deltaX) * 180 / (atan(1)*4) + 180);
我用一个速度变量来增加或减少每帧角度的值

distance = Speed*Time.asSeconds();
currentAngle += distance;

首先,了解其中的区别:

diff = target - current
diff
是“短”角度(导致较短旋转的角度)或“长”角度(在另一个方向旋转较长)。请注意:

  • 从一个角度到另一个角度,不需要旋转超过180度(绝对值)

  • “短”角和“长”角有相反的符号(+/-),它们的绝对值相加为360

例如:0-359=-359。这是“长”角度。我们之所以知道这一点,是因为它的绝对值大于180。“短”角将有相反的符号,其绝对值加起来将为360加359,因此它是1

一种简单的计算方法是:

while (diff < -180) 
    diff += 360;
while (diff > 180)
    diff -= 360;
while(差异<-180)
差值+=360;
而(差值>180)
差值-=360;
diff
现在是您要寻找的角度。所以如果是负数,将你的速度乘以-1

如果角度不在
[0,360]
,则
while
(与
相反,如果
)存在-例如,您有
当前=1440,目标=359