How do I get the angles between 2 vector3 points

how to find “alpha” and “beta” given 2 vector3 points

alpha = math.atan2(z, x)
beta = math.atan2(y, z)
angle_between = math.acos(math.clamp(Vector1:Dot(Vector2) / (Vector1.Magnitude * Vector2.Magnitude)), -1, 1)
2 Likes

what is z? what is x? what is y?

They are the x, y, z components of the vector

components of which vector? the middle or the top one

The image you posted only shows 1 vector. It looks like the blue and red dotted ones represent the projections of that green vector onto those respective planes. In my first post x, y, and z are the coordinates of the green vector.

im finding the angles between the vectors of bottom left (the origin) to the top right

the angles of the origin and the tip of green’s arrow is what im finding, not to be mistaken with blue, green is the combination of red and blue

With respect to what other vector? You can’t compute an angle if one of the vectors has no magnitude.

here is an image to explain what i mean

i want to find 2 angles

the X and Y angles, X being right and left, Y being up and down

so combined, they can be used to locate the green brick

Alright, sorry for the confusion. You basically want to convert a set of cartesian coordinates into spherical coordinates.

local point = --grey part position (direction and magnitude of the brown line)

-- the orange line in the image represents the projection of the brown vector onto the XZ plane.
local xzProjection = point * Vector3.new(1, 0, 1)

-- let theta be the angle between the blue unit vector and the orange projection vector.
-- also known as the 'azimuth 'or longitude
local theta = math.atan2(xzProjection.X, xzProjection.Z)

-- let phi be the angle between the projection and the original brown vector
-- also the 'polar angle' or latitude
local phi = math.atan2(point.Y, xzProjection.Magnitude)

-- finally, our 'r' value is just the distance from the origin
local r = point.Magnitude

You can also re-construct the position using CFrame operations, too.
Because the coordinate system is kind of weird on roblox, I had to rotate the azimuth by 180 degrees and make r negative but this should give you the original position:

local cf = CFrame.fromOrientation(phi, theta + math.pi, 0) * CFrame.new(0, 0, -r)
local position = cf.Position
1 Like