2012-01-14 67 views
1

這是我旋轉ATAN2 CCW CW連續性

  • 我在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 - 遺憾的演示版,我無法獲得自由在這一刻什麼。

這就是我需要

  • 我需要這個正確的行爲,比方說飛機在角度350 旋轉,我設定的目標和新的角度應在15

而不是去的:350,340,330,320,310,300,290,... 10,0,15 應該繼續:350,0,15

希望你能幫助我和這些傢伙在一起,我已經放棄了貝塞爾的方法 - 而且我幾天以來一直在努力解決這個問題。

+0

試圖觀看視頻時你說的「這個視頻是私人的」。請公開。 – 2012-01-14 09:33:20

+0

已更新,對不起。 – PeeS 2012-01-14 09:34:59

+0

座標系可能在視頻中旋轉嗎?在我看來,當光標在平面下時出現問題。當m_vDestination.y - m_vPosition.y == 0分開時,你應該處理這種情況,因爲atan2f會有問題 – 2012-01-14 09:40:07

回答

1

如果我正確讀到這個,你試圖找到兩個向量之間插入的最小角度?如果是這樣,則應使用以下算法:

  1. 找到第一個向量相對於固定向量[1,0]的角度。這是a1。
  2. 找到第二個向量相對於固定向量[1,0]的角度。這是a2。
  3. 讓da = a2-a1。
  4. 如果da> 180,da = 360;
  5. else if if < 180,da + = 360;

您需要計算相對於另一個第三個向量[1,0]的角度,以便您可以確定向左或向右旋轉的天氣。

編輯:我看到您的YouTube鏈接已損壞,現在我發現它再次運行。我想我的答案就是你想要的。

+0

我已經用類似的方法解決了問題。感謝Liam - 幹得好! – PeeS 2012-01-14 09:45:13

+0

不用擔心!這是一個簡單的問題,但我認爲我們都在一個或另一個階段處理這個問題。 – 2012-01-14 09:46:20

+0

嘖嘖,有時我覺得我應該起訴我的數學老師;-) 可愛的東西,我有這個做伴侶,我現在可以繼續這個東西:)有一個偉大的週末利亞姆:) – PeeS 2012-01-14 09:53:34