3

i am currently having trouble with my object rotating to face another object.currently it works fine as in this picture, my object at the origin will be able to rotate and move to the object in the 3 quadrants except the quadrant with positive 90degrees to 180 degrees, my object will rotate a few full rotation for some time while moving to the object does anyone know why? the sentence that i use to rotate is rotation += (foodLocationDegrees - (rotation - 90)) * .2; whereby food is calculated using

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

y and x being the object which is a fish, and foodtarget being a object of a food.

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();
            }

        }
sutoL
  • 1,737
  • 10
  • 27
  • 48
  • PS my knowledge of maths is very weak as i did not put in effort in learning maths, and the formulas are pieced together from the answers asked here and a bit of messing around with the numbers on my part, please pardon me . – sutoL Jun 30 '11 at 02:26

1 Answers1

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;
}

Can't you just use sin and cos to move in the direction you're facing?

Marty
  • 39,033
  • 19
  • 93
  • 162
  • i tried your function but the fish moves away from the food , and the function is handled in the Enter_frame handler making the values change repeatedly. – sutoL Jun 30 '11 at 03:53
  • i am able to get the moving working , but the rotation of the fish head to face the food is what im having trouble with. using your method the fish moves to it sideways.is it because of the way my registration point of my fish symbol is set. – sutoL Jun 30 '11 at 04:22
  • What you need to do here is rotate the graphic in the library so that it's facing right, not up. – Marty Jun 30 '11 at 04:33
  • thanks that works, may i know where i can read up on how the formula works? – sutoL Jun 30 '11 at 05:41
  • http://en.wikipedia.org/wiki/File:Sine_and_Cosine.svg sin and cos take a value in radians that equates to a value between -1 and 1. For example, cos(0) will equate to 1, and sin(0) equates to 0. So if you look at the formula, When your angle is 0 radians, you're moving along the x axis by 1 * speed, and across the y axis by 0 * speed (0). – Marty Jun 30 '11 at 05:45