How do you calculate the Range and Flight Time of a trajectory if the initial height is greater than zero (assuming you're in a vacuum)?
The regular formulas I come across don't take initial height into account. Below are the regular formulas:
$$ \textrm{Flight Time} = \frac{2 * \textrm{initial_velocity} * sin(\textrm{angle})}{G} $$ $$ \textrm{Range} = \frac{\textrm{initial_velocity}^2*sin(2*\textrm{angle})}{G} $$
I am working on a trajectory calculator and I can plot the trajectory fine on a graph using these two formulas:
$$ X = \textrm{initial_position}_x + (\textrm{initial_velocity}_x * \textrm{delta_time}) $$
$$ Y = \textrm{initial_position}_y + (\textrm{initial_velocity}_y * \textrm{delta_time}) + (\frac{G*\textrm{delta_time}^2}{2}) $$
After this, I use formulas to calculate the precise flight time, range, and max height. These work fine, until I change the initial height, and then they miscalculate.
I'll make the usual first-year physics assumptions that there is no air resistance and no effects from the curvature of the Earth or from the Earth's rotation. (That is, we pretend the Earth is flat and non-rotating.) Also, we assume gravity is constant.
Assuming the initial firing direction is at some upward angle, you have an initial upward velocity component and an initial forward velocity component.
From the initial height and upward velocity you compute the time until the top of the trajectory, when the projectile has zero vertical velocity. You also compute the height at the top of the trajectory (initial height plus height gained during the time you just calculated).
From the height at the top of the trajectory, and the height of the impact area (I'm guessing this is zero for your problem, since you only said the initial height was not zero), you compute the amount of time spent falling.
The total flight time is the time going up plus the time going down.
During the total flight time, the projectile continues moving at the same horizontal velocity. Use that to compute the range.
These are the same calculations that the "regular" formulas come from, except that the "up" and "down" parts are not equal.
Alternatively, you can use your formula for height. For easier mathematical manipulation I'll write $y_0$ for "initial position $y$", $v_{y0}$ for "initial velocity $y$", and $t$ for "delta time":
$$ y = y_0 + v_{y0} t + \frac12 g t^2. $$
Swap the two sides of the equation and subtract $y$ from both sides:
$$ \frac12 g t^2 + v_{y0} t + y_0 - y = 0. $$
Now if we let $a = \frac12 g,$ $b = v_{y0},$ and $c = y_0 - y,$ this is
$$ at^2 + bt + c = 0, $$
Which is a quadratic equation with variable $t$. We want to solve this for $t.$
You can find the quadratic equation and its solution explained in many places, for example here. Most explanations use $x$ as the variable to be solved, that is, the equation is $ax^2 + bx + c = 0,$ but the solution works just as well if we use $t$ instead of $x.$
The solution for $t$ is
$$ t = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} = \frac{-v_{y0} \pm \sqrt{v_{y0}^2 - 2g(y_0 - y)}}{g}. $$
This standard solution gives two values for $t,$ one when you replace the $\pm$ sign with $-$ and one when you replace it with $+.$ There are two solutions because if you extend the trajectory forward and backward in time, it is a parabola, and for each $y$ coordinate below the top of the parabola, the parabola crosses the $y$ coordinate twice, once on the way up and (later) on the way down. Since you want the "on the way down" solution, where the projectile falls onto the ground at height $y,$ you want the later solution (greater value of $t$).
You might think you get the greater value of $t$ by changing the $\pm$ to $+$, but since $g$ is negative you actually want the numerator to be as negative as possible, so use a $-$ operation:
$$ t = \frac{-v_{y0} - \sqrt{v_{y0}^2 - 2g(y_0 - y)}}{g}. $$
Now set $y$ to whatever your final height should be, and use the equation above to find the time $t$ at which the projectile finally falls to that height. For example, if you launch the projectile from a platform at height $y_0$ and are trying to find when and where it hits the ground at height zero, set $y = 0.$ But this formula also works for other cases of unequal height, such as where you fire from the ground ($y_0 = 0$) onto a platform at height $y$.
This value of $t$ is your total flight time.
Once you have found your total flight time, use the formula for horizontal distance traveled to find the range.
Now for a note connecting the two solutions:
Notice that we can write the solution this way:
$$ t = \frac{-v_{y0}}{g} + \frac{-\sqrt{v_{y0}^2 - 2g(y_0 - y)}}{g}. $$
Since $g$ is negative, this is the sum of two positive numbers. The first number, $\frac{-v_{y0}}{g},$ is the time it takes from the instant of the launch until the projectile reaches the top of its trajectory. The second number is the time it takes to fall from the top of the trajectory onto the target area.