## Circular Orbits

Posts that don't fit into other categories.
deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

### Circular Orbits

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)

Code: Select all

`/**    * 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();

Code: Select all

`//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);            //account for the elapsed time      force.multiply(elapsedTime);                        //apply this force of gravity to planet      planet.applyForce(force);            //shouldn't this always be 1?      Vector2 difference = gravityWell.getWorldCenter().difference(planet.getWorldCenter());      double distance = difference.getMagnitude();      Log.d("distance", "" + distance);   }}//update the worldworld.update(elapsedTime);`

Here is the code that I use to calculate the force of gravity and the orbital velocity of another body.

Code: Select all

`/**    * 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();            //calculate the mass      double mass = gravityWell.getGravitationalMass();            /**       * calculate the acceleration of gravity and multiply it by the vector to give us a force with a direction       *       *     G * M       * a = -----       *      R^2       */      double acceleration = (G * mass) / Math.pow(distance, 2);      difference.multiply(acceleration);            return difference;   }      /**    * 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();            //calculate the mass      double mass = gravityWell.getGravitationalMass();            /**       * calculate the orbiting velocity to be applied the the orbiting body       *         _______       *        / G * M       * v =   /  -----       *     \/     R       */      double v = -Math.sqrt(G * mass / distance);            //apply this velocity on an angle of 0 and then rotate it for the given direction of the vector      Vector2 velocity = new Vector2(0, v);      velocity.rotate(difference.getDirection());            return velocity;   }`

deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

### Re: Circular Orbits

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.

deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

### Re: Circular Orbits

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.

deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

### Re: Circular Orbits

I believe I have solved the issue. The code that was applying the force to the planets was being done in the game loop. I moved that over to a StepListener and now the orbits are stable.