# Using velocity verlet algorithm for nbody simulation results in planet leaving orbit

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.

• 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? Jul 27, 2022 at 12:13
• 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 Jul 27, 2022 at 20:40