Issues calculating the angle between two vectors

I want to get the angle between two vectors. I found this website. On that website I found this equation
α = arccos[(a · b) / (|a| * |b|)]
I tried implementing this in code with the following
print(math.deg(0-math.cos(vectorA:Dot(vectorB) / ( vectorA.Magnitude * vectorB.Magnitude))))
but I have a problem. One I couldn’t find arccos or inverse cosine so please correct me if I’m wrong but I believe 0-math.cos() could be used as a substitute I also tried math.cos()^-1 but this I don’t believe worked. Secondly when I input the values of vectors A & B into the website which has a calculator for want I want todo I get a different answer to theirs.

math.acos() exists???

That’s not how it works. It’s the inverse operation of the function, not the additive/multiplicative inverse. If x gives you y, the inverse of the function is yx.

Either because of your faulty “substitute”, or you weren’t converting between radians and degrees properly.

Also, when used with unit vectors, you can disregard the division and it just becomes math.deg(math.acos(vec1:Dot(vec2)))

your correct with acos :slight_smile:
however just for your information math.deg(math.acos(vec1:Dot(vec2))) seems to be giving me a lot of nan not all the time but a lot of the time
when I use acos in my equation it doesn’t do this
math.deg(math.acos(vectorA:Dot(vectorB) / ( vectorA.Magnitude * vectorB.Magnitude)))
regardless ty

That’s an issue with floating point precision i think, just clamp the number with math.clamp between the interval [-1, 1] before using arccosine on it

1 Like