Rotation Radians and Angles help

I am trying to get an angle between two points

The formula for doing this is inverse tan(absolute y value / absolute x or z value).
In my case:

math.atan(height / 1.5)

I know both height and 1.5 are the correct values. However, I am having issues with the output. Apparently, the output of atan is a radian. Sure, so when I put it into a CFrame angle, it should give the correct rotation (without me needing to use math.rad).

Of course, it doesn’t, I’ve tried multiple things such as using math.deg or math.rad or neither. I’ve tried converting my radian back into degrees then using math.rad in the CFrame.

Here’s the code I’m using (it’s sloppy, right now I’m not trying to make it look neat)

local height = math.abs(npoint.Position.Y) - math.abs(point.Position.Y)
			local pyth = 1.5^2 + height^2 
			pyth = pyth^0.5
			local rot = math.atan(height / 1.5)
			local cfra = CFrame.new(Vector3.new(point.Position.X, point.Position.Y, point.Position.Z)) * CFrame.fromEulerAnglesXYZ(math.rad(rot), 0, 0)
			beam.Length = pyth
			beam.CFrame = cfra

I think I’ve tried using CFrame.Angles instead of CFrame.fromEulerAnglesXYZ however, I can’t be sure. It still shouldn’t affect it.

I’ve concluded that the issue is in math.atan and my use of the output, I know both variables in the tangent are correct. All help appreciated

Hello!

My sincerest apologies, but would it be possible to clarify what you mean by “angle between two points”? I am a bit concerned with the seemingly arbitrary “1.5” on the second line too, but an answer to my first query would be most helpful!

So I have 2 points in the world. They are at different heights. They are on the same X axis. The distance between them is 1.5 studs. I need to find the angle for say point 1 to point 2

Ahhh, I see. I am still not entirely clear if you want the angle between the two points and the origin, or if you want the angle along the XY plane between the two points and the origin.

I will explain how to find the angle between two points and the origin.

local angle = math.acos(point.Position:Dot(npoint.Position)/(point.Position.Magnitude * npoint.Position.Magnitude))

This is the formula that you can use to find the angle between the two points (as stated in your original code) and the origin and assign it to a variable called “angle”. To explain what I have done in greater detail would require a bit of knowledge about vectors, but I will still endeavor to explain it as simply as possible.

To lay it out, I have found the dot product of the two position vectors and divided it by each of the magnitudes of the respective position vectors. After that, I simply found the arccosine of that result to get what we wanted.

To break down each step, the dot product finds the magnitude of one vector that is in the same direction as a second vector and multiplies that with the magnitude of the second vector.

So how would we write this out in a mathematical notation? We could try:

a · b = ||a|| * ||b|| * cos(θ)

Where a and b are two vectors and θ is the angle between them. Observe how the multiplication of the magnitude of a with cos(θ) IS the magnitude of a in the direction of b. Hence, why this expression makes geometrical sense.

With this in mind let us move on to the second step. To refresh you, I divided this dot product with the magnitudes of each vector. To visualise this in a mathematical sense:

a · b / ( ||a|| * ||b|| ) = ||a|| * ||b|| * cos(θ) / ( ||a|| * ||b|| )

a · b / ( ||a|| * ||b|| ) = cos(θ)

From here, I think it fairly obvious as to how we would get θ from here. We simply apply the arccosine operator to both sides. Visualising this again:

arccos(a · b / ( ||a|| * ||b|| )) = arccos(cos(θ))

arccos(a · b / ( ||a|| * ||b|| )) = θ

And hey, we got θ! If you would then like to see how to express this in lua, refer to the earlier line of code. Hope this helps!

1 Like

Hey, thanks a ton for the detailed response. And I will definitively try and imply it when refining the code. However, in a rather peculiar chain of events, I managed to work out a solution in probably the sloppiest math I have done in my life. Your help is immensely appreciated, and best of luck to you. (Here’s my makeshift math)

local height = math.abs(npoint.Position.Y) - math.abs(point.Position.Y)
			local pyth = 1.5^2 + height^2 
			pyth = pyth^0.5
			local rot = math.atan(height / 1.5)
			rot = rot - rot - rot
			local cfra = CFrame.new(Vector3.new(point.Position.X, point.Position.Y, point.Position.Z)) * CFrame.Angles(rot, 0, 0) * CFrame.Angles(math.rad(180), 0, 0)
			beam.Length = pyth
			beam.CFrame = cfra

Do I know why it works? No.