2012-05-22 154 views
2

顯然,即使我已經通過微積分3,我的觸發是可怕的失敗我(我可能甚至不能稱之爲觸發,這只是基本的數學)。 所以基本上我有一堆旋轉的圖像,我不希望它是瞬間的。 我明白如何讓它旋轉緩慢,但我不知道如何計算它應該旋轉的方式。緩慢的角度旋轉

所以如果我想轉到的角度是在179度以下,轉一圈。 如果它超過180度,轉向另一個方向。不知道我是否說得對,但我基本上只是希望它使用最短距離旋轉。 想象一下我想的導彈。

我找角度的兩種方式,一種方式是使用矢量如下所示:

hero = new Vector2f(this.x, this.y); 
target = new Vector2f(mouseX, mouseY); 
target.sub(hero); 

// Calculates the rotation 
finalRotation = (int) target.getTheta(); 

的另一種方法是使用atan2(這看起來簡單,我知道我可以把它縮短到一個return語句) :

direction = Math.atan2(player.x - this.x, player.y - this.y); 
return (int) Math.toDegrees(direction); 

我假設尋找最短旋轉的方法對兩者都適用。我一直在嘗試一些嘗試和錯誤的東西一段時間,我只是難住。 幫助我毫無價值的編程技巧!任何幫助感謝!

編輯: 我實際上忘了提到的是,我指着老鼠。 因此,如果我的鼠標與我的'圖像'(這是玩家)和玩家目前正在面對的角度爲200,我將如何確定它是否應該正向200或向下0,轉移到360並繼續向下到200

DOUBLE編輯: 所以我有什麼,我想要一個工作版本,但由於某些原因最終與當前旋轉是關閉的180,它會導致我的性格從來沒有停止抽搐。 明天我需要更多的工作。由於我的精靈最初旋轉的方式,最後我必須從currentRotation中減去90。

TRIPLE編輯: 好了,所以我有,所以我的遞增和遞減語句顛倒它試圖晃過180和再反方向。我完善了它!

mouseX = input.getMouseX(); 
mouseY = input.getMouseY(); 

hero = new Vector2f(this.x, this.y); 
target = new Vector2f(mouseX, mouseY); 
target.sub(hero); 

// Calculates the rotation 
finalRotation = (int) target.getTheta(); 

if(!(currentRotation <= finalRotation + 1 && currentRotation >= finalRotation - 1)) { 
    double tempVal; 

    if (currentRotation < finalRotation) { 
     tempVal = finalRotation - currentRotation; 

     if (tempVal > 180) { 
      currentRotation -= .3 * delta; 
     } else { 
      currentRotation += .3 * delta; 
     } 
    } else if (currentRotation > finalRotation) { 
     tempVal = currentRotation - finalRotation; 

     if (tempVal < 180) { 
      currentRotation -= .3 * delta; 
     } else { 
      currentRotation += .3 * delta; 
     }    
    } 
} 

if (currentRotation < 0) 
    currentRotation = 359; 

if (currentRotation > 360) 
    currentRotation = 1; 

this.setAngle((int) (currentRotation+90)); 

輸入的東西和向量來自Slick2D。

+0

你只需要最小的角度(請參閱@Amadan的回答),還是需要知道產生最小角度的旋轉方向(順時針或逆時針)? –

+0

我需要產生最小角度的順時針或逆時針旋轉。如同,如果我想從0到60,我移動正數(0到60)而不是負數(360到60)。 – Paha

回答

3
if (finalRotation >= 180) finalRotation = 360 - finalRotation; 

這將反轉任何角度爲180或以上的方向。

+0

哦不,不能這麼簡單。這會讓我哭泣! – Paha

+0

@Paha:大聲笑......它甚至可能與'finalRotation = -finalRotation'一起工作,但是'360-finalRotation'在0-360之間保持良好。 – Amadan

+0

啊,真好!感謝您的簡單答案,我找不到:( – Paha