18

This question may be a stupid question, but I'm really curious.

After playing games like HL2, GMod, or Angry Bird, and using physics libraries like Box2D, I began to wonder "how a physics engine simulates physics?"

Like lexer and parser are used to understand the code when compiling and ray-tracing is used to render a 3D scene, I think that there's some notions (other than collision detection) which are used in the physics engine to simulate physics, like calculating torque and velocity of a pentagon performing barrel roll.

How does a physics engine actually simulate physics? What notions are used? Is there any 'tutorial' on the web about making physics engine like this one, which demonstrates ray tracing?

theJollySin
  • 5,687
  • 6
  • 39
  • 55
JiminP
  • 2,036
  • 19
  • 26

3 Answers3

21

Creating a (robust) physics engine is a lot more complicated then it may seem at first. The trick is to fake as much as possible rather than calculate the exact values. As a starting point, this blog post has a great introduction. I think this paper by Thomas Jakobsen is also a good read and introduces certain concepts. This blog also has a few interesting articles explaining the details of integrators and how to manage physics for online games.

Going through the source code of physics engines such as Box2D is a good idea to see implementation, but if you don't know the theory of what they're doing, it can come across as confusing. The reason for this is because of the fact that the theory is often too inefficient to implement in a real time game and so algorithms and techniques are used to strike a balance between realism and speed.

If you're creating your own physics engine because you want to use it in a commercial game, I'd suggest choosing an already existing solution instead. (For example, Angry Birds uses Box2D). However, if you're doing it for the experience and for learning about physics engines, it's certainly something that'll teach you quite a bit about efficiency and smart techniques.

keyboardP
  • 66,755
  • 13
  • 145
  • 199
13

In principle, all physics engines are simply a direct application of Newton's second law of motion:

acceleration = force / mass

By integrating acceleration over time, you get velocity. By integrating velocity, you get the positions of the object in space. The integrals are performed numerically, using something like Runge-Kutta.

The main complications arise from:

  • Handling rotational motion
  • Handling impulse events, such as collisions and explosions
  • Detecting collisions at the extreme ends of velocity. At high speeds, objects can end up penetrating or even passing through each other before the collision is detected. At low speeds, it is a challenge to robustly handle one object resting on another without jittering or sliding around
  • Kinematic linkages, where objects are partially constrained to each other (like a hinge or slider)
  • Calculating the forces and torques for complex objects such as cars or aircraft
  • Doing all this efficiently in real time for hundreds or thousands of objects

A good place to start is to simulate a single particle bouncing around in a 2-dimensional box under the influence of gravity. Then, give the particle a radius, add more of them, and calculate collisions between them. At that point, you have a basic physics engine that's enough to use for simple games.

Theran
  • 3,706
  • 17
  • 31
1

How about looking through the source of one. That's where I would start.

http://www.tokamakphysics.com/

Marlon
  • 328
  • 4
  • 12
  • Thanks, but since I'm not a good programmer, I can't figure out what algorithm they used... :( From the code, I found that the general step is `detect collision - solve the "physics problem" - adjust`, but I can't figure out how exactly solver.cpp calculates physics... :( – JiminP Jun 10 '11 at 03:02