How to get the angle between two points?

Hi. I’ve come up with a scripting idea but I need help with this part. How would I go about finding this angle?

2 Likes

You can use the

math.atan2(x,y)

function, as it would take in two values, the x difference and the z difference

an application of this would look something like this:

local p1 --position 1
local p2 --position 2

local diff = p1-p2;
local degrees = math.deg(math.atan2(diff.X,diff.Z));

for example, if i wanted angle x. i would do
image
Addition: I would like to say that this is , for lack of a better word “directional”

math.deg(math.atan2(1,math.sqrt(3))) -> 30 degrees

however, if i wanted angle z, i would flip the two values and do

math.deg(math.atan2(math.sqrt(3),1)) -> 60 degrees

(*it’s implied that the triangle is a right triangle (i forgot to draw the angle marking))

12 Likes

I have another question if you don’t mind.
This is a little hard for me to explain so I’ll draw a picture.

Let’s say I want to make a point from an angle from the starting point.

In this case let’s make the blue angle 30 degrees, the red line 20 studs, and the purple X is the new point i’m trying to find.

How would I find X?

52%20PM

Well, you can use a bit of trigonometry, as the Pythagorean identity dictates:
image
polar grid graph:
image
using that, we can plot a point on a circle with radius r by doing
image

where theta. the dashed zero, is the angle of that part, and r is the radius of the circle

putting that into roblox, we get:

local angle = math.rad(30)--convert to radians
local origin = origin.Position
local radius = 10 --the distance between the parts
Part.Position = origin + Vector3.new(math.sin(angle)*radius,0,math.cos(angle)*radius)

the reason there isn’t any squaring the the declaration of the Position is because that is to calculate the distance/radius

6 Likes