这就是我的
- 我在 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 之间进行插值?如果是这样,则以下算法应该有效:
- 找到第一个 vector 相对于固定 vector [1, 0] 的角度。这是 a1。
- 找到第二个 vector 相对于固定 vector [1, 0] 的角度。这是 a2。
- 令 da = a2 - a1。
- 如果 da > 180, da -= 360;
- 否则如果da < 180, da += 360;
您需要计算相对于另一个第三个 vector [1, 0] 的角度,以便确定向左或向右旋转的天气。
编辑:我看到您的 YouTube 链接已损坏,现在我发现它又可以使用了。我想我的答案就是你所追求的。
关于c++ - 旋转 Atan2 CCW CW 连续性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861170/