5

When calculating the angle between two vectors, I have traditionally used acos, but this requires the two vectors to be normalised. atan2 can be used to accomplish the same (specifically atan2(b.y_, b.x_) - atan2(a.y_, a.x_)), does this require normalised vectors?

If atan2 doesn't require normalised vectors, would this be better to use since normalisation can be costly and 'more' error prone since it requires a sqrt operation?

Then I read that atan2 itself can be more costly than acos, but more accurate? And then I also read other interwebs suggesting the opposite :( lots of conflicting information, unsure what the deal is with using acos or atan for calculating the angle between two vectors.

Which is recommeneded? and what are the benefits/issues for each usage?

Any help would be appreciated, thanks!

lfgtm
  • 1,037
  • 6
  • 19
  • 4
    `atan2` does not require normalised vectors – Damien Jul 05 '19 at 11:58
  • 2
    No, `atan2()` does not required any scaling/normalising of inputs, as long as at least one argument is non-zero. Recommended usage depends on how the input values are supplied. Simple minded advice like "never use `acos()`" is simply dumb - the skill is picking an appropriate option based on how input data is supplied/calculated, and the form of output required. Sometimes more than one alternative may be appropriate. – Peter Jul 05 '19 at 12:34

1 Answers1

8

No, atan2 does not require normalized vectors, and if your vectors are not already normalized you shouldn't pre-normalize them as that may slightly reduce precision. The function works correctly for any inputs other than (0,0).

You should never use acos for anything.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Thankyou. atan2 certainly would be more useful in a number of situations for me due to not having to normalise ;) – lfgtm Jul 05 '19 at 12:08
  • 4
    You may add that `atan2` provides a result modulo 2-pi, on the contrary of `acos` or `atan`. – Damien Jul 05 '19 at 12:12