Posted: 29th Sep 2013 1:00
I've seen this asked a few times, I think I've wanted to know myself before as well. It wasn't until I saw the equation to find the dot product that I realized how easy it was to figure out.

|A| * |B| * cos(a)

The lengths of vectors A and B multiplied together along with the cosine of the angle between them equals the dot product. But the simpler way to calculate the dot product is:

A.x * B.x + A.y * B.y


Put two and two together and I get the method shown below. Here's a quick demo. Use the mouse buttons to change the vectors.

+ Code Snippet
n = make vector2(1)
set vector2 1, 50, 13
Alen# = length vector2(1)

n = make vector2(2)
set vector2 2, 27, -40
Blen# = length vector2(2)

dp# = dot product vector2(1, 2)


REPEAT
    cls
    
    if mouseclick() = 1
        set vector2 1, mousex()-320, mousey()-240
        Alen# = length vector2(1)
        dp# = dot product vector2(1, 2)
    endif
    if mouseclick() = 2
        set vector2 2, mousex()-320, mousey()-240
        Blen# = length vector2(2)
        dp# = dot product vector2(1, 2)
    endif

    a# = acos(dp# / (Alen#*Blen#))
    
    
    
    print "Angle: ", a#
    
    
    ink rgb(255,0,0),0
    line 320, 240, 320+x vector2(1), 240+y vector2(1)
    text 320+x vector2(1), 240+y vector2(1), "A"
    ink rgb(0,255,0),0
    line 320, 240, 320+x vector2(2), 240+y vector2(2)
    text 320+x vector2(2), 240+y vector2(2), "B"

    

UNTIL spacekey()
end
Posted: 12th Oct 2013 15:48
hi,

A.x * B.x + A.y + B.y


you meant to say:

A.x * B.x + A.y * B.y
Posted: 12th Oct 2013 19:28
This is too much complicated, and there is a hidden issue with this method..
I advise this instead:
+ Code Snippet
Set vector2 3, -Y vector2(1), X vector2(1)
Angle# = Atanfull(dot product vector2(2, 3), dot product vector2(1, 2))


The lentghs are not needed.
With Acos, numerical inacuracies can occur
And dp# / (Alen#*Blen#) cannot handle len#=0.0
Posted: 18th Oct 2013 8:18
Yes Silverman, thanks for the correction. A slight typo on my part.

Le Verdier, at first glance I would agree your method is better since I can skip the calculation on the vector lengths. But even though mine uses acos, yours is still using atanfull which internally would use atan in its calculations anyway. So any inaccuracies found in acos would surely be present in atan as well.