c++ - 旋转 Atan2 CCW CW 连续性

标签 c++ ios math

这就是我的

  • 我在 2D X,Y 中有一个平面
  • 我通过点击屏幕 X', Y' 来设置他的目的地
  • 我计算它需要转向以面向此目的地的角度:
// Calculate the angle between plane position and destination point

CVector3 facingVec = m_vDestination - m_vPosition;
fAngle = -Math::radiansToDegrees (  (float)atan2f(m_vDestination.x - m_vPosition.x, m_vDestination.y - m_vPosition.y)  )  ;

//This doesn't work, when rotating from ex. 350 degree to 0 
//plane has to go all the way around 360,350,340,330,
//...,120,...100,90,..down to zero
float angleToTurn = fAngle - m_fRotationAngle;
if(angleToTurn < 0)
{
    angleToTurn += 360.0f;
}

m_fRotationAngle += (angleToTurn) / 5;

// Move the unit towards the calculated angle m_fRotationAngle

m_vDirection.x =   (-sin(Math::degreesToRadians(m_fRotationAngle)));
m_vDirection.y =   (cos(Math::degreesToRadians(m_fRotationAngle)));

m_vPosition += ( 2 * m_vDirection * fDelta);

这是它的样子

YT Video - sorry for the demo version, i couldn't get anything free at this moment.

这就是我需要的

  • 我需要它才能正常运行,假设平面旋转了 350 度。 我设置了目的地,新角度应该是 15。

而不是去:350,340,330,320,310,300,290,...10,0,15 它应该继续:350,0,15

希望你们能帮我解决这些问题,我已经放弃了贝塞尔曲线方法——几天以来我一直在努力解决这个问题。

最佳答案

如果我没看错的话,您是在尝试找到最小角度以在两个 vector 之间进行插值?如果是这样,则以下算法应该有效:

  1. 找到第一个 vector 相对于固定 vector [1, 0] 的角度。这是 a1。
  2. 找到第二个 vector 相对于固定 vector [1, 0] 的角度。这是 a2。
  3. 令 da = a2 - a1。
  4. 如果 da > 180, da -= 360;
  5. 否则如果da < 180, da += 360;

您需要计算相对于另一个第三个 vector [1, 0] 的角度,以便确定向左或向右旋转的天气。

编辑:我看到您的 YouTube 链接已损坏,现在我发现它又可以使用了。我想我的答案就是你所追求的。

关于c++ - 旋转 Atan2 CCW CW 连续性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861170/

相关文章:

java - 找到由给定数字整除的两位数字组成的最小数字

iphone - 迪尔德 : Symbol not found: error in iOS 3. 2

python - 如何用Python画一个简单的Argand图?

c++ - 为什么这些具有外部链接的名称不表示同一实体?

c++ - PluginController 的概念和设计

c++ - 在哪里可以了解有关 C++0x 的更多信息?

c++ - 在 getter 中返回字符串引用的正确方法

ios - 自定义文本字段不符合约束

ios - 在 drawRect 中将文本放在其他 CALayer 之上

javascript - 如何将图像像素转换为纬度和经度中的相同位置以供谷歌地图使用