In modern games, the physics engine usually relies on an “impulse” model. In essence, it computes the impulses and forces acting on an object, either by pushing objects apart or by finding the closest point before contact and recalculating the forces. Many physics engines exist today, and all of them work with different error margins. Even in the most basic engines, the optimization problem remains unsolved: namely, fast calculation of an object's movement as it slides and collides with others using the minimum number of operations. In games, a physics engine typically recalculates the scene about 60–200 times per second. The result will be correct, but computing it costs far more CPU time. Usually people optimize the number of collision checks, simplify models by building a convex representation and apply other optimizations. So what can we apply to simplify the calculations? It is very simple: we can compute the new position of an object in fewer steps, taking into account all the forces acting on it. If an object is about to collide, then:
  • For each object, estimate the likelihood of movement based on the surrounding world. Check whether the object can keep moving and whether it is pinned down. Using the contacts, compute the resulting movement vector with the object's weight taken into account.
  • For collision calculations, take the two nearest contacts between the object and other objects, and recalculate the forces and impulses.
These two consecutive steps reduce the total number of calculations needed to move objects. As a result of the optimization, the physics engine now works correctly with just 30 scene recalculations per second instead of ~60–200. I also managed to exclude objects that cannot perform actions from the calculations and to cut the total number of computations, so objects move to the required positions in fewer operations. These changes are especially noticeable in JavaScript: while the speed gain in C++ was only 5–30%, tests in JavaScript showed a 20–80% speed increase.