I am trying to build a perfectly circular orbiting body around another body. The issue that I am running into is that the orbit is not a circle and quickly gets thrown off balance and becomes extremely elliptical even though I believe the math is correct. The world gravity has been set to 0. Any thoughts or suggestions as to what is wrong here?

Here is the initial code to place the body into an orbit. (Subclass of Body)

/** * Places this body into an orbit around the gravityWell at a given distance and angle from the center mass * * @param gravityWell the object that this body will orbit * @param distance the distance from the gravityWell to place the body * @param theta the angle around the gravity well to place the body */ public void orbit(final GravityWell gravityWell, final double distance, final double theta) { //move this body to the distance and rotate it around the center Vector2 translate = new Vector2(distance, 0); translate.rotate(theta);

//now move the rotated object to center on the gravity well translate.add(gravityWell.getTransform().getTranslationX(), gravityWell.getTransform().getTranslationY()); translate(translate);

//calculate the gravitational force between the two objects Vector2 orbit = GravityUtil.getOrbitingVelocity(gravityWell, this);

//set the linear velocity of this object to be the orbiting velocity setLinearVelocity(orbit); }

On each frame of the game loop, I am applying the force of gravity to each of the planets before calling world.update();

//for each of the gravity wellsfor (GravityWell gravityWell : gravityWells){ //for each of the planets for (Planet planet : planets) { //calculate the gravitational force between the two objects Vector2 force = GravityUtil.gravitationalForce(gravityWell, planet);

/** * Calculates the gravitational force between a gravity well and a body * * @param gravityWell the object that exerts a gravitational force on other bodies * @param body the object that will receive the gravitational force from the gravity well * @return the gravitational force */ public static Vector2 gravitationalForce(final GravityWell gravityWell, final Body body) { //calculate the distance between the gravity well and the body Vector2 difference = gravityWell.getWorldCenter().difference(body.getWorldCenter()); double distance = difference.getMagnitude();

//normalize the vector to be used as our gravitational force once the acceleration has been applied difference.normalize();

/** * Calculates the orbiting velocity of orbiting body given the gravity well * * @param gravityWell the object that exerts a gravitational force on other bodies * @param orbitingBody the object that will be orbiting the gravity well * @return the orbit velocity to apply to the orbitingBody */ public static Vector2 getOrbitingVelocity(final GravityWell gravityWell, final OrbitingBody orbitingBody) { //calculate the distance between the gravity well and the body Vector2 difference = gravityWell.getWorldCenter().difference(orbitingBody.getWorldCenter()); double distance = difference.getMagnitude();

I suspect the issue may be somehow related to each world step and how/when I am applying the force of gravity to the object on each iteration of the game loop. Perhaps each world step and the way I am applying the gravitational force are not perfectly in sync? I noticed that when I start to manipulate the elapsed time (slow down or speed up), the orbit noticeably changes as well. However, I am still not sure as to how to fix this.

Thinking about it even more, I think I have an idea of where the problem lies but still unsure of the solution. The world does not actually know of any gravitational forces on this body object since I am applying them outside of the world object. Therefore, I believe that the body is not actually traveling in a circular trajectory, instead, each step of the world is actually moving the object along a very small straight line. Since this is being done at 60 frames per second, it looks nearly circular on screen but is not perfect. When the elapsed time can fluctuate slightly between frames, each of these straight lines may be slightly different lengths which is why this is not traveling in a perfect circle.