Shop OBEX P1 Docs P2 Docs Learn Events
Determining the Angle Btwn Vectors — Parallax Forums

Determining the Angle Btwn Vectors

DiablodeMorteDiablodeMorte Posts: 238
edited 2005-12-30 19:11 in General Discussion
I have a quick question that I was unable to solve using the Search function....

If I have two coordinates(pt A and pt B) on an XY(cartesian) plane. I know the angle of pt A and would like to determine the angle that pt A has to be at inorder for it to be "Facing" pt B. In video game authoring this would be called VEC_TO_ANGLE. I have googled it and with my limited knowledge of advanced math I have not found a clear method.

Does anybody have any suggestions?

-DiablodeMorte

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2005-12-30 06:35
    Just a thought...

    Couldn't you use the formula " y = mx + b " in a way that compares 'm' between your two vectors
    without actually needing to find the angle?

    something like ...

    m = ( y - b ) / x

    I would think that if ptA is facing ptB then 'm' would be the same for both points. The exception
    would be that if ptA or ptB were to face the other at 180 Deg out of phase. A secondary check
    should be able to eliminate this scenario.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • MikeKMikeK Posts: 118
    edited 2005-12-30 15:06
    Try this (both formulas are the same, one notation might make more sense than the other)

    Angle = Arctan( (By - Ay) / (Bx - Ax) )

    or

    Angle = Arctan( (Yb - Ya) / (Xb - Xa) )
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2005-12-30 15:41
    I will try both person's idea. Thanks for the quick reply...

    P.S. MikeK.. When I red your response I was so confused.. What the heck are the b's and a's and then I smacked my head against the table and got up and got a coke. To early to think

    P.S.S. Schwabe.. I'm not entirly sure how you envision your idea.. How do I determine B? Do I use be as the Y intercept of the slope?
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2005-12-30 15:42
    Yes, but assuming he is using a stamp, "m=(y-b)/x" (a derivative of "y=mx+b") might be easier to implement
    when the Arctan function is not available.




    DiablodeMorte,

    "b" is simply an offset value of "x" from the origin.


    See this for reference:
    mathforum.org/cgraph/cslope/mxplusb.html

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 12/30/2005 3:49:08 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-12-30 17:45
    How about using the dot product?

    The dot product is

    95c2bf58b5151d1fb37720535f306e92.png


    It just so happens that

    25579cd5bf59b6551dd92fe316b3112b.png

    so Θ = arccos((a1b1+a2b2)/ab) where ab is the product of the lengths of the vectors, ai and bi are the component value of the vector in the ith axis (x and y).


    Here's a picture of the terms used:

    Scalarproduct.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 12/30/2005 5:53:04 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-12-30 19:11
    following Beau's lead, you could avoid using the arccos function and simply compute the cos Θ of the value or
    (axbx+ayby)/sqrt((ax2+ay2)(bx2+by2)).
    The benefit of the the equation is the y axis crossing (b) of a vector isn't needed, you only need the component values of the vectors. If you only have two vectors which you are comparing and the two vectors have a constant magnitude·you can simply do··(axbx+ayby) to calculate an unscaled measure of angle between the two vectors.

    An alternative to the entire approach is to switch the coordinate system you use to store your vectors into magnitude and angle, then you can just subtract the angle component of your vectors to determine the angle between the two.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
Sign In or Register to comment.