Search code examples
javascriptsimulationbehavior

Simulation of planets orbiting


I am creating a simulation of planets with the help of newton's law(here) and everything works fine but I can't find any source how to calculate the perfect start velocity of a planet in order to orbit another planet given following known parameters of both planets: mass, distance and G

following code to calculate acceleration:

calcBodyAcceleration(body1, body2) {

    var G = Settings.get("G");
    var mults = Settings.get("multipliers");

    var pos1 = Vec2.add(body1.position, body1.center);
    var pos2 = Vec2.add(body2.position, body2.center);

    var subtraction = Vec2.subtract(pos1, pos2);
    var dist2 = subtraction.magnitudeSq();
    var dist = Math.sqrt(dist2);

    var mass1 = body1.mass * mults.mass;
    var mass2 = body2.mass * mults.mass;

    var mass = mass1 * mass2;
    var force = G * (mass / dist2);

    subtraction.divide(dist).multiply(force).divide(mass1);

    body1.acceleration.subtract(subtraction);

}

Solution

  • You can calculate the speed using the formula

    v = sqrt(Gm/r)

    where G is the gravitational constant, m the mass of the object around which you orbit and r is the radius of the orbit(from the center of an object).