Physics Quickie: Interpolation over time. Following the bouncing ball.
All of the equations are done, but it seems applying them may still be confusing. Check out the previously defined equations and then come back here to see how they can be quickly and easily applied in a simulation Physics Installment #1: On the road to collisions. The first problem that people seem to run into is the translation of units to screen space. Rendering the output of a simulation with respect to some drawing coordinate space can actually be fairly daunting, meaning that laying down some ground rules is going to be important. Let's start by using the equations to give us a good grounding.
How long do we want the ball to bounce?
time = 4s
distance = 0.5at^2
distance = 0.5(9.8m/s^2)(4s^2)
distance = 78.4m
pixelPerMeter = graphHeight / distance
As you can see if we want the ball to initially take 4 seconds to drop from the top of our graph to the bottom we have to account for it travelling 78.4 meters. Each meter traveled can be turned into a distance in pixels and whenever we come up with a meter distance we'll have a way to quickly plot a pixel distance. With this in mind we can set up the simulation with respect to our screen space. Since screen real-estate is always going to be fixed, no matter how we change the parameters of the simulation with respect to initial height, amount of time the ball should bounce, etc.., we'll always have to do a meter to pixels conversion at some point.
How do we find the location of the ball at any time? How do we incrementally calculate the value?
distance = 0.5(9.8m/s^2)(t^2)
t1 = .25s, t2 = .7s, t3 = .3s
v0 = 0
v1 = 9.8m/s^2 * t1
v2 = v1 + 9.8m/s^2 * t2
v3 = v2 + 9.8m/s^2 * t3
d1 = (v0)(t1) + (v1 - v0)(t1)
d2 = d1 + (v1)(t2) + (v2 - v1)(t2)
d3 = d2 + (v2)(t3) + (v3 - v2)(t3)
; 1/2at2
d = (0.5)(9.8)(.25^2) = 0.30625
d = (0.5)(9.8)(.95^2) = 4.42225
d = (0.5)(9.8)(1.25^2) = 7.65625
; Initial velocity form or time-step form if you will
d1 = (0)(.25) + (0.5)(2.45 - 0)(.25) = 0.30625
d2 = 0.30625 + (2.45)(0.7) + (0.5)(9.31 - 2.45)(.7)
= 0.30625 + 1.715 + 2.401
= 4.42225
d3 = 4.42225 + (9.31)(0.3) + (0.5)(12.25 - 9.31)(.3)
= 4.42225 + 2.793 + 0.441
= 7.65625
The bouncing ball jumps out of the above equations. If you are using a short enough time-step, you won't have to worry about the ball ever going *below* the surface of rebound. If you are using longer time-steps then you might just overshoot the surface of rebound if you aren't being careful. If that is the case, you need to find out at what time we impacted the surface, the velocity of rebound, and how far we've travelled away from the surface as a result. The collision is critical and we have to make sure it gets handled even if it happens in between our calculations.
How do we discover the moment of impact (using our original distance)?
t = sqrt(2ad)/a
t = sqrt(2(9.8)(78.4)) / 9.8
t = 4
Once you know the moment of impact, you can divide out your time-slice. If the previous time-slice put you at 3.8 and the next timeslice would put you at 4.2, then you either need to compute the physics for an offset of .2 followed by .2, or take a shortcut since you already know what the values should be during the collision and then simply compute the .2 part of your simulation starting with these known values. You an discover the velocity at impact because you have time and acceleration. You can use some sort of CoR value on the velocity and then reverse the sign in order that the ball travels in the reverse direction. Once you've reversed the direction you'll simply continue running the equations until the velocity falls to zero and start all over again.
Is the number 4 significant in any way? Well, you can use some shortcuts... What happens if we add 4, 3, 2, and 1? Well we get 10... Now split the pixel distance by 10... You now have a good gradient or set of bands that you can use. Rather than interpolate exactly based on time, you can use an incremental measure... This is actually a fairly common approach, but with processor speeds, using the physics isn't going to hurt your perf in the slightest.