2011-12-21 53 views
1

此刻我正確地找到了屏幕上物體和光標的距離和旋轉。XNA 2D點對象隨時間變化的光標

Vector2 direction = podType[podIndex]._Position - MouseCursorInWorld; 
mousePoint = (float)(Math.Atan2(-direction.X, direction.Y)); 

這工作得很好,下一步我拿了搞清楚我如何可以慢慢旋轉當前方向到鼠標位置是使用百分比的正常工作,但有1個大問題,就是我想要修復

 percent = mousePoint/mousePoint * increment; 
     if(percent < mousePoint)increment += 0.01f; 
     if (percent > mousePoint) increment -= 0.01f; 

正如你可以在這裏看到百分比是總旋轉光標的百分比,如果旋轉更少或更多,然後移動到該增量,直到達到100%,這意味着它的正確的百分比面向光標。

問題是因爲左側是負面的,右側是正面的,我的完整旋轉達到3.1和-3.1,所以當我將光標移動到最底部某處並從最右側移動到最左側時繼續其朝向光標的路徑,它向右旋轉,因爲當前鼠標點值爲負2.2它目前是正值1.5

是否有某種方式可以包裹旋轉因此它沒有負值和正值的角度?還是有更好的技術,我可以使用其他的,然後我現在是一個?謝謝你的時間:)

回答

3

我不太確定我明白你是完整的百分比的東西,但從我的理解你想要一個對象旋轉逐漸轉向鼠標。

嘗試,敷Pi和-Pi之間你的目標旋轉量,你可以這樣做以下

  Vector2 dist = podType[podIndex]._Position - MouseCursorInWorld; 
      float angleTo = (float)Math.Atan2(dist.Y, dist.X); //angle you want to get to 

      rotation = MathHelper.WrapAngle(rotation); // keeps angle between pi and -pi 

      if (angleTo > rotation) 
       while (angleTo - rotation > MathHelper.Pi) 
        angleTo -= MathHelper.TwoPi; 
      else 
       while (angleTo - rotation < -MathHelper.Pi) 
        angleTo += MathHelper.TwoPi; 


      if (rotation < angleTo) rotation += 0.01f; 
      if (rotation > angleTo) rotation -= 0.01f; 

rotation將是你的當前旋轉。另外,如果你想得到兩個數字之間的百分比值,我會考慮​​(線性插值)。

替代+或

最後, - 0.01F,你可以有像

rotation = MathHelper.Lerp(rotation, angleTo, 0.01f);

這將增加對你的目標角1%的每一幀的旋轉的價值。

+0

究竟是我在找什麼謝謝你本傑明:D如果你有興趣爲什麼即時通訊試圖做到這一點的宇宙飛船遊戲我想要一些飛船旋轉慢一點,然後其他飛船,我不希望他們即刻。再次感謝幫助我:) – user1109013 2011-12-22 07:29:49