# Calculating time of flight for projectile with gravitational trajectory

Jeff Peff
Greenhorn
Posts: 1
Hi!

I've been lurking some time on the forums but today I encountered a problem which I simply can't seem to solve. I've looked at various sites explaining projectile trajectories in combination with gravity but I can't seem to get my code right.

What I am trying to figure out is the amount of time in seconds a projectile is traveling while completing its flight. I've seen all kinds of (hard to read) formula's and have concocted the following:

The angle is currently at 45 degrees but it should also accept negative degrees such as -10. Seaching for formula's which incorporate an initial velocity (which is probably what I need) all gave me a complete different formula than the one I have. I think I've got it for 99% but can't seem to find the last piece.
The sample might be a bit messy but it was just to figure out what kind of formula goes behind this all.

Thank you for your time!

Mich Robinson
Ranch Hand
Posts: 260
1
Split the movement of the projectile into two parts - horizontal and vertical speeds.
Then just use the vertical velocity as that's the only bit that matters.
You could work out the height of a projectile at any given moment by applying the vertical climb and subtracting gravity.
Obviously gravity will be -10m/s after 1 sec, -20m/s after 2 secs etc.
When the height reaches 0 then that's how long it stays in the air.
Then I'd just store these results in a quick look up table and work out the vertical velocity on the fly from the angle.
This will work fine for games but I'd suggest something more rigorous if you're planing on launching men into space

Mike

Andrew Kenyon
Greenhorn
Posts: 12
I recognize this is an old topic, but I felt like taking a crack at it anyway. Essentially it is
just an implementation of Mike's suggestion. The quadratic function was added to provide
a better prediction of the total time the projectile is in motion.

The formulas can be found in any physics I textbook (or online). As for the reason behind
all the variable name changes, it just made it easier for me to keep track of the components.

Abridged Legend:

vi := Velocity Initial
vxi := Velocity X-component Initial

you get the idea.

Guillermo Ishi
Ranch Hand
Posts: 789
Andrew Kenyon wrote:I recognize this is an old topic, but I felt like taking a crack at it anyway.

I plotted some of the output and it gives a nice arc. Nice work. I was snooping around and found this code using differential equations translated to code.

http://www.codeproject.com/Articles/19107/Flight-of-a-projectile

Andrew Kenyon
Greenhorn
Posts: 12
Good find. I've bookmarked it for my own reference.

From reading over the OP's comments (and the comments in his code), I didn't get the impression that he'd had any recent exposure to the calculus or even to algebra-based physics.
With that in mind, I tried to keep the changes easy to follow (if he ever checks back). I do really like that code in that link you provided, and if I ever need to build something more
robust I'll definitely use it for inspiration.