4 ms·
You want a symplectic integrator (https://en.m.wikipedia.org/wiki/Symplectic_integrator https://en.m.wikipedia.org/wiki/Symplectic_integrator) such as Verlet In
by comicjk 8y ago
You want a symplectic integrator (https://en.m.wikipedia.org/wiki/Symplectic_integrator https://en.m.wikipedia.org/wiki/Symplectic_integrator) such as Verlet Integration (https://en.m.wikipedia.org/wiki/Verlet_integration https://en.m.wikipedia.org/wiki/Verlet_integration). Such integrators naturally conserve energy and momentum as long as your forces and energies are self-consistent and you don't use gigantic timesteps.
A classic mistake is to use something like "velocity += acceleration*time" (this is called Euler integration). It looks reasonable, and it's good enough for a toy project, but it doesn't conserve energy unless the timesteps are infinitely small.
A more sophisticated mistake is to use something like Runge-Kutta Integration: highly accurate in terms of position, but it is not symplectic so the total energy will drift over time. Think of your simulated world as a stack of graph paper sheets, where each sheet represents a surface of constant energy. Runge-Kutta will take you very close to the ideal (x,y) point - but not necessarily on the same sheet. Verlet Integration may be a little further from the right point each time, but by its mathematical form it will always stay on the same sheet.