2d simulation shooting projectiles that inherit the gun's veocity at a moving target while also moving Overview: I'm working on programming a simulation that requires 'shooting' projectile-type objects at other moving objects. How can I calculate the angle at which to shoot the object to hit?
Details:
Imagine you're holding some gun-type object that shoots projectiles at velocity $v_d$ in straight lines. You are moving with velocity $v_p$ at angle $\theta_p$, in a straight line. Another object, which we will name the target, is located a distance $R$ away from you at an angle $\theta_{tp}$, and is moving in a straight ine with velocity $v_t$ at angle $\theta_t$. 
I need to calculate the angle $\theta_a$ I should aim the gun in order to hit the target. Note that there is no decision on the TIME to fire.  I have to fire immediately, at $t=0$, so this adds a helpful constraint but also makes it so that some targets are not able to be hit based on the numerical values of the parameters mentioned, which is fine.  The target is moving so you need to lead it with your shot to compensate. Also, the projectile will inherit your velocity on top of its own since you are also moving, which will further modify the angle you need to shoot at. 
It seems to me that the strategy should be that I find a point in space which will be occupied by both the projectile and the target. I know all the points in space that the target will take along its linear path, and the times at which it will take all points. However, because the total velocity of the projectile varies based on the angle you shoot it (e.g. the magnitude of the velocity vector will be different depending on what Theta_a you decide to shoot at), I'm not sure how to figure out where to shoot it. 
Any help would be appreciated! 
In addition, if it is easy, a solution to a target moving in a circular path with radius $R$ (same $R$ as the distance between you and it, so it's making a perfect circle around you) would be appreciated!
 A: First of all, you should do the math in terms of vectors, i.e. position $\vec{x} = (x, y)$, velocity $\vec{v} = (v_x, v_y)$. Then for every object the position over time can be expressed as $\vec{x}(t) = \vec{x}_0 + t\vec{v}$, where $\vec{x}_0$ denotes the position at the time $t=0$, $\vec{v}$ the constant velocity, and $\vec{x}(t)$ the position at a given time $t$.
To simplify the picture, you can do the calculations in the co-moving reference frame of the gun. The pointing angle of the gun will not change by this (as long as you aren't considering relativistic velocities ;) ). The ejection velocity is defined in the gun's frame of reference anyway.
To do this, let's write the target time dependent position in the universe frame as:
$$\vec{x}_t(t) = \vec{x}_{t0} + t \vec{v}_t$$
and the gun's position as
$$\vec{x}_g(t) = \vec{x}_{g0} + t \vec{v}_g$$
To transform to the gun's reference frame, just subtract the gun's position equation; the ' denotes the new reference frame. Then we get:
$$\vec{x}_t'(t) = \vec{x}_{t0} - \vec{x}_{g0} + t (\vec{v}_t - \vec{v}_g),\quad \vec{x}_g'(t) = 0$$
Or, with $\vec{x}_{t0}' = \vec{x}_{t0} - \vec{x}_{g0}$, and $\vec{v}_t' = \vec{v}_t - \vec{v}_g$ you get
$$\vec{x}_t'(t) = \vec{x}_{t0}' + t \vec{v}_t'$$
Into this equation you can simply insert the time when you want to hit the target, and you get the position of the hit. As the gun is located at $0$, this also gives the direction to which you have to point your gun. The required velocity then is defined by the distance to the hitting point divided by the hitting time.
The angle can simply calculated with the atan2 (a 360-degree complete version of the arctangent function available in most programming languages) of the required velocity vector for the projectile $\vec{v}_p = (v_{px}, v_{py})$ by theta = atan2(vpy, vpx). This of course depends on your definition of theta, so you might need to adjust it.
If you shoot with a predefined velocity, you can write an equation with the hitting time as a variable and solve for that.
This should show the way to get your result, you might have to work out some more details, or ask again for something specific (e.g. vector arithmetics) if this isn't sufficient.
A: I found the solution posted by Gareth Rees to work, which was posted at this link:
https://stackoverflow.com/questions/4107403/ai-algorithm-to-shoot-at-a-target-in-a-2d-game

Notation: I write vectors in capital letters, scalars in lower case,
  and ∠V for the angle that the vector V makes with the x-axis. (Which
  you can compute with the function atan2 in many languages.)
The simplest case is a stationary shooter which can rotate instantly.
Let the target be at the position A and moving with velocity VA, and
  the shooter be stationary at the position B and can fire bullets with
  speed s. Let the shooter fire at time 0. The bullet hits at time t
  such that |A − B + t VA| = t s. This is a straightforward quadratic
  equation in t, which you should be easily able to solve (or determine
  that there is no solution). Having determined t, you can now work out
  the firing angle, which is just ∠(A − B + t VA).
Now suppose that the shooter is not stationary but has constant
  velocity VB. (I'm supposing Newtonian relativity here, i.e. the bullet
  velocity is added to the shooter's velocity.)
It's still a straightforward quadratic equation to work out the time
  to hit: |A − B + t(VA − VB)| = t s. In this case the firing angle is
  ∠(A − B + t (VA − VB)).
What if the shooter waits until time u before firing? Then the bullet
  hits the target when |A − B + t(VA − VB)| = (t − u) s. The firing
  angle is still ∠(A − B + t(VA − VB)).
Now for your problem. Suppose that the shooter can complete a half
  rotation in time r. Then it can certainly fire at time r. (Basically:
  work out the necessary firing angle, if any, for a shot at time r, as
  described above, rotate to that angle, stop, wait until time r, then
  fire.)
But you probably want to know the earliest time at which the shooter
  can fire. Here's where you probably want to use successive
  approximation to find it. (Sketch of algorithm: Can you fire at time
  0? No. Can you fire at time r? Yes. Can you fire at time ½r? No. etc.)

