Computer games look extremely pretty these days. Find out how a decent understanding of physics makes it possible to implement "realistic" computer games!
Emil sits at his desk. He is quiet and concentrated. Soon he will fight the most important battle of his life. We're in the year 2001 and it's time to claim the world championship... It was a long journey until computer games became popular and Emil Christensen could be one of the world best so-called cyberathletes. The reason is simple: a lot of technologies had to be invented to make this kind of entertainment possible from the hardware side - electricity, semiconducters and so on. But what about software and their algorithms:
How is physics realized in games?
At its core, a computer can only do very limited things like addition and multiplication. The challenge is to find a representation of physical theories in terms of these operations. Luckily, high-level programming languages are available such that we can use real numbers, vectors and may define functions acting on such objects. It is for example possible to implement Maxwell's equations. The drawback is that an electromagnetic simulation of, say, a cube of 10µm edge length for a wavelength of 500nm would take almost forever on single gaming computer for interesting simulation times. So we cannot put our whole knowledge about physics into a game engine - game physics is an art of approximation as we will see shortly.
Game physics in action - Formula 1 2012 (© Codemasters) and Star Fox (© Nintendo). Some things have changed over time... slightly :)
Game Physics - the Art of Approximation
Before we talk about the actual simplifications that are used, we can think about the different branches of physics we will need to implement. First of all, we will consider methods to approximate actual body movement in terms of classical mechanics. After that we shall focus the use of electrodynamics for visual effects.
Movement Dynamics - Classical Mechanics
The simulation of moving bodies is probably the most extensive task and makes up for most of the part in game physics. The movement of bodies may be roughly divided into three different levels of approximations depending on the properties of the body itself:
- rigid body dynamics - solids
- elastic body dynamics - soft bodies
- particle system dynamics - fluids
Each of the different parts brings its own implementation difficulties. One might think that the movement of rigid bodies is per-se an easy task. It is not - a freely moving body is easily described, but what happens if a rigid body collides with another solid body, or an elastic body! One approach is to approximate the bodies again as basic objects like spheres and cubes and use conservation of impulse and energy. This is described in a nice talk on game physics collision detection by Alan Hazelden; in-depth information can be found in the PhD thesis of Kenny Erleben who also programmed the OpenTissue engine.
Elastic body dynamics can be sub-divided into really three-dimension objects (footballs, rubbers), surfaces (clothes) and one-dimensional ones (ropes, chains). Then, the linear deformation of such bodies can be modeled in terms of harmonic oscillators; such approaches are called mass/spring models. Also more advanced schemes like finite element simulations can be used if the body has an anisotropic stress tensor.
Fluids and for example fire is simulated using a lot of particles within particle dynamics. The particles are chosen to be that so small that it looks like a continuum. Laws of interaction are formulated such that that the overall motion looks natural and is not too computationally expensive. The simplification to use actual particles is extremely helpful: the equations coverning fluids for instance are the Navier-Stokes equations - terribly complicated and hard to handle numerically.
If you want to try out some game physics engines on your own, here are some suggestions:
- State-of-the art is Nvidia's PhysX capable of calculating the game physics on a graphics card and used in a lot of games
- the open dynamics engine for solid-body interactions written by Russell Smith until 2007. The engine is available at sourceforge and very slim (1.8MB)
- OpenCloth for simulations of surfaces
- dynamic particle simulations can be nicely accomplished by the dynamic particle system framework DPSF or the SPARK particle engine
We have discussed the implementation of classical mechanics for video games so far. But of course this site is about electromagnetism. So, how is it possible that modern computer games look so damn realistic and run at considerable frames per second?
Visual Effects - Electrodynamics
It might be safe to say that most of the structures in games are much bigger than wavelengths of visible light (roughly 400nm - 800nm). Then, it is safe to say that if hits an object, all it “sees” is a flat surface. That surface is defined where one material, e.g. air changes to another one, e.g. glass. Then, all we have to know to solve for reflected and transmitted light are the Fresnel equations. These equations are strictly valid for plane wave scattering at a planar boundary.
However, any structure is much bigger than the wavelength (again), so we can use ray optics. If we combine the Fresnel equations with ray optics, we end up with a procedure called ray tracing: a light source is approximated with a number of emitting rays and the scattered light at the position of the player is calculated.
Ray tracing is an extremely powerful method suitable to make ultra-realistic images, see picture on the right made with POV-Ray, modified from its wikipedia entry. So, can we use ray tracing for games like Daniel Pohl who "ray-tracyfied" Quake IV? Daniel is now working for Intel and stated that he has achieved roughly 20 frames per second on a fairly impressive system. Also John Carmack, founder of id Software, said that he is“confident in saying now that ray tracing of some form will eventually win because there are too many things that we’ve suffered with rasterization”. By the way, he also elaborates on the importance of physics and mathematics education in the interview :)
So there is an obvious performance problem with ray tracing: rendering took about 15 hours for a single frame of the movie "Cars" as Jeff Atwood explains. Within the limitations of ray optics, ray tracing supplies correct reflections and refraction. This is nice but as we have seen, the actual implementation is rather computationally demanding. Furthermore, it is very rare that one can take advantage of such a description - most relevant objects in games are opaque for visible light! Also, we do not need an absolute accurate description to think what we see is real. This means we can potentially further approximate the electromagnetic field for games! This brings us to the next point: rasterization.
Rasterization is the standard rendering method for current games. So, what is it? A raster engine divides the surface of objects into triangles which is called meshing. Now it analyzes which of the triangles are visible in the current perspective. Then, the engine calculates the scattered light only on those triangles by the light sources. The reflections on each triangle are calculated with more simplified formulas than the Fresnel equations. One of the tools that uses rasterization in a lot of games is the Open Graphics Library (OpenGL). In older video games you could still sometimes see the polygons, like in the legendary StarFox for the SNES (1993). However, several techniques as antialiasing make the impression extremely realistic as we have seen in the first image.
You can try out the following renderers which are also very suitable to make nice figures for scientific articles:
- POV-Ray: script-based and easy to learn
- Blender: Not so easy to learn but an all-in-wonder tool. Supports different render algorithms and can also handle mechanical game physics calculations. Neat: you can make videos, here's an example.
All in all, mechanics and electrodynamics are at the very core of game physics:
The different levels of approximation used in games - mechanics and electrodynamics.
“Its the Frames per Seconds, stupid!”
We have seen how electrodynamics is really implemented in games - the only thing left from Maxwell's equations are reflecting triangles...! Game physics is all about performance - we cannot just take equations and implement them. We have to think about suitable and sometimes crucial approximations that cover the basic features. But of course, this is like real research! Remember Einstein's famous
“Everything should be as simple as it can be, but not simpler”
which certainly holds for problems in current research. To conclude, game physics is a science on its own. Engineers, physicists and mathematicians have worked together for a long time to provide us fast and working tools, algorithms, and, last but not least, a lot of pleasure playing realistic video games with breathtaking graphics. I want to thank all these contributors and hope you enjoyed reading!
About the author: Robert Filter loves physics and games of all kinds.