It'd be great to improve the current physics engine, which is currently mostly ad hoc. This would allow us to have features such as grappling hooks and tractor beams.

This document specifies some of the implementation details for such a change.

Core Loop

Today the core animation loop looks like this:

while (StillPlaying())
   {
   PaintFrame();
   UpdateObjects();
   MoveObjects();
   }

Right now, collision-detection happens during MoveObjects but damage-resolution happens in UpdateObjects.

For example, in frame n we detect that a laser beam hit a ship at the bottom of the loop. We store store that fact in the objects themselves.

On frame n+1, we paint the laser beam just touching the ship. Then, in UpdateObjects we resolve damage and destroy the ship and beam (creating an explosion in its place).

On frame n+2, both ship and beam are gone (not painted).

While this loop works fine, the main problem is that we need to remember the result of a collision across frames. Since a game could be saved at any point, we effectively have to save collisions persistently, and deal with object lifetime.

A better system is as follows:

while (StillPlaying())
   {
   PaintFrame();
   UpdateBehaviors();
   UpdatePhysics();
   }

The principal change here is that we move damage-resolution to the UpdatePhysics phase (what used to be MoveObjects). Essentially, we divide the update in two:

  • UpdateBehaviors runs the AI and systems (e.g., triggers weapon fire, etc.).
  • UpdatePhysics updates all consequences (including collisions, damage, etc.).

Our example will now look like this: On frame n we detect that a laser beam hit a ship at the bottom of the loop (in UpdatePhysics). We resolve the damage and determine that the ship has been destroyed. We create an explosion and mark both the ship and beam as having been hit and about to be destroyed (but we don't remove them yet).

On frame n+1 we paint the laser beam just touching the ship, then in UpdateObjects we destroy the ship and beam.

On frame n+2, both ship and beam are gone (not painted).

Collision Detection and Constraints

Once we've modified the main loop as above, we can concentrate of improving the UpdatePhysics phase.

In this phase we need to do the following:

  • Apply forces to all objects (e.g., gravity, springs, etc.).
  • Update motion.
  • Detect collisions and constraint violations. For example, if two objects connected by a rod have moved apart, we need to detect that and correct.
  • Resolve position of objects to satisfy constraints (e.g., bounce off walls).
  • Resolve the effects of collisions (e.g., damaged by a missile).
nms 30 Nov 2016:

Applying all the forces at once (after weapon fire) should resolve Recoil affects projectile speed when firing a primary and several linked-fire weapons.

Also, while you're at it, maybe you could look into Most, but not all, instances of effects are not visible the tick they're created