dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: help with rotation, please?

  1. #1
    Large Member
    Join Date
    Feb 2002
    Location
    I live in a van down by the river!
    Posts
    227

    help with rotation, please?

    im trying to get this movieclip to face the direction it is moving. cant seem to get it right. here is what script is applied to the frame.
    code:
    if (_root.obj. hitTest(_root.bottom)) {
    speedy = (speedy)*-1;
    _root.obj._rotation -= 90;
    }
    if (_root.obj. hitTest(_root.top)) {
    speedy = (speedy)*-1;
    _root.obj._rotation -= 90;
    }
    if (_root.obj. hitTest(_root.right)) {
    speedx = (speedx)*-1;
    _root.obj._rotation -= 90;
    }
    if (_root.obj. hitTest(_root.left)) {
    speedx = (speedx)*-1;
    _root.obj._rotation -= 90;
    }


    ill attatch the file also if anybody wants to have a look.
    any ideas?
    thanks for your time!
    Attached Files Attached Files

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    It looks like speedX and speedY are the vector you are travelling. If you know that vector, then you can determine your angle using the following function:

    Code:
    function getAngle(vecx,vecy)
    {
      var dist = Math.sqrt( vecx*vecx + vecy*vecy);
      if (dist == 0)
        return 0;
      var a;
      if (vecy < 0)
        a = Math.PI*2-Math.acos(vecx/dist);
      else
        a = Math.acos(vecx/dist);
      return a*180/Math.PI;
    }
    So use this:

    root.obj._rotation = getAngle(speedx,speedy);

    Since the directions you are travelling are quite simple, this can probably be simplified. My solution will work however, regardless of what wacky direction you pick. Also note that that angle "0" is when you are pointing to the right. If you want zero to be some other angle, then add the difference to the result.

    - Jim

  3. #3
    Large Member
    Join Date
    Feb 2002
    Location
    I live in a van down by the river!
    Posts
    227

    thanks jim

    i will try that as soon as i get off work.

    thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center