- Home /
Mathf.atan2 vs Vector2.SignedAngle
Which is the difference between Mathf.Atan2 and Vector2.SignedAngle? I was using both of them for a problem and they seems to give different answer, but when I searched on internet it gave me both of them for the solution of the same problem. Why?
Answer by Pangamini · Jan 29, 2019 at 03:40 PM
Well, obvious difference is in the input. Atan2's input is 2 floats / 1 vector, SignedAngle's input is 2 vectors; Also, Atan2 returns radians, while SignedAngle returns degrees, perhaps that's the main problem?
You can see the SignedAngle implementation here
public static float Angle(Vector2 from, Vector2 to)
{
// sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
float denominator = Mathf.Sqrt(from.sqrMagnitude * to.sqrMagnitude);
if (denominator < kEpsilonNormalSqrt)
return 0F;
float dot = Mathf.Clamp(Dot(from, to) / denominator, -1F, 1F);
return Mathf.Acos(dot) * Mathf.Rad2Deg;
}
// Returns the signed angle in degrees between /from/ and /to/. Always returns the smallest possible angle
public static float SignedAngle(Vector2 from, Vector2 to)
{
float unsigned_angle = Angle(from, to);
float sign = Mathf.Sign(from.x * to.y - from.y * to.x);
return unsigned_angle * sign;
}
taken from HERE
Unity's Mathf.Atan2 simply calls and returns value of System.Math.Atan2, you can read about it HERE
Also notice that SignedAngle uses two transcendentals, Sqrt and Acos, while Atan2 is a single one. So I assume that Atan2 is approx. twice as fast, use that provided the other vector is the X axis (so you don't really need the second vector to be variable)
Your answer
Follow this Question
Related Questions
how do i keep a gameobject in between the union of 2 or more than 2 circles 0 Answers
Events called every Nth day 1 Answer
Determinism problems: Mathf, Unity.Random VS Math, System.Random 2 Answers
Mapping dot product to positive range 1 Answer
How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer