Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Find angle between two 3-dimensional parts?

Asked by 7 years ago

Hi.

I'm trying to find a way to get a CFrame angle from the magnitude of two 3-dimensional parts. For example, if I were to have a line go across the points of two parts, then find the angle of that line. What is the best method of doing that?

0
I believe you need to use trignometry. Use Cosine. http://wiki.roblox.com/index.php?title=Trigonometry User#17125 0 — 7y
0
to elaborate, the magnitude is the hypotenuse, the length of a line from one parts x and z coordinates to anothor's is the adjacent side, and finnaly the opposite side is the hieght diffrence between both parts. The angle is theta User#17125 0 — 7y
0
Make sure to look at my updated code! I had a mistake in it! My apologies! wierdgamer100 130 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

Let's say one part (let's call it "Brick") is at the point Vector3.new(0, 0, 0) and another (let's call it "Brick1" is at Vector3.new(5, 7, 6). To calculate the angle, we will be using math.asin, which is the inverse of sine. If you haven't taken trigonometry, inverse sine is used to find an angle in a triangle based on the fraction of the length of the opposite side of the angle divided by the hypotenuse. In this case, the hypotenuse is the magnitude of the two vectors (magnitude is the distance between two points). To calculate the magnitude, one would write:

local Magnitude = (Brick.Position - Brick1.Position).Magnitude --This calculates how far apart the blocks are away from each other, or their magnitude.

Then we need to find the opposite side of the angle, which is the height difference of the part. To do this, we will be using the Y values of the two positions. distance formula and math.sqrt, except using only two points. So, our code would then be this:

local Magnitude = (Brick.Position - Brick1.Position).Magnitude --This calculates how far apart the blocks are away from each other, or their magnitude.

local Height = math.sqrt((Brick.Position.Y - Brick1.Position.Y)^2) --This calculates the height difference between the two positions.

We've just pretty much created two sides of a triangle, which is all we need for inverse sine. Our code would then be:

local Magnitude = (Brick.Position - Brick1.Position).Magnitude --This calculates how far apart the blocks are away from each other, or their magnitude.

local Height = math.sqrt(Brick.Position.Y - Brick1.Position.Y) --This calculates the height difference between the two positions.

local Sine = Height / Magnitude --It is height over magnitude since sine is opposite over hypotenuse, as we stated height is opposite the angle in a triangle and magnitude is the hypotenuse.

local Angle = math.asin(Sine) --This calculates our angle measure, in radians (a unit to measure angles). If you'd like it in degrees, read the line below.

local AngleInDegrees = math.deg(Angle) --"math.deg()" converts a value from radians to degrees.

If you have any problems or need any clarification, feel free to comment.

Ad

Answer this question