3 Replies Latest reply on Nov 24, 2008 12:42 AM by Newsgroup_User

    Rotating object to follow mouse

    Level 7
      Okay, say I have an object (a bee in this case) that I want to follow the
      mouse around, that part's easy:

      property my,oldLoc
      on beginSprite me
      my = sprite(me.spriteNum)
      oldLoc = my.loc
      end
      on exitFrame me
      my.loc = the mouseLoc
      if diff(my.locV,oldLoc.locV) + diff(my.locH,oldLoc.locH) > 5 then
      my.member = "BeeBuzz"
      else my.member = "BeeStill"
      oldLoc = my.loc
      end

      It even includes a cute little function to make the wings buzz when it's
      moving, and not when it's standing still. The tricky part is I want to have
      some way of making the bee rotate to move in the direction the mouse moves.
      This seems to *almost* work, except that when the locV doesn't change, it
      results in a divide-by-zero error:

      my.rotation = atan((oldLoc.locH - my.locH) / (oldLoc.locV - my.locV)) *
      57.2958

      I could check and artificially add a small amount to avoid the
      divide-by-zero, but that seems like a hash. Is there a better way to do
      this correctly?

      (And does anybody mind if I rant a bit about how all rotation properties in
      the program operate in degrees, while all the trig functions operate in
      radians? And the program doesn't include a method to convert between them
      either. That's just annoying.)


        • 1. Re: Rotating object to follow mouse
          robdillon Most Valuable Participant
          Have you looked at the Library behaviors for controlling sprites with the cursor? Look in the Animation - Interactive section. There are a couple of behaviors in there that will probably do what you're after. They may also explain how to work with degrees and radians.
          • 2. Re: Rotating object to follow mouse
            Level 7
            Thanks. I generally try to avoid the Library behaviors because I've found
            that they mostly seem to be 98% unnecessary error-checking and 2% functional
            code, and it can often be very difficult to find and extract that tiny
            little bit of code that actually DOES anything. However, in this case I was
            able to pull a bit of useful code out of the Turn Towards Mouse behavior -
            extremely simplified of course. It didn't work at all out of the box. For
            the record, the code I used looks like this:

            angle = GetAngle(oldLoc - my.loc) - 90
            if my.rotation <> angle then my.rotation = angle

            With the following code copied (mostly verbatim, but simplified) from the
            Turn Towards Mouse:

            on GetAngle slope
            deltaH = slope[1]
            deltaV = slope[2]
            if deltaH then
            slope = float (deltaV) / deltaH
            angle = atan (slope)
            if deltaH < 0 then angle = angle + pi

            else
            if deltaV > 0 then angle = pi / 2
            else if deltaV < 0 then angle = (3 * pi) / 2
            else angle = 0
            end if
            return (angle * 180) / pi
            end GetAngle

            It seems to do pretty much what my original code did, only it considers
            those divide-by-zero situations separately as special cases. Still seems
            like more code than should be necessary to do what it does - this feels like
            the sort of thing that should be possible with 1-2 lines of code, but
            apparently it's not that simple...


            • 3. Re: Rotating object to follow mouse
              Level 7
              Darrel Hoffman wrote:
              > This seems to *almost* work, except that
              > when the locV doesn't change, it results in a divide-by-zero error:
              >
              > my.rotation = atan((oldLoc.locH - my.locH) / (oldLoc.locV - my.locV))
              > * 57.2958
              >
              > I could check and artificially add a small amount to avoid the
              > divide-by-zero, but that seems like a hash. Is there a better way to
              > do this correctly?

              There's an undocumented version of atan which works as you need:

              my.rotation = atan(oldLoc.locV - my.locV, oldLoc.locH - my.locH)*57.2958

              Andrew