I've seen box2d and bullet ported into JavaScript, but neither of them attracted me, except for the source code. It all seemed quite simple, once I looked inside.

What are they doing within each of those libraries to get nice physics simulation? I can not find any explanation.

  • \$\begingroup\$ I'll eventually answer the question myself because I am studying the sources. If you manage to shorten the effort I'll appreciate it. \$\endgroup\$
    – Cheery
    Jul 27 '10 at 0:05

Rigid body physics is actually quite simple in concept - that is, the behaviors an engine is trying to create are not very complicated. That's probably why the libraries you looked at seem quite simple to you.

The trick is that making a sim that is robust, stable, and fast, even in the presence of things like floating-point error and low, variable framerates, is quite difficult and usually involves a great deal of tweaking and fiddling. Further complicating the issue is the fact that a lot of extremely smart people have made heroic efforts at finding better solutions, and have come up with beautiful and powerful but difficult-to-understand algorithms that have given the field a reputation for black magic.

In general, a physics engine is going to be concerned with three things (the order in which they do them in the internal loop varies):

  1. Integration (approximating the Newtonian equations of motion),
  2. Collision detection, and
  3. Constraint solving (updating positions and velocities to satisfy nonpenetration or other user-specified constraints.)

For most of these, there are both simple but not-so-accurate algorithms and complex but more-accurate algorithms. To get you started:

  1. For integration, most people use either Symplectic Euler or Verlet (which are actually equivalent. You can read up on numerical integration if you like, but the consensus seems to be that cheap first-order methods are fine.
  2. You can find all the collision detection references you could want on the Real-Time Rendering resource page.
  3. For constraint solving, the method of sequential impulses is very easy to understand. Erin Catto (the author of Box2D) gives a presentation at GDC every year - the slides are linked from the Box2D main page. The 2006 slides cover sequential impulses pretty well. You can also look into fancier methods like Featherstone or projected Gauss-Seidel.

There's an excellent introduction to simple methods and what can go wrong in the early chapters of the just-published book Game Physics Pearls.


http://en.wikipedia.org/wiki/Rigid_body_dynamics might be a good place to start. Different games will divide things up into different bodies, but in general you have a number of discrete rigid bodies linked by joints with specific properties. Each loop through the game you calculate the forces on each body and calculate its new parameters (position, velocity, etc). You also have to transmit forces from each body to the others it shares joints with and factor in global forces (gravity).

This doesn't cover things like liquid or cloth physics, those are all gibberish to me. There are differential equations involved and thats about the point where my eyes glaze over.

  • \$\begingroup\$ Well.. wikipedia would be the first obvious place to look things from. Also everybody this far probably know that they tend to simulate body motions through forces. There's much more in box2d than that. Here's few things I'm interested about: Constraint solving and some other stuff that prevents simulation from exploding. Collision detection algorithms. Optimizations on constraint solving and collision detection. Joint/Collision response algorithms. Simulation of the friction. \$\endgroup\$
    – Cheery
    Jul 27 '10 at 0:02
  • \$\begingroup\$ Thats verging into "too long for a real answer" territory. For example, damping to prevent simulation artifacts is a complex bit of math. As for things like joint force transfer, the simple version is that each joint has specific axes it will transfer force on with a percentage of the body's energy counting towards the join depending on the angle between the two. The simplest joint is a fixed link, where all force from either side is translated across. \$\endgroup\$
    – coderanger
    Jul 27 '10 at 0:27
  • \$\begingroup\$ Like I said in the question: It's all right to just tell there's complex math here and there and provide a link for further reading. \$\endgroup\$
    – Cheery
    Jul 27 '10 at 11:03
  • \$\begingroup\$ Well, there you go then :-) \$\endgroup\$
    – coderanger
    Jul 27 '10 at 15:32

One important thing besides the physics equations is tracking all the objects, so that checking for collision detection and interactions is as fast as possible. Spatial Hashing is the technique that the Chipmunk physics engine uses.


Not the answer you're looking for? Browse other questions tagged or ask your own question.