Question about vec2

Hi all, I was playing around today, when I noticed the vec2:angleBetween(vec) function.
I tried to use it like this:

v1 = vec2(1, 1)
v2 = vec2(4, 4)
print(math.deg(v1:angleBetween(v2))) -- Expected = 45

v1 = vec2(1, 1)
v2 = vec2(4, 1)
print(math.deg(v1:angleBetween(v2))) -- Expected = 90

What am I doing wrong?

I can give you a hint: a vector’s origin is always 0,0. so take a piece of paper and draw the vectors you want to compute the angle between above.

Ummm, @gunnar_z, what do you mean origin? (Sorry, I’m only 13)

Ok, I played around a bit, and when I change v1 to vec2(0,0) the angle is correct, please can someone explain why this happens. :slight_smile:

The angle between two vectors is the angle between the lines that they make to the origin, not the angle of the line between the vectors. As gunnar_z suggested, try drawing the vectors on paper (squared paper is best for this). Draw the lines to the origin and then measure the angle between them by placing a protractor on the paper centred at the origin and aligned with your first vector. The angle returned by angleBetween is then the angle subtended by the second vector.

So your original two are in the same direction from the origin, hence get an angle of 0. The second two are both in a similar direction so get an angle of somewhere around 30 (I’m guessing).

To get the angle you’re expecting, you’d need to do vec2(1,0):angleBetween(v2 - v1) as that would measure the angle between the vector from v1 to v2 relative to the x-axis (horizontal).

ok then :slight_smile: A vector has a direction and a length. Both is indirectly given by the coordinates specified, and the fact that a vector starts at 0,0. So, when you say vec2(1,1) you mean short line starting at 0,0 and extending to 1,1. Same thing with the second vector, starting at 0,0 and extending to 4,4. As you may now notice, both vectors go in the same direction, so the angle between them is 0 degrees. So in order to get your 45 degrees, you would want a vec2(0,1) and a vec2(1,1), that should work.

Now, a vector (0,0) has no length, and thus no direction, so there really can not be an angle between this and any other vector…

Edit: what he said :wink:

Thanks SOOO much! You guys are awesome!

edit: nvm. already answered