An Easy Way Of Solving Complex Mathematical Models: The Finite Difference Scheme
June 18, 2008 Page 1 of 3
[In this technical article, veteran coder Jovanovic looks at dynamic game-related effects like heat distribution models, explaining useful mathematical formulas that should help decrease the amount of programming.]
In games today, more and more realistic effects are expected, and the best way to get them is by using the correct physical models. The problem many of these models share is that they are represented with partial differential equations (PDE).
The solving of this type of equation is often avoided by using different types of tricks. It is sad to say, but in the end tricks are just tricks, and they don't give the correct solution - just something that looks like it.
There are two main reasons for using tricks. The first one is that they are usually much faster - but with the increase of CPU power we can give up some of the speed to get more reality. The second reason is that most people think solving PDEs is very complicated, and in many cases this simply isn't true.
In this article we'll show a standard method of solving PDEs and how to implement it. This method is called Finite Difference Scheme (FDS). It is not the most exact and stable method that exists, but is much better than using tricks, and it is the easiest to understand and implement.
A game example of the use of PDE
Let us try to show the advantages of using a mathematical model with PDE and the FDS method in a game. Let's say we wish to have a trampoline, or something like a rubber sheet that represents a wall that moves with the wind. How would it be implemented in a game? The approach in games today is to use a spring system.
Anyone who programmed spring systems knows this isn't the simplest task in the world. We have to create classes for nodes, springs, the hole system; and than calculate the forces from all the springs, and than apply the sum of forces corresponding to each node. And after all that work we get something that can often become numerically unstable.
Spring systems are actually great and very complex objects can be modeled with them, but in this case we don't need them. There is a much easier way.
The behavior of a trampoline, or the vibrating membrane, is one of the standard physical problems and one of the basic PDE. The equation that represents it looks something like this:
When someone sees something like this the natural reaction is "I'll stick with spring systems, since it's surely much easier." This is wrong for a couple reasons.
First, the implementation will be simpler; less programming will be needed. Second, the final result will look better because we used a better mathematical model, or in other words we modeled a trampoline and not just a bunch of nodes connected with springs. Third, there will be less calculation which means the code will work faster.
Page 1 of 3