Angle in degrees between 2 Vector2 points

I know this seems like a simple question, but how do I find an angle in degrees from one Vector2 point to another. I have searched online and found only complicated results. Give that we have very complex stuff like CFrame LookVector built in, I thought there might be an easy way to do this. But I have not found anything except for math.acos2. But that only takes 2 numbers, and I need to use 4 numbers (2 Vector2’s). Is there an easy way to do this?

1 Like

Your question is a little confusing. What angle are you looking for specifically?

The confusion comes from this - a Vector2 has two values, which in this case, let’s consider to be X and Y coordinates. If you plot the two Vector2 values using their coordinates, you get two points on a standard 2D graph. The only thing you can do with those two points (generally speaking) is draw a line between them. There is no angle by themselves.

That said, if you had some point of origin (which you must), you could get an angle based off of that. Or, if you consider both Vector2 points to be vertices of a right triangle, we could get some angle within that triangle as well.

Could you clarify please?

1 Like

What I meant is the angle from 1 point to another. The first point is the origin, and I need to calculate the angle from that point to the second point.
image
Like this; I want to get the angle of the blue line.

Angle relative to what? Using two vectors, you can use the dot product to find the angle between the two. With only one, the angle you’re looking for is ambiguous, since it can be relative to any thing: 0 on a unit circle, 0 on a clock, etc.

1 Like

What is a dot product? And, I do have 2 points, like in this picture:
8f364f81d88f4078ad32916ad5efdb7eb4e09ea4
I am trying to find the angle from point 1 to point 2.

Consider the following graph:

Here you have your two points. One at the origin (0, 0) and another at some other point in 2D space.

There are two potential angles you could be solving for. Either you want the red one, or the green one. I’ll explain how to get both.

The green one is rather simple. You just imagine a third point above the origin at the Y value of the second point. In this case, we can draw a third point at (0, 2) and create a right triangle.

We then need to find the distance between the two points (the length of the hypotenuse). This can be found by the square root of (point2.X ^ 2 + point2.Y ^ 2)

Using trigonometric principles, we know that the green angle is then equal to: 90 + arcsin(2/sqrt(8))

We use arcsin(2/sqrt(8)) because the length of the line opposite the angle in question is 2 and the length of the hypotenuse is the square root of 8.

The red angle can be determined by the following: 180 - (green angle)

Does that make sense? I’m not sure how well I worded that.

2 Likes

You need two vectors, not points, to find the angle between them.
Using two vectors A and B, the angle between them would be found by using math.acos(A:Dot(B)).

1 Like

Sorry, I made some mathematical errors the first time around and have corrected them now.

If the first vector is at the origin, wouldn’t you just plug in the second vector into math.atan2?

local angle = math.atan2(vec2.Y, vec2.X)

And if neither of them are at the origin, you could subtract one from the other:

local vectorDiff = vec2 - vec1
local angle = math.atan2(vectorDiff.Y, vectorDiff.X)

(This function returns an angle in radians)

4 Likes

This wouldn’t work if the two vectors have different origins or different magnitudes.

EDIT:
Vectors don’t have an origin lol I’m stupid. But the magnitude part still holds true

1 Like

Thanks! This worked. I had to do -math.deg(math.atan2(vectorDiff.Y, vectorDiff.X)) + 180 because it was in radians and was also flipped and rotated 180.
image
Thanks @ChiefWildin, @wow13524, and @goldenstein64 for help too.

2 Likes