Derivatives and differentiation
Introduction
Derivatives are used extensively in games, even though many people don’t realize that. When you are calculating the difference between this and last position, because you want to align an object along the path it follows, you are actually trying to calculate the derivative at the current position (Note that if you use physics with an integrator, you can just use the velocity. We are talking about the case where an analytical function is used instead).
The problem is that calculating the direction this way will depend a lot on the elapsed time. If dt is too large, you might be off a by a lot, and if dt is too small, your vector may become undefined.
A better way is to calculate the derivative of the function you use to decide the trajectory. See for example the following ballistics function:
This function calculates the position of a projectile given the initial position , the initial velocity and the gravity vector, which is most likely zero in the x dimension, e.g. . So how do we get the direction of the trajectory at a time t? By calculating the velocity, which is the derivative of the position.
This function will give us the velocity vector at each time t. If we take the angle of this vector, we can rotate our sprite in order to align it with the followed trajectory.
Local minimum, maximum and inflection points
What if we want to know how high our projectile will go? Let’s say we have an enemy in a cave system throwing stones. These stones follow the same kind of ballistic trajectory which we discussed before. The enemy is throwing the stones at random angles, but we don’t want it to hit the ceiling. How do we get the highest point of the trajectory?
A local maximum or minimum of a function is a point on the function where the derivative is 0. This is logical as with a local minimum you go up (positive), reach the top, and go down (negative), so at the top your derivative is 0. So what if we set our derivative to 0? Note that we only look at the y output of our function
This happens when
Using this t in our original function gives the position where we reach a maximum.
We see that this happens at
So the maximum position of our projectile for a given position and velocity is
We can now say, given an angle, how high the projectile will go, and only shoot when it won’t hit the ceiling. But we can do better than that, since we know that
we can get the velocity in function of the height
So we know our maximum y velocity given the height of our ceiling.
The ballistics case is special since the function has no local minimum and no inflection point. If our analytical function has minima or inflection points, we need to look at the sign of the second derivative. If it is negative, our point is a maximum, if it is positive a minimum, and if it is zero, we have found an inflection point. The second derivative of our ballistics function is
Thus whether we found a minimum, maximum or inflection point is completely dependent on the sign or value of g.
Taylor series
Sometimes there are functions which we can’t calculate directly. Let’s say you need to calculate a cosine, but there is no cosine function. You only have mathematical operators (or a non-scientific calculator).
With derivatives, you can approximate the function around a given point. This approximation is a polynomial and is called a Taylor series. Given a function f, its Taylor series is
To give an example, let’s take f(x) = cos(x). We know that its derivatives are
So our Taylor series for cosine is
For cos(x) around 0 (so a = 0) we get
Since we know cos() = 1 and sin(0) = 0 we get
Cleaning this up we get an approximation for cosine around 0
This is how trigonometric, logarithmic and other functions can be converted into polynomials which can then be calculated using basic operators. This is how also how libraries implement them.
Calculating derivatives
In case you don’t remember how to calculate the derivative of a function, here is a short guide. For a constant function, the derivative is 0.
The derivative of a sum is the sum of the derivatives
The derivative of a product is
The derivative of a nested function is
Out of the product rule, we can derive the rule for polynomials. This is the rule which you’ll use most of the time, as anything from ballistics to general splines are polynomials
Out of the nested function rule, we can derive the rule for
From this we can get the quotient rule
For the two most used trigonometric functions we have
and
For logarithmic functions
and
For exponential functions
and
Partial derivatives
Sometimes we have functions with more than one input. In that case we can calculate partial integrals, which are integrals in one of the dimensions of the function.
For example the function
Has a partial derivative in x equal to
This function gives the rate of change in the x dimension at every point .
Integration
Introduction
Integration is the opposite of differentiation. While differentiation looks for the rate of change of a give function, integration looks for a function given a rate of change.
Do we use that in games? Yes we do. Every physics simulation uses it. When we use simple Euler integration, we have
We integrate twice here, we calculate our new velocity from the rate of change of velocity, acceleration. Then we calculate our new position from the rate of change of position, velocity. Our velocity at a certain time is thus a sum of accelerations at certain points of time over a certain time span.
Same for position
These are Riemann sums. You can see them as rectangles with as height f(x) and width dt. Thus the result will be better the smaller dt is, as many thin boxes can approximate the function better than fewer larger boxes. This is why the KAPLAY physics engine runs at a fixed framerate. If it would run at the display framerate, results would be different between different devices. More performant devices would obtain better approximations for the same integral.
There are two kinds of integrals. What we just described is a definite integral. A calculation of the signed surface area under the function. An indefinite integral is a function called the anti-derivative.
Indefinite integrals
If you know how to find the derivative of a function, you can find the anti-derivative of a function. What you need to look out for though is constants. The derivative of a constant was zero, this means that there are many anti-derivatives possible for the same function. For example both
derive to , thus any function , with c a constant is an anti-derivative of .
We can use integrals to calculate areas of various surfaces, water volumes or obtain a function when we know the rate of change of a certain value.