# Find projectile initial velocity, and air time, given projectile elevation, distance, angle

I have a target X position (D). I have a turret Y elevation (E). Turret is always pointed at 45 degrees (A). I need to calculate the initial projectile velocity required to hit the target, with an arbitrary gravity constant (G). If your projectile is fired on a 45 degree angle, that's:

$$\vec v = (c, c)$$

...for some scalar component $$\c\$$.

Gravity doesn't affect the horizontal axis, so the time it takes this projectile to cross the horizontal distance to your target is:

$$T = \frac D c$$

Now we can plug that into the equation of our parabola on the vertical axis, tracking the height at time $$\T\$$, $$\h_T\$$ as a function of time, our initial height $$\h_0\$$, our upward speed $$\c\$$, and our acceleration due to gravity $$\g\$$:

$$h_T = h_0 + c \cdot T + \frac g 2 T^2\\ h_T = h_0 + c \frac D c + \frac g 2 \frac {D^2} {c^2}\\ h_T - h_0 - D = \frac {g D^2} { 2 c^2}\\ c^2 (h_T - h_0 - D) = \frac {g D^2} 2\\ c^2 = \frac {gD^2} {2 (h_T - h_0 - D)}\\ c = \sqrt{\frac {gD^2} {2 (h_T - h_0 - D)}}\\ c = \sqrt{\frac {-gD^2} {2 (E + D)}}$$

...taking only the positive root because by construction our vector has to point up and to the right.

Don't be alarmed by the negative sign inside the square root. Because gravity points downward, your acceleration constant $$\g\$$ in this formula is negative, canceling out to a positive.

• Fantastic! I put your resulting equation into my game logic and it works as expected. Thank you! – JoeyJoJo Oct 14 '20 at 23:21