2011-06-30 10 views
3

私は現在、原点で私の物体が回転して3つの象限内のオブジェクトに移動することができるようになります私のオブジェクトが別のobject.currentlyそれがこの絵のように正常に動作に直面して回転させるとのトラブルを抱えています正の90度から180度の象限を除いて、私のオブジェクトはオブジェクトに移動している間、何回か全回転を何回か回転させます。食品がのAdobe Flash CS4回転数学

var angle:Number = Math.atan2(foodTarget.y - y, foodTarget.x - x); 
      foodLocationDegrees =Math.floor((angle * 180/Math.PI)); 

Yを用いて計算した魚である対象であるX、および食品の対象となるfoodtargetされるiが回転するために使用する文はrotation += (foodLocationDegrees - (rotation - 90)) * .2;あります。

http://iepro.files.wordpress.com/2009/12/atan2.jpg?w=280&h=283

public function moveToFood():void 
     { 

      var dx:Number = x - _destinationX; 
      var dy:Number = y - _destinationY; 

      trace("Food location degree relative to fish mouth "+foodLocationDegrees); 
      var targetRotation:Number = 0; 
      if (foodLocationDegrees > 0 && foodLocationDegrees < 90) 
      { 
      trace("food is on 1st quadrant of the fish mount"); 
       this.x -= dx/18; 
       this.y -= dy/18; 
      }else if (foodLocationDegrees > 90 && foodLocationDegrees < 180) 
      { 
      trace("food is on 2nd quadrant of the fish mount"); 

       this.x -= dx/18; 
       this.y -= dy/18; 
      }else if (foodLocationDegrees > -180 && foodLocationDegrees < -90) 
      { 
       trace("food is on 3nd quadrant of the fish mount"); 

       this.x -= dx/18; 
       this.y -= dy/18; 
      }else if (foodLocationDegrees < 0 && foodLocationDegrees > -90) 
      { 
       trace("food is on 4nd quadrant of the fish mount"); 

       this.x -= dx/18; 
       this.y -= dy/18; 
      } 
      trace("Before adding Rotation " + rotation); 
      var number:Number = (foodLocationDegrees - (rotation - 90)) * .1; 
      trace("rotation to add "+number); 
      rotation += (foodLocationDegrees - (rotation - 90)) * .2; 

      trace("After adding Rotation " + rotation); 

      //removing food when both hit boxes hit 
      if (hit.hitTestObject(foodTarget.hit)) 
      { 
       foodInPond--; 
       foodTarget.removeSelf(); 
      } 

     } 
+0

PS私の知識は、私は数学を学んで努力をしていない、と数式がここで尋ねた答えと私の部分の数字をいじりのビットからつなぎ合わせているように、私を許してください、非常に弱いです。 – sutoL

答えて

0
public function moveToFood(food:Food):void 
{ 
    var speed:Number = 6.5; 

    var a:Number = food.x - x; 
    var b:Number = food.y - y; 
    var ang:Number = Math.atan2(b, a); 

    rotation = ang * 180/Math.PI; 

    x += Math.cos(ang) * speed; 
    y += Math.sin(ang) * speed; 
} 

あなたはちょうどあなたが直面している方向に移動するsincosを使用することができませんか?数学の

+0

私はあなたの機能を試してみましたが、魚は食物から離れて移動し、関数は値が繰り返し変更することENTER_FRAMEハンドラで処理されます。 – sutoL

+0

私の悪い、更新されたaとb。 – Marty

+0

iは、移動作業を取得することができていますが、食べ物に直面する魚の頭の回転がイムに問題があるものです。あなたの方法を使用して、魚は横に移動します。私の魚のシンボルの登録ポイントが設定されているためです。 – sutoL

関連する問題