Finding the angle between a point and another point with an angle

5k Views Asked by At

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

2

There are 2 best solutions below

6
On 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 :)

0
On

Assume that the origin is $(0, 0)$ and that your character (blue dot) is located at $(a, b)$, and that the character's angle of rotation (clockwise from "up") is $\theta$. From the character's perspective, we can construct two vectors:

  • A (unit) vector that points in the direction of the character's line of sight: $\vec u = (\sin\theta, \cos\theta)$.
  • A vector that points in the direction of the origin: $ \vec v = (-a, -b)$.

We want the angle $\phi$ between $\vec u$ and $\vec v$, and we can indeed use the dot product to compute it via the formula: $$ \phi = \arccos\left( \frac{\vec u \cdot \vec v}{|\vec u||\vec v|} \right) = \arccos\left( \frac{-a\sin\theta - b\cos\theta}{\sqrt{a^2 + b^2}} \right) $$


Applying this to your examples, we have:

  • $\phi = 155^\circ$ if $a = 5, b = 5, \theta = 20^\circ$
  • $\phi \approx 178.2^\circ$ if $a = 5, b = 2, \theta = 20^\circ$