Search code examples
mathvectorgraphicslua

Angle between vectors atan2(y2,x2) - atan2(y1,x1) explanation


So while playing with the game framework Love2D lua engine I noticed the following formula inside HUMPS vector code (https://github.com/vrld/hump/blob/master/vector.lua):

return atan2(self.y, self.x) - atan2(other.y, other.x)

Note: atan2 is described here https://en.wikipedia.org/wiki/Atan2 :basically it is a safe atan function that protects against divide by zero and some other atan pitfalls - to my understanding.

I've been drawing some graphs of two separate vectors and trying to figure out how someone would come up with a formula like this. The context for this formula, for me at least, is rotating an image (image_pos) to where the mouse has clicked (mouse_pos).

Can anyone please ELI5 how this formula was derived/how it works?


Solution

  • As you can see in the picture it is the difference of angles between two vectors.

    enter image description here

    atan2(self.y, self.x) == \beta

    atan2(other.y, other.x) == \alpha

    Hence:

    atan2(self.y, self.x) - atan2(other.y, other.x) == \theta