Find the Angle and Distance to point B from the Origin

trigonometry

I'm about average when it comes to math, so I probably have no business asking this question, but I'm going to ask anyway because I'm curious. I've been playing a video game with artillery, and the player shooting the artillery can't see the target. They can, however, see a different person who CAN see the target. The original person knows the distance and angle to the second person, and the second person knows the distance and angle to the target from themselves.

The game's map is the grid, and angles are measured relative to it. North is 0 Degrees going counter-clockwise (West is 90, South is 180, East is 270). Game is top-down, so for the intents of this question, this is on a 2D plane. I'm just trying to make a program that will help me do this so I don't have to guess anymore.

What I'm given: I'm given the angle and distance from the Origin (Player on Artillery) to Point A (the Spotter) and the Angle and distance from point A to point B (The target).

What I'm looking for: How do I calculate the angle and distance from the Origin to point B (Artillery to target)?

Redirect me if this has been asked before, and I'll try to remove this post. I just couldn't find anything that looked familiar to me when searching for this kind of problem. Also, if there are better tags for this, I honestly have no idea what to put down. My best guess was Trigonometry, as it relates to 3 points and the angles/distances related to them, but I honestly wasn't sure.

Best Answer

Let us say that the distance and direction from you to the spotter are $r_1$ and $\theta_1$. If we imagine that you are at the origin, then the spotter is at $(r_1\cos\theta_1,r_1\sin\theta_1)$. We can think of this as a vector representing the displacement of the spotter, relative to you.

Similarly, if the distance and direction from the spotter to the target are $r_2$ and $\theta_2$, then the displacement of the target, relative to the spotter, is $(r_2\cos\theta_2,r_2\sin\theta_2)$.

The displacement of the target, relative to you is the sum of these displacements: $$(x,y)=(r_1\cos\theta_1+r_2\cos\theta_2,r_1\sin\theta_1+r_2\sin\theta_2)$$

We get the distance by the Pythagorean theorem: $$r=\sqrt{x^2+y^2}$$ Toi get the direction, you might think we'd take $\tan^{-1}\left(\frac yx\right)$ but since this will only give a value between $-\frac\pi2$ and $\frac\pi2$, it doesn't give the correct answer when $x<0$. Programming languages provide the function atan2(x,y) to deal with this.

If the angles are given to you in degrees, you'll probably have to convert them to radians for this to work. You can convert back to degrees at the end if you like.

EDIT

If you're familiar with vectors, then you know that the displacement to the target is the sum of the displacements. If not you can see this way: a displacement of $(x_1, y_1)$ can be achieved by moving a distance of $x_1$ in the $x$ direction, then moving $y_1$ in the $y$ direction, so following it by a displacement of $(x_2,y_2)$ results in a total displacement of $(x_1+x_2,y_1+y_2)$.

Related Question