3 Replies Latest reply on Dec 1, 2008 12:30 PM by somascope

    Rotate object in direction of drag movement

    somascope Level 3
      I've messed around with getting a draggable object to point to a static object, but I want to have the item I'm dragging actually point in the direction that I'm dragging it in.

      Anybody with ideas for how to do this?
        • 1. Re: Rotate object in direction of drag movement
          Ned Murphy Adobe Community Professional & MVP
          I imagine you'd need to continuously keep track of current versus previous _x and _y positions of the object as it is being moved and use some math to figure out what the angle of the path you are following is. You'll probably need to weave in some conditionals to avoid dealing with no movement while still in a drag state and zeroes in a denominator.

          Maybe someone else has another idea?
          • 2. Re: Rotate object in direction of drag movement
            Rothrock Level 5
            Yup that is pretty much it. NedWebs has described it. I especially like how you thought about the special case when you have no movement. I wouldn't notice that until I was testing it.

            Here are a few more tips.

            Yes keep the difference in the x and y positions from move to move or frame to frame. Often these are called dx and dy. ("d" for delta meaning change in.)

            Use Math.atan2(dy,dx) to get the angle of those changes. The result will be in radians so you need to multiply that by 180 and divide by Math.PI. (Or just multiply by 57.2957795130823).

            Math.atan2 takes care of the problems regarding dividing by zero.

            You might also want to add some easing so that the angle doesn't just "snap" to the direction but sort of slowly spins around to the direction. I always get it wrong, but I think it would be something like this:

            var angleMoving:Number=Math.atan2(dy,dx)*57.2957795130823;
            var dA:Number=myClip._rotation - angleMoving;
            myClip._rotation+=dA/3;

            You can try different values for the damp factor there in the rotation assignment. If it isn't right then switch the order of the subtractions in the dA line....
            • 3. Re: Rotate object in direction of drag movement
              somascope Level 3
              Great information, guys, thanks a bunch!