A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Got minimum angle between 2 vectors, but need direction as well...

  1. #1
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967

    Got minimum angle between 2 vectors, but need direction as well...

    I know I should be able to find this in google, but my google fu is failing, HARD.

    So, the magic of dotproduct can get an angle between two vectors really easily (using points here just for simplicity), I managed to find that much on google. I don't think I need to bother with a full vector class, I am only using this stuff sparingly right now.

    Actionscript Code:
    p2pang = function (p1:Point, p2:Point, inradians) {
        if (inradians==undefined){inradians = false;}
        var cosA:Number = p2pdot(p1, p2) / (p1.length * p2.length);
        var A : Number = Math.acos( cosA );
        if ( inradians == false ) {A = A / Math.PI * 180;}
        return A;
    }
    p2pdot = function(p1:Point, p2:Point){
        return ( p1.x * p2.x + p1.y * p2.y );
    }

    So great, works nice, no awkward problems with -180/180 issues, but I want to be able to know which vector is to which side of which. Basically, what rotation do I have to apply to p1 to get to p2. I need the +/- for what I am doing. The above just returns an always positive value.

    Is there an easy way to add this to the function above?

  2. #2
    Senior Member
    Join Date
    Oct 2009
    Posts
    117
    I'm not sure I really understand your question but...

    ... what's the value that it's returning?
    If it's returning from 0 to 360, couldn't you just subtract 180 to give you to the -180 to 180 range?

    Sorry if I didn't understand your question

  3. #3
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    It is returning 0 to 180.

    If p1 is 1,0 and p2 is 0,-1 it returns 90. That is the angle between the vectors.
    If p1 is 0,-1 and p2 is 1,0 it also returns 90. That is still the angle between the vectors.

    But I want the first case to return -90 and the second to return 90. Basically, what is the shortest rotation around the origin needed to make to p1 so it has the same angle as p2.

  4. #4
    Senior Member
    Join Date
    Oct 2009
    Posts
    117
    Could you use this formula to help you find the angle of each vector?

    vectorAngle = Math.atan2(vy, vx) * 180/Math.PI

    (you will need to know the vx and vy values).

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    To know which side of p1 is p2 you use dot product of p1 normal (normal is perpendicular or 90 degrees from original vector) to p2. Right hand normal of p1:

    p1r.x = -p1.y;
    p1r.y = p1.x;

    If p1r dot product with p2 is positive (> 0), it means p2 is on the right hand side of p1 and you need to turn p1 clockwise to reach p2. If the dot product is negative, turn anti-clockwise.

  6. #6
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    I thought the normal vector of a vector was the unit length 1 vector with the same angle?

    Anyway, the dot product of the perpendicular worked awesome, thank you very much!

    For anyone's information, the final function with sign is:
    Actionscript Code:
    function p2pang (p1:Point, p2:Point, inradians) : Number {
        if (inradians==undefined){inradians = false;}
        var cosA:Number = p2pdot(p1, p2) / (p1.length * p2.length);
        var A : Number = Math.acos( cosA );
        p1r = new Point(-p1.y, p1.x);
        if (p2pdot(p1r, p2) > 0) {sign=1;} else {sign=-1;}
        if ( inradians == false ) {A = A / Math.PI * 180;}
        return A * sign;
    }

  7. #7
    Senior Member
    Join Date
    Oct 2009
    Posts
    117
    Quote Originally Posted by Alluvian View Post
    I thought the normal vector of a vector was the unit length 1 vector with the same angle?
    No, that's a "normalized" vector. A vector's "normal" is a vector that lies perpendicular to it (at a 90 degree angle).

    ... don't worry, it's confusing!

  8. #8
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    I get it, just forgot the term for the perp vector. Thanks.

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