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 wells`

for (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 world

world.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;

}