- Caveats:
- Step 1 makes the simulation less accurate because you're essentially warping the object back in time (different objects will be warped back different time increments based on object velocity, penetration vector, and collision resolution order). Ideally, you should handle the collision before objects ever intersect!
- In step 2, the x and y components should be handled separately for more accurate exit velocity angles. Ideally, the exit velocity angle should be based on the normal of the collision.
As you have noted, using velocity to simulate stacked/resting objects does not work well. Velocity: velocity is the speed an object is moving, so if it is resting on a static object, velocity should be near 0. It does not make sense to increase the velocity of an object to make it appear at rest:
Most physics simulations need some type of work-around to deal with stacked/touching objectsWhat should really happen is an acceleration force that is going in the opposite direction as gravity should cancel gravity out. Otherwise, stacked/touching objects will constantly jitter/bounce because a real-time (gravityThis is called the normal contact force) simulation simply cannot be made precise enough. (In the real world, objects really are jittering/bouncing at a microscopic level dueA shortcut is to simply not apply gravity.) to bodies that are not in the air:
- In layman's terms, Newtonian physics says the total energy before and after a collision must match. When two objects crash into each other, their energy is redistributed. Energy is a combination of speed and weight: heavier, faster things have more energy. That's intuitive. However, what's not intuitive is the exact way weights affect the energy redistribution.
- Swapping velocities is a shortcut only for two dynamic, unfixed bodies that have the same mass (static, fixed objects have very large, infinite masses).
- The shortcut when one static body is fixed is: the other dynamic, unfixed body keeps the same speed; only angle is changed (imagine a pool table when a ball hits the rail. The rail essentially has a very large, infinite mass).
- For other cases, like three or more objects, the full Newtonian motion equations must be solved (conservation of momentum and conservation of kinetic energy).
- I'm not sure if the Newtonian equations for motion can be solved for more than two bodies. Fortunately, however, three objects almost never collide at the exact same time. It is sufficient to handle the first two bodies that collide, then handle any following collisions using the new velocities from the previous collision resolutions. This is a good reason to keep your physics time steps as small as possible and handle collisions before any penetrations occur.
- You'll notice in my asteroids demo many bodies are created as bigger rocks are split into smaller ones. However, I always handle collisions between pairs of bodies; never explicitly handling a collision with more than threetwo bodies.