This problem is in 3D space. My question is: How do you find the initial velocity of a projectile that flies under constant gravity, so that it hits a target moving with constant velocity, given the parameters:

  • Launch Position
  • Launch Angle
  • Target Position
  • Target Velocity
  • Acceleration due to Gravity

Most of the answers I find solve the problem using Launch Speed instead of Launch Angle.

The reason why I want to use Launch Angle, rather than Launch Speed, is because the arc of the projectile doesn't look good when solving with Launch Speed. The low angle is too low, and the high angle is too high.

My goal is to be able to adjust the arc of the projectile to be more visually pleasing, while still being able to hit the moving target 100% of the time.


2 Answers 2


I'm assuming that your gravity acts on the vertical axis, and that your launch angle is an altitude (measured vertically from the horizon, so 0° means firing horizontally, and 90° means firing straight up). Our turret is still free to pivot on the azimuth (side to side) to track the target and intercept it if it's moving laterally.

The first thing we'll do is take the absolute target position \$\vec p_T\$ and launch position \$\vec p_L\$ construct a relative target position vector \$\vec r\$, and its projection on the horizontal plane \$\vec r_h\$ and vertical axis \$r_v\$:

$$\begin{align} \vec r &= \vec p_T - \vec p_L\\ r_v &= \vec r \cdot \vec {up}\\ \vec r_h &= \vec r - r_v \vec {up} \end{align}$$

This lets us take launch position right out of the equation. We can apply the 2nd & 3rd steps to the target's velocity vector \$\vec v\$ to split it into a horizontal vector \$\vec v_h\$ and a vertical component \$v_v\$.

Now if we want to hit the target at an initially unknown time \$t\$ seconds after launch, we'll need to cover the horizontal displacement \$\vec r_h + \vec v_h t\$ in a straight line in time \$t\$. So the horizontal component of our launch velocity \$\vec l_h\$ is:

$$\vec l_h = \frac {\vec r_h + \vec v_h t} t$$

Given our desired launch angle \$ \theta \$, we know the ratio between the horizontal and vertical launch speeds is:

$$\begin{align} \tan \theta &= \frac {l_v} {\| \vec l_h \|}\\ \tan \theta \cdot \|\vec l_h\| &= l_v\\ \tan^2 \theta \cdot l_h^2 &= l_v^2\\ \tan^2\theta \left( \frac {\vec r_h + \vec v_h t} t \right)^2&= l_v^2\\ \tan^2\theta \left( \frac {r_h^2} {t^2} + 2 \frac {\vec r_h \cdot \vec v_h} t + v_h^2 \right) &= l_v^2 \end{align}$$

Now we want to know if that vertical launch velocity will bring us to our target's height at the end of the arc at time \$t\$:

$$\begin{align} l_v t - \frac g 2 t^2 &= r_v + v_v t\\ l_v &= \frac {r_v} t + v_v + \frac g 2 t\\ l_v^2 &= \frac {r_v^2} {t^2} + \frac {2 r_v v_v} t + r_v g + v_v^2 + v_v g t + \frac {g^2} 4 t^2\\ \tan^2\theta \left( \frac {r_h^2} {t^2} + 2 \frac {\vec r_h \cdot \vec v_h} t + v_h^2 \right) &= \frac {r_v^2} {t^2} + \frac {2 r_v v_v} t + r_v g + v_v^2 + v_v g t + \frac {g^2} 4 t^2\\ \tan^2\theta \left( r_h^2 + 2 \vec r_h \cdot \vec v_h t + v_h^2 t^2 \right) &= r_v^2 + 2 r_v v_v t + r_v g t^2 + v_v^2 t^2 + v_v g t^3 + \frac {g^2} 4 t^4\\ 0 &= t^4 \cdot \left(\frac {g^2} 4 \right)\\ &+ t^3 \cdot \left( v_v g \right) \\ &+ t^2 \cdot \left(r_v g + v_v^2 - \left( \tan \theta \right)^2 v_h^2 \right)\\ &+ t \cdot 2 \left(r_v v_v - \left( \tan \theta \right)^2 \vec r_h \cdot \vec v_h \right)\\ &+ r_v^2 - \left( \tan \theta \right)^2 r_h^2\\ \end{align}$$

Now we have a quartic equation in one variable with known real number coefficients. As described in this answer, you can apply your favourite quartic solving routine (there's an example in the comments) to find potential values of \$t\$ that satisfy the equation. In general there could be up to four solutions. We're looking only for those that...

  • are real numbers (zero imaginary component)
  • are greater than zero (negative values correspond to shots the target could have lobbed at us to strike us at our launch angle at time 0)
  • yield a positive vertical launch velocity \$l_v\$. (Since the formula we solved involved only the square of the angle's tangent, we discarded the sign, so we can get some false positives that aim downward instead of upward)

If you have multiple candidate solutions that meet those criteria, you can choose freely between them. Generally the smaller the time value \$t\$ the shallower the arc, but also the less time the target has to change course and dodge. Higher \$t\$ solutions typically correspond to taller arcs (we spend more time flying up and down through the air), giving the target more time to evade.

With your chosen time \$t\$ in hand, you can substitute it into the equations above to find your horizontal & vertical launch velocity components, and combine them into the final velocity.

  • \$\begingroup\$ Fixed some math errors - when transcribing the formula I accidentally skipped the v_v term, so everything after that was way off! \$\endgroup\$
    – DMGregory
    Aug 3, 2019 at 17:12

Thank you again very much! You are very generous with your time sir. This got me 90% of the way there. For me, I had to make a bit of a modification to the final quartic equation to get it working properly:

$$\begin{align} \\ t^4 \left(\frac {g^2} 4 \right) + t^2 \left(r_v g - \left( \tan \theta \right)^2 v_h^2 \right) + t \left( 2 \left( \tan \theta \right)^2 \vec r_h \cdot \vec v_h \right) + \left( \tan \theta \right)^2 r_h^2 - \vec r^2 &= 0 \end{align}$$

  • \$\begingroup\$ Sorry about that DMGregory, thank you for the correction. I will mark your answer as the best one. I am not sure how to incorporate my modification into your answer, or if I should mark my answer as the best one. \$\endgroup\$
    – JPSmithpg
    Aug 2, 2019 at 17:37
  • \$\begingroup\$ Can you show the mathematical derivation that leads to the changes you made? It's not clear to me whether I made an error somewhere, or if these changes correspond to a slightly different scenario, or what they signify. \$\endgroup\$
    – DMGregory
    Aug 2, 2019 at 17:40
  • \$\begingroup\$ The issue I was getting was this: If the 5th coefficient in your equation was set to 0, I would get 0 solutions for time when solving the quartic equation. The first two solutions would be NaN, and the second two would be 0. I am using Andrew S. Glassner's approach to solving quartic equations, found in Graphic Gems I. In other solutions to this problem (using speed instead of launch angle), the 5th coefficient is the vector r^2, so I tried that in instead of 0. When I multiply the 5th coefficient by -1 (hence the - r^2), it worked. \$\endgroup\$
    – JPSmithpg
    Aug 2, 2019 at 17:53
  • \$\begingroup\$ My first draft was missing an r_v^2 in the constant term. That might have been the source of the issue. Have you tried the edited formula? \$\endgroup\$
    – DMGregory
    Aug 2, 2019 at 17:54
  • \$\begingroup\$ Using your modified formula, it works at 45 degrees, but displays erratic behavior at other degrees (30 degrees looks more like 60 degrees). My current implementation to solve the quartic equation requires the constant term to be the 5th coefficient. So I am not sure if there are any further errors with your final formula, or if it is just the specific way I am solving the quartic equation. As of right now the formula I posted works for my situation perfectly. Setting the angle between 0 and 90 works as expected. It adjusts the angle correctly, and still hits the target. \$\endgroup\$
    – JPSmithpg
    Aug 2, 2019 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .