Getting the angle between two vectors


I’m trying to find the angle between these two angles. Does anyone have an idea about what to use? I’ve been thinking about using Vector3:Cross() or Vector3:Dot() but I don’t know which one to use I guess. Or maybe CFrame?

5 Likes

The Dot product would be used for this, as the equation for the dot product of two vectors is:

va.vb = |a|*|b|*cos(theta)

assuming both vectors are unit vectors, the dot product between the two would be

cos(theta)

where theta is the angle between the two vectors.
After that you can use

acos(a.b)

to get the angle as long as it is between -90 and 90 degrees

final result:

local function getAngleFromUnitVectors(a,b)
    local dot = a:Dot(b)
    return math.acos(dot)
end
11 Likes

Although theking’s response is valid, if you need an angle in the range -pi to pi (that is, the entire circle such that the quadrant is preserved) you can utilize the atan2 function of the math library. Just like regular inverse tangent, it expects the opposite and adjacent sides, but as separate arguments.

3 Likes

@theking48989987’s answer is perfect! But there is actually another way to find the angle between 2 vectors, using the dot product as well.

It turns out that

cos(theta) = u.v/|u|.|v|

So, to find theta we would have to use arcos

theta = arcos(u.v/|u|.|v|)

svg

function angleBetweenVectord(u, v)
   return math.acos(u:Dot(v)/u.magnitude*v.magnitude)
end

Another way of finding the angle between 2 vectors is the cosine rule. I recommend you check it out

8 Likes