I have recently been working on a problem that I have long wanted the time to study and solve, in order to implement better, robust, efficient and accurate simulations of balls, which is something of an obsession. Even though I have created many games where the humble ball featured as the focus of the game, the truth is that I have never implemented as yet a 100% accurate ball simulation. Actually this is a bit non trivial, if we take into account angular momentum and ground friction and air friction and so on. But quite doable. In fact in a DS game I helped with a couple of years ago, I did simulate a bowling ball with reasonably correct physics, so I am familiar with all the ingredients, but have as yet never put them all together to create the most accurate simulation of a football possible. The thing is that in the early games there really was not the computational power to do such things, but it is a different matter now. In spite of this, Kick Off did actually have air friction on the ball, using the simple Euler integrator (although I had no idea it was called that at the time). It sounds impressive until I tell you it only took a few of lines of assembler to implement!

What I am going to focus on here is an attempt to get a prefect integrator for a ball under gravity and influenced by air friction. The case without air friction is pretty trivial, so I’ll start with that.

### Calculus

This is something of a difficult branch of mathematics, which I believe every video game programmer should have a basic understanding of. If you are a game programmer and don’t think you have a grasp of calculus, please don’t be offended. Most video game programmers create a Euler integrator even if they do not know what it is called. But in principle, the only thing you need to know is that if you have a function that gives the acceleration at time t, integrating it gives you the velocity at time t. Integrating that gives you the position at time t. But first let’s look at how most programmers would create ballistic behavior. Here I only concern myself with the vertical motion of the ball.

float y=10; // height of ball

float vy=2; // velocity of ball

float g=-0.5; // gravity

EachGameLoop do {

vy+=g; // sum acceleration over time

y+=vy; // sum velocity over time

updateBallHeight(y);

}

Looking at the above, it is all pretty straight forward. A most natural piece of simple code that gives realistic motion, without air friction. In this sample, there two Euler integrators. Yes that’s all a Euler integrator is, addition.

I am not going to go into great detail on the subject of calculus, but I will point out a couple of things. First of all, the above code is not precise (compared to the mathematical truth of an object under constant gravity in a vacuum). This is not because of floating point rounding errors, but because Euler integration is an approximation when the amount you are adding varies over time. To get a perfect result you would need to use infinitely small time steps, and this mathematical problem was understood hundreds of years ago, and efforts to solve it resulted in calculus. The second thing is that calculus allows you to create a formula that will give you the position of the ball at a time directly without having to add lots of steps together (and this is something that may be important to you if you are concerned with performance). You could just ask “Where will the ball be in 5 seconds time?” and get the answer back in one step, rather than having to do 250 integration steps.

So how do we get this formula? Well we simply start with a function for the acceleration at time t, which is of course a constant:

No matter the time, the acceleration is the same. Now we want a formula for the velocity at time t. There really is no need for calculus at this point: pretty intuitively you should see that this formula will be an initial velocity plus the acceleration multiplied by the time, or:

Finally we want a formula for the position at time t, and to get this we do need calculus. Again I will not go into the details here, instead I will just give you the mathematical answer:

When you integrate, there is the need to add a constant, because the answer is a set of functions. This is actually quite reasonable. When you integrate velocity to give position, you have to add the initial position, just as we had to add the initial velocity when integrating acceleration (remember?). So C is simply the initial position:

This formula will give us any future (or past!) position of the ball. Very nice. Here is a plot of the function, height of ball against time, for an initial height of 10, initial velocity of 20, and a gravity of -2:

## Leave a Reply