2011-02-25 74 views
0

即時獲取此旋轉功能下拍。我有一個應該朝另一個移動物體旋轉的炮塔。它應該找到最短的旋轉方向。輪換問題

基本上,我所做的是將動作腳本旋轉從使用-1 - > -179轉換爲180 - > 359。但是現在,當它正在追蹤的對象經過零度點時,我有問題。然後電流圍繞另一路旋轉。

這裏是代碼。我的頭腦變得有點b。。它被傳遞在y區別對象之間的X差異,

public function Move(xdiff:Number,ydiff:Number){ 
     var currentRotation:Number; 
     rot = Math.round(Math.atan(ydiff/xdiff) * (180/Math.PI)); 
     if(xdiff < 0){ 
      //this.rotation = 180 + rot; 
      rot = rot + 180 
     }else{ 
      //this.rotation = rot; 
     } 
     if(rot < 0 && rot > -90){ 
      rot = 270 + 90 + rot; 
     } 
     trace("idealrotation: " + rot + "currentrotation: " + rotation); 
     currentRotation = rotation; 
     if(currentRotation < 0){ 
      currentRotation += 360; 
     } 
     if(rot - currentRotation > 0){ 
      if(Math.abs(rot-currentRotation) < 5){ 
       rotation = rot; 
      }else{ 
       rotation += rotationSpeed; 
      } 
     }else if(rot - currentRotation < 0){ 
      if(Math.abs(rot-currentRotation) < 5){ 
       rotation = rot; 
      }else{ 
       rotation -= rotationSpeed; 
      } 
     }else{ 
      rotation = rot; 
     } 

     //this.rotation = rot; 
    } 

回答

0

這是我如何做的:

public function aim(stageX:Number, stageY:Number):void 
{ 
    var angle:Number = (Math.atan2(stageY - y, stageX - x) - Math.PI * 0.5) * 180/Math.PI; 
    if (angle < 180) angle += 360; 
    if (angle > 180) angle -= 360; 
    targetAngle = angle; 
} 

x,y是大炮,stageXstageY - target的座標。