2

I'm making a game and need to figure out how long it will take an object to fall to a certain height.

The object has an initial y value (y), an initial vertical velocity (vy), a gravity constant (gravity), and the vertical target distance it is supposed to drop (destination).

I can figure this out using a loop:

int i = 0;
while(y < destination) {
    y += vy;
    vy += gravity;
    i++;
}
return i;

The only problem with this is that I need to do this for several hundred objects and I have to do it every frame.

Is there any way to figure this out using some kind of formula? That way I can speed up my game while still figuring out this problem.

Thanks

Henry
  • 183
  • 1
  • 1
  • 4

4 Answers4

1

You can solve this explicitly using elementary physics (kinematics).

Given an initial velocity v, constant acceleration a and a fixed distance x, the time is:

(1/2)at^2 + vt - x = 0

or

at^2 + 2vt - 2x = 0

Solve that quadratic formula and take the positive time.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • Fun fact: the negative time represents how long ago the object would have passed the same point, if you had thrown it up in the air from the ground (!). Physics is cool. – Patrick87 Jul 20 '11 at 20:34
  • These are the *continuous* equations of motion, but the OP has a *discrete* simulation. So this method will be a little bit inaccurate. – Gareth Rees Jul 20 '11 at 20:35
  • 2
    In fairness, it's the simulation that's inaccurate... not physics! – Patrick87 Jul 20 '11 at 20:36
  • True! But complete accuracy is unnecessary (and often expensive) in video games, so I don't think it's unreasonable to implement a discrete simulation in the way that the OP did. ([Verlet integration](http://en.wikipedia.org/wiki/Verlet_integration) would be better than the OP's Newtonian integration, but still not exact.) – Gareth Rees Jul 20 '11 at 20:39
  • Accuracy is all academic, though, since this is probably stuff falling in Earth's atmosphere (given the constant g) yet there is no accounting for air resistance (hence terminal velocity). – Patrick87 Jul 20 '11 at 20:41
0

Check out: http://en.wikipedia.org/wiki/Equations_for_a_falling_body

dgrant
  • 1,417
  • 3
  • 16
  • 23
0

The only problem with this is that I need to do this for several hundred objects and I have to do it every frame.

I think if performance is critical for you, then a formula with lots of *s and ^s probably won't suit your situation.

I don't know what your purpose is, whether you need the simulation to be accurate or not, but I think you could check out Particle System. Although it's just a general method and it won't speed up your program directly, there are many researches going on in this field that might be useful. Also, you could read this to know more about this.

Also, since your method only use +, I believe it is quite efficient. Unless your objects are really hard to render, a few hundred won't be a problem. Most of the formulas will probably make your program looks better but does not work better. FYI, I once rendered almost 10000 particles on a pretty old machine and it worked great.

zw324
  • 26,764
  • 16
  • 85
  • 118
0

You want to know how many frames it will take to travel a distance d, given initial (vertical) velocity v and acceleration (due to gravity) a.

After n frames the distance travelled is

vn + Σ(0≤j<n) aj = vn + ½an(n−1)

So set d = vn + ½an(n−1) and solve for n:

d = vn + ½an(n−1)
∴ ½an2 + n(v − ½a) − d = 0

And then use the quadratic formula to get n:

n = (½av ± √((v − ½a)2 − 2ad)) / a


Some of the other answers have referred you to the usual solutions to Newton's equations of motion, but these only work for the continuous equations. You have a discrete simulation here, so if you want accurate rather than approximate answers then you need sums rather than integrals, as described above.

The first time I had to write this kind of prediction code was in a game involving tanks firing artillery shells at each other. To assist with aiming, the game drew a target reticule on the ground at the predicted position that the shell would land. For my first attempt I used the normal solutions to the continuous equations of motion, and the result was quite a way off. The discreteness of the simulation makes a noticeable difference to the result, and for some applications (like drawing a target reticule) agreement between the prediction and the simulation is vital.

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163