I'm new to computational physics and attempting an n-Body simulator in java but for some reason the planet in orbit leaves the orbit when it reaches the fastest accelaration aka the closest point to the other planet. I'm testing the velocity verlet algorithm for two bodies at the moment. I'm not quite sure what I'm doing wrong but here is the site im using to learn the Velocity Verlet algorithm. And here is my code:

    public void move(float simSpeed, Planet planet) {
        float deltaT = Gdx.graphics.getDeltaTime() * simSpeed;

        float deltaX = planet.getPos().x - this.pos.x;
        float deltaY = planet.getPos().y - this.pos.y;
        float alpha = (float) Math.toDegrees(Math.atan2(deltaY, deltaX));

        float distance = (float) Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
        float F = MainScreen.G * this.m * planet.getM() / (distance * distance);
        this.force.x = F * MathUtils.cosDeg(alpha);
        this.force.y = F * MathUtils.sinDeg(alpha);

        this.pos.x += this.vel.x * deltaT + 0.5f * (this.force.x / this.m) * deltaT * deltaT;
        this.pos.y += this.vel.y * deltaT + 0.5f * (this.force.y / this.m) * deltaT * deltaT;

        this.vel.x += (this.force.x / this.m) * deltaT;
        this.vel.y += (this.force.y / this.m) * deltaT;


EDIT: the code works fine if I use simple Newtonian equations without any integration.

  • 2
    $\begingroup$ Welcome to SciComp. If this is meant to simulate two gravitational bodies, what do the trigonometric functions have to do with it? Are the forces you are calculating between the bodies correct? $\endgroup$
    – MPIchael
    Jul 27, 2022 at 12:13
  • $\begingroup$ the forces are correct as the simulation works when using simple newtonian mechanics and the trigonometry is to separate the force into two components in x and y $\endgroup$
    – ght007
    Jul 27, 2022 at 20:40

1 Answer 1


There are two sources for such an error, if the exact solution is known to stay bounded.

  • Verlet becomes catastrophically incorrect in singular situations, that is, if two objects become close in a N-body simulation or any other with potentials with singularities.

  • "Sequential Verlet" is not Verlet, nor is it second order, nor is it symplectic. You get a complicated first order method with the ensuing error sources.

For expansion on these ideas see

  • 2
    $\begingroup$ One can check from the initial conditions if the total energy (kinetic (>0) + potential (<0)) is positive or negative. If positive then (with two bodies) the solution should be indeed such that one or both bodies fly away to infinity. $\endgroup$ Jul 27, 2022 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.