Search code examples
javascriptphysicsgame-physicsverlet-integration

Verlet / Euler Integration is inaccurate


I want create some physx to game, and I started with small example to understand how it works. During this i had a few problems but i resolved them in 90%.

To create my exmaple i studied some other examples and to create this one i used: codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/

At first - This is dirty and inefficient code, only 1 thing i am interested in two problems:

#1 There is "timestep" loop to create accurate ellipse but if i move 1 object (second is static) with for example steps = 5, ellipse is accurate, but if both object are dynamic, curves are totaly inaccurate.

BUT run with steps = 1 my objects are more accurate (WHAT?) moreover if 1 object is static my ellipse is little inaccurate.

planet1.updateVelocity(planet2.position);

planet1.updatePosition();
planet1.repaint();

jsfiddle example with 1 static - http://jsfiddle.net/hnq8eqta/ change window.steps (1 or 5) to test.

planet1.updateVelocity(planet2.position);
planet2.updateVelocity(planet1.position);

planet1.updatePosition();
planet1.repaint();

planet2.updatePosition();
planet2.repaint();

jsfiddle example with 2 dynamic - http://jsfiddle.net/agbhwe9g/ change steps too.

#2 I think this is not normal behavior - if 1 of object have greater inital vector, both objects trajectory is werid and they run away from the screen. Is it normal for this alorithm? We can do very similar simulation here: phet.colorado.edu/sims/my-solar-system/my-solar-system_en.html but this is not the same...

window.planet1 = new Planet("planet1",250,350,0,1);
window.planet2 = new Planet("planet2",550,250,0,-1);
//changed to
window.planet1 = new Planet("planet1",250,350,0,1);
window.planet2 = new Planet("planet2",550,250,0,-2);

example - jsfiddle.net/hr1ebq3c/

Whats wrong with my verlet integration?


Solution

  • First, what you are using is not Verlet but the symplectic Euler method.

    Second, it is of utmost importance to treat a coupled system as a coupled system. In this special instance this happens to be correct for steps=1. Any other value of steps or an implementation of Verlet in this style will destroy the consistency of the method.

    Always compute the accelerations for all components of the system at once, without updating any position or velocity values in between.