Ok, sorry, I was trying avoid any actual mathematics, and just show the process. So yes, I'm sure the maths is highly confusing. I'm afraid on this board I cannot see how to do mathematical notation, but I'll try harder to make things clear. I'll try the lesson again, but I cannot honestly say I'm very good at this. This time I'll go heavy on the maths, to the point you find me patronizing. There's a balance somewhere, but damned if I can find it.
There's actually several things to appreciate here. The first is that a common way of describing paths is by parametric equations, i.e. a function that maps from t, time, to position. Or equivalently a function that maps to x position and a second function for y, and a third for z.
The second thing to realize is that a popular choice of function is quadratic equations, or more generally polynomials. Other choices would be cubic equations, or sin waves, or more complicated things, but as we'll see quadratics are physically justified.
Finally, having decided on the family of functions (the quadratic polynomials), we need to pick the exact one we want based on the results we want. I'll go over this last part again.
We can characterize any quadratic with 3 co-efficients, which I will call
a,b,c.
In maths, we'd write:
Or we could use code (I hope you like function closures):
def makeQuadratic(a,b,c):
def myPoly(t):
return a*t*t + b*t + c
return myPoly
which we could use like so.
quadratic1 = makeQuadratic(0,0,1)
print quadratic1(0) # 1
print quadratic1(5) # 1
quadratic2 = makeQuadratic(1,0,0)
print quadratic2(0) # 0
print quadratic2(5) # 25
quadratic1 represents the function
x(t) = 1, i.e. a constant, while
quadratic2 would be the function
x(t) = t*t. Note I still call
quadratic1 a quadratic, even though it involves no powers of
t at all. It's just degenerate, but that doesn't stop it being a quadratic.
Ok, that's cool, we can form a function from knowing a,b,c, but what we really want to do is go backwards, based on things we know about the function. So we form equations, and solve them simultaneously.
We know 3 things about our function, which we are going to call
f.
- f(0) = startZ
- f(1) = endZ
- The second derivative, written f''(t) is a constant function, of value gravity.
The first two are requirements that we start and end where we want, at the times we want. The third is less obvious, but it derives from physics. The second derivative of position is more commonly called acceleration, and we all know earth gravity causes constant acceleration near the surface, right? In fact, knowing that the second derivative is constant
requires f to be a quadratic. We didn't need pre-suppose it. But I'm not going to get into that.
So now we expand out those 3 known things into statements about
a,b,c. The first two are simple substitution:
a*0*0 + b*0 + c = startZa*1*1 + b+1 + c = endZThe next one requires calculus. If
f(t) = a*t*t + b*t + cf'(t) = 2*a*t + bf''(t) = 2*aOh, good, it is a constant function like we said. So we know that
2*a = gravity.
Ok, we've got three equations, for three unknowns. Time to solve. Turns out, it's not very difficult. If we simplify the above, we get
c = startZa + b + c = endZ2*a = gravitySo we can read off what
c and
a are.
c = startZa = gravity/2Plugging in to middle equation:
(gravity/2) + b + startZ = endZi.e.
b = startZ - endZ - gravity/2Now we are done. We could make our quadratic function with
makeQuadratic(gravity/2, startZ - endZ - gravity/2, startZ)or simply just write it out:
f(t) = gravity/2*t*t + (startZ - endZ - gravity/2)*t + startZBut either way, we have successfully found a function that describes the motion we want.
NB: I did not do the above in order to produce my original solution, so don't try to reconcile the two.
NBB: This is a highly mathematical way of dealing with things. You have all encountered quadratic beziers before, but expressed in the form of two anchor points and a control point (ever edited a curve in Flash?). It's a more convenient form for a lot of work, and generalizes very nicely to bezier curves, but I didn't want to use it as it confuses the equations, and it's not quite as clear how gravity fits into the whole thing. Perhaps another day.