[Math] Finding the angle between a point and another point with an angle

vectors

To preface – I am not from a mathematics background, so I was unsure what this is actually called so I couldn't really look it up. Please let me know if it is a duplicate / against the rules as I am a first time poster.

I have a scenario where there is an origin point, and then a character with a position and rotation. I am trying to figure out how to get the angle between a ray cast from the rotation and position of the character to a ray cast from the origin point to the character.

enter image description here

In this image, I am trying to solve for the green arc's angle. I have tried various solutions with basic trig, but they aren't always reliable when dealing with the other three possible quadrants. (Also note that in these cases 0 degrees is "up").

Calculating an angle would be easy, but I am stuck since the blue point can keep the same angle, but change position.

E.g., the image below has the same character direction as the above of 20 degrees, but a different position.

enter image description here

From what I've tried to research, I've gotten a vague feeling that I should be looking to use something called a "dot product". If you do have an answer, may I ask that it be simple and explanatory? As I said, I am not proficient in this sort of math.

Thank you!

enter image description here

Best Answer

A word of caution: It's highly possible that something is off here, with the frame-of-reference being not quite what I'm used to. Hopefully the ideas are something you can implement and test with a variety of cases to see if something funny happens, with your particular situation. (If anything, I suspect with angles measured clockwise, we might have that $\varphi$ below comes back as $-\varphi$ or something).

enter image description here

I've uploaded another picture with a useful angle marked. If the coordinates of the blue point are $(x, y)$, then the red angle, called $\varphi$ here, can be computed using the $\operatorname{atan2}$ function; that is, $\varphi = \operatorname{atan2}(x, y)$.

Note that both red angles in the picture are $\varphi$.

In the picture, we can see that the green arc is made up of three nice angles:

$$\text{Green Angle} = \varphi + 90^\circ + A,$$ where $A$ is the known angle labeled $20$ in the picture.


A note on $\operatorname{atan2}$: You want your language to have an $\operatorname{atan2}$ function. This is because you need inverse trig functions to find angles, but trig functions are notorious for having "finicky" inverses: if you don't know exactly what you're doing with an inverse trig function, it's easy to get the wrong result half the time. And thus, $\operatorname{atan2}$ was born, it's designed to be very user-friendly :)

Related Question