## A ball with air friction (part 2)

In part one, I showed you the basic calculus required to get a formula for the height of a ball at time t, without air friction. Before I go on to adding friction, I’ll just cover one thing that comes up commonly: figuring out where the ball will land. Of course, without air friction this is easy. If you are a math-o-phobic as so many people are (I blame the education system myself) you might just remember the ABC formula. Yes, it is actually useful as you shall see in a minute.

In order to find the landing position, we need to first find the time of landing (or time of collision). Clearly, if we can find this we can find the position of the ball at the collision. The height will be known, of course: it will be zero. However we would have equivalent formulae for the horizontal location of the ball at time t too, so knowing the time we can find the location of the collision.

To find the time of collision, we are really asking for the time that the ball reaches a certain height. For simplicity, let’s pretend the ball is infinitely small, so it will collide with the ground at a height of zero (to take into account the size of the ball, an adjustment of the starting height (p0) is all that is needed). We need to solve this equation:

$t\,{v}_{0}+\frac{g\,{t}^{2}}{2}+{p}_{0}=0$

Solving any equation of this type can be done using the ABC forumla:

$a\,{x}^{2}+b\,x+c=0$

$[x=-\frac{\sqrt{{b}^{2}-4\,a\,c}+b}{2\,a},x=\frac{\sqrt{{b}^{2}-4\,a\,c}-b}{2\,a}]$

In our case, a is one half of the acceleration, b is the initial velocity and c is the initial position. Note that there are two answers: this is because the curve is a parabola and can cross the x axis in two places. In our case, one answer is a future solution (when the ball will hit the ground) while the other is a past solution (when the ball would have had to be launched from the ground to arrive at the starting point). Although you could calculate both and choose the future time by comparison, a little bit of simple analysis of the function can prove you only need to use one of them. I’ll leave that as an exercise for you.

So, our formula for the time of collision is:

$\frac{-\sqrt{{v0}^{2}-2\,g\,p0}-v0}{g}$

Let’s look at a graph of an example case, with a starting height of 10, a starting velocity of 20 and a gravity of -2:

If you put the test case example into the formula to calculate the time of impact, you get about 20.49. Marvelous.

### Air Friction

It may come a a surprise, but air friction is not a luxury in a football game. Without it, you get wrong kind of ball motion; either you have to kick the ball softly all the time so that it does not go out of play, or you allow the ball to be kicked hard and then fake the bounces to keep the ball in play (as seen in many versions of Pro Evolution Soccer).  It is also necessary if you want to simulate wind (without air friction, wind has no effect ). I implemented air friction in Kick Off, and doing so was actually quite easy, it goes something like this:

float y=10; // height of ball
float vy=2; // velocity of ball
float g=-0.5; // gravity
EachGameLoop do {
vy+=g-vy*k;  // sum acceleration over time
y+=vy;  // sum velocity over time
updateBallHeight(y);
}

Notice the vy*k term. k in this case is a constant that represents the effect of air friction (and this depends on the shape of the ball and its mass… a heavier ball is not effected by air friction as much. What happens is that the ball is slowed down by an amount proportional to the speed of the ball. In reality, using the square of the speed is more realistic, but at slow speeds (such as that of a football) the difference is minimal, and not something anyone is likely to notice. Here is a plot of what you get with an initial velocity of 20, an initial position of 10, gravity of -2 and k=0.3:

As you can see, the ball quickly reaches a terminal velocity and falls to the ground at a constant speed. Very nice. But once again, this is only an approximation, because we are using Euler integration. The actual path depends on the time step, and to get the true result we need infinitely small time steps. So, the obvious thing to ask here is whether a formula exists that will give the position of the ball at time t for this case, with air friction. The reality is that even just getting to this relatively simple level of realism for the mathematics of a ball requires solving a differential equation. Which will be the subject of part 3 of this series. Don’t worry, I’ll show you how it is done using math software packages such as maxima, mathcad and mathematica (or even just Wolfram Alpha!). It won’t hurt at all, I promise!