# Solving the ballistic trajectory equation for Launch Angle

• Comp Sci
zazz0000
Homework Statement:
Solving the ballistic trajectory equation for Launch Angle
Relevant Equations:
y = h + x * tan(α) - g * x² / 2 * V₀² * cos²(α)
In a process of writing a game. Effectively need to know how to angle the barrel for the projectile to hit the selected target.
So for the equation

y = h + x * tan(α) - g * x² / 2 * V₀² * cos²(α)

Everything except α is known. Could anyone more wise in the ways of science than me help me solve for alpha?

Thanks

## Answers and Replies

Homework Helper
Hello @zazz0000 , !
Your y = h + x * tan(α) - 1/2 g * ( x / ( V₀ cos α) )² (note the use of brackets !) requires some explanation ! It is the equation for a parabola that starts at ##\ (x,y) = (0, h)\ ## with a slope ##\tan \alpha## and a speed ##v_0##.

Another (equivalent) representation is in terms of time ##t##: \begin{align*} x&=\ \phantom{h +}v_0\cos\alpha\ t \\ y&=h + v_0\sin \alpha\ t - {1\over 2} g\,t^2 \end{align*}You will recognize the equivalence if you substitute ##\ t = x/(v_0\cos\alpha)\ ##, right ?

To hit a 'selected target', it is useful to know its coordinates ##(x_T, y_T)\ ##

Solving this amounts to solving two equations (##\;x_T = .. \ \& \ y_T = .. \ ##, see above) with two unknowns (##t## and ##\alpha## ).

The case ## y_T = h ## is 'solved' here to give $$\alpha = {1\over 2}\arcsin \left ( g\, x_T\over v_0^2 \right )$$(with zero or two solutions!)

The general case seems to be carefully avoided in the many examples I find googling 'projectile trajectory' (but you want to look at a few of them anyway to get a feeling for game physics !).
And from just looking at it, I don't see any clever analytical solution, but I would be happy to be proven wrong !

And the award goes to @PeroK -- see #12 (and #13 )

Even if there is an analytical solution: the next refinement is to add air drag and then you are back to having to do it numerically anyway...

##\ ##

Last edited:
zazz0000
Hi @BvU

Appreciate the response and the breakdown.
I've been scouring the internet and other various resources and as you point out, the case does seem to be generally avoided.
So it seems like there's no elegant solution for solving for α here (unless I'm looking right past it).

Full disclosure, for the purposes of my project I, having exhausted most other options, decided to take a brute force approach and simply testing the original function for arbitrary values of α, adjusting them based on some heuristic and retesting until the result puts me within a set tolerance for accuracy. The results are very good so far and if I can come up with a better heuristic it seems that I would be able to find an acceptable α in under 10 iterations or so.

As for having air drag, while tempting, I would have to justify implementing it for projectiles. I currently use air drag for vehicles in a simplified form of R = ½v^2 * drag, where drag is a constant representing the combination of fluid density, drag coefficient and reference area (for my purposes, all those can remain constant). If I can incorporate that into the original trajectory function, it might just be worth it.

Thanks again for the reply!

Homework Helper
Full disclosure, for the purposes of my project I, having exhausted most other options, decided to take a brute force approach and simply testing the original function for arbitrary values of α, adjusting them based on some heuristic and retesting until the result puts me within a set tolerance for accuracy. The results are very good so far and if I can come up with a better heuristic it seems that I would be able to find an acceptable α in under 10 iterations or so.
Numerical solution is one way (Newton-Raphson is very fast with fair initial guesses), and if it's a game you might consider cheating: the analytical solution for ##\ y_T=h\ ## plus a straight line sloping from ##h## to ##y_T## can hardly be distinguished from the correct solution
Wikipedia said: I don't know your context (bullet, arrow, cannonball shooting, throwing ?) so it's hard to say anything further ...

##\ ##

zazz0000
For context, I am shooting bullets or unguided rockets from a helicopter or a ground vehicle.
The ranges can be pretty long, I'd say over a kilometer, while the travel speed of the projectile is currently significantly lower than the real-life counterparts, for example 120m/s vs an actual cannon that would be somewhere around Mach 3.

Homework Helper
Gold Member
2021 Award
For context, I am shooting bullets or unguided rockets from a helicopter or a ground vehicle.
The ranges can be pretty long, I'd say over a kilometer, while the travel speed of the projectile is currently significantly lower than the real-life counterparts, for example 120m/s vs an actual cannon that would be somewhere around Mach 3.
You know the starting height ##h## and the velocity ##v_0## and you want to find the launch angle required to hit a fixed target ##(X, Y)##?

zazz0000
@PeroK that is correct, yes. I suppose it's safe to say that Y will always be zero (not sure if that's of any help)

Homework Helper
Gold Member
2021 Award
@PeroK that is correct, yes. I suppose it's safe to say that Y will always be zero (not sure if that's of any help)
You should be able to get a quadratic equation for the tangent of the launch angle. It may be a bit messy, but you just put these two equations together, by getting rid of ##t##:
\begin{align*} x&=\ \phantom{h +}v_0\cos\alpha\ t \\ y&=h + v_0\sin \alpha\ t - {1\over 2} g\,t^2 \end{align*}

Homework Helper
Getting rid of ##t## results in OP's original equation. For ##\alpha## that's like $$y_T - h = x_T\tan\alpha - {1\over 2} g\left ( x_T\over v_0\right)^2{1\over\cos^2\alpha}$$

##\ ##

Homework Helper
Gold Member
2021 Award
Getting rid of ##t## results in OP's original equation. For ##\alpha## that's like $$y_T - h = x_T\tan\alpha - {1\over 2} g\left ( x_T\over v_0\right)^2{1\over\cos^2\alpha}$$

##\ ##
That's a quadratic in ##\tan \alpha## if you use ##\sec^2 \alpha = 1 + \tan^2 \alpha##.

zazz0000
@PeroK I'm not quite as nimble as you are I'm sure as far as math goes, what would the quadratic look like in the end? And ultimately, how would that be solved for α?

P.S. I really appreciate you guys helping me on this, I thought this thread was gonna go unnoticed and ultimately die

Homework Helper
Gold Member
2021 Award
@PeroK I'm not quite as nimble as you are I'm sure as far as math goes, what would the quadratic look like in the end? And ultimately, how would that be solved for α?

P.S. I really appreciate you guys helping me on this, I thought this thread was gonna go unnoticed and ultimately die
You can simplify things a bit, by setting ##\mu = \frac{v_0^2}{gx_T}##, which is dimensionless. And, noting that ##\frac{y_T - h}{x_T} = \tan \theta## is the tangent of the angle from the launch point to the target. This gives: $$\tan \theta = \tan \alpha - \frac{1}{2\mu}(1 + \tan^2 \alpha)$$ Re-arranging gives:
$$\tan^2 \alpha - 2\mu \tan \alpha + 1 + 2\mu \tan \theta = 0$$ And if you hit that with the quadratic formula you get: $$\tan \alpha = \mu \pm \sqrt{\mu^2 - 2\mu\tan \theta - 1}$$
We can check that in the case when ##\theta = 0## we agree with the result above:
$$\alpha = {1\over 2}\arcsin \left ( g\, x_T\over v_0^2 \right )$$
That solution is $$\sin 2\alpha = \frac 1 {\mu}$$ Whereas, we have: $$\tan \alpha = \mu \pm \sqrt{\mu^2 - 1}$$ We can use the trig identity:
$$\sin 2\alpha = \frac{2\tan \alpha}{1 + \tan^2 \alpha} = \frac{2(\mu \pm \sqrt{\mu^2 - 1})}{2(\mu^2 \pm \mu\sqrt{\mu^2 - 1})} = \frac 1 \mu$$ And that checks out.

Last edited:
• BvU
Homework Helper
I'm impressed ! So much fun with a simple parabola !
One slight improvement $$\tan \alpha = \mu \pm \sqrt{\mu^2 - {\bf 2}\,\mu\tan \theta - 1}$$

(popped up when I was checking with a simple Excel ( ) example -- that I had to retype from scratch since I forgot to save it yesterday )

And that way it dawns on me that
the analytical solution for yT=h plus a straight line sloping from h to yT can hardly be distinguished from the correct solution
Is not miraculous: it is the analytical solution -- you just shouldn't make a mess of things in Excel (as is virtually unavoidable )

##\ ##

Last edited:
• PeroK
Homework Helper
Gold Member
2021 Award
One slight improvement $$\tan \alpha = \mu \pm \sqrt{\mu^2 - {\bf 2}\,\mu\tan \theta - 1}$$
Well spotted, thanks.

Mentor
Another slight improvement:
Re-arranging gives:$$\tan^2 \alpha - 2\mu \tan \alpha + 1 + 2\mu \tan \theta$$
Make this an equation and I'll be happy
$$\tan^2 \alpha - 2\mu \tan \alpha + 1 + 2\mu \tan \theta = 0$$
Now it's ready for an application of the Quadratic Formula.

• PeroK
Homework Helper
Gold Member
2021 Award
Another slight improvement:

Make this an equation and I'll be happy
$$\tan^2 \alpha - 2\mu \tan \alpha + 1 + 2\mu \tan \theta = 0$$
Now it's ready for an application of the Quadratic Formula.
Thanks. It's all the backwards and forwards with this new preview functionality.

Mentor
Thanks. It's all the backwards and forwards with this new preview functionality.
I agree -- I liked the Preview button where it was.

zazz0000
@BvU @PeroK @Mark44
Wow thanks guys, this is awesome! Works flawlessly.
Plus this resource is really nice to have out there, since hours and hours of googling didn't yield a solution for this, even though it seems like a very useful function.

Thanks again!

• BvU and PeroK
$$sin(\theta+\gamma)=\frac{gx}{uv}.$$Then $$\tan\theta=\frac{u-v\cos(\theta+\gamma)}{v\sin(\theta+\gamma)}.$$ There will be 2 answers for the first equation and hence two values of ##\theta##.