1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

B Gravitational attraction between two masses

  1. Jul 5, 2016 #1

    I'm writing a javascript application to illustrate gravitational attraction between masses, but am getting hung up on some of the details, particularly mow far masses move in response to each other's gravitation.

    I've read all available wikipedia articles on gravity and have come up with the following:

    Gravitational constant: (6.67408 x 10^-11 meters^3) / (kilos * seconds^2)
    Force between masses: G * (mass 1 in kilos * mass 2 in kilos) / (distance in meters ^2)

    Now for a concrete example
    Mass 1: 1 kilo
    Mass 2: 5 kilos
    Distance: 1 meter
    Seconds: 1

    Pluging that into above yeilds
    f = (667408 * 1^3 * 1 * 5) / ( 10^11 * 1 * 1 * 1^2)
    which reduces to
    f = 667408 * 5 / 10^11
    f = 3.33704 x 10^6 / 10^11
    f = 3.33704 x 10^-5

    Simple enough, but here's where I'm getting lost. How far would the 1 kg mass move and how far would the 5 kg mass move in response to this force?

    Secondly, so long as all values in f = G x m1 * m2 / d^2 are converted to metric, is "G" just 6.67408 x 10^-11

    Thanks for any help
  2. jcsd
  3. Jul 5, 2016 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    CORRECTION: This post overlooked that the OP specified a time of 1 second. The proportions are still correct but the amounts of motion are (probably) wrong.

    Their accelerations will be inversely proportional to their masses. The velocities and distances are simple integrals of acceleration and velocity, respectively, so they are also inversely proportional to their masses. They will move toward each other till they collide. The total motion of both is Distance=1 meter. It is simple algebra to find the answers. Mass 1 travels 5/6 meter and mass 2 travels 1/6 meter.
    Last edited: Jul 6, 2016
  4. Jul 5, 2016 #3
    Since the time in the original was specified as 1 second, does that mean they would come into contact after 1 second? For such small masses, that seems like a really powerful attraction. Almost as fast as two strong magnets.
  5. Jul 5, 2016 #4
    OK. Just thinking out loud...

    Since F is now known, acceleration for each mass is:
    f = m*a
    a = f / m
    a1 = 3.33704 x 10^-5 / 1 (mass 1)
    a2 = 3.33704 x 10^-5 / 5 (mass 2)

    a1 = 3.33704 x 10^-5 meters per second ^2
    a2 = 6.7408 x 10^-6 meters per second ^2

    so after 1 second mass 1 moves 3.33704 x 10^-5
    after 2 seconds it moves 6.67408 x 10^-5
    after 3 seconds it moves 1.001112 x 10^-4

    after 3 seconds total distance moved = 2.002224 x 10^-4

    I think I get that part. Just loop the script til total distance for mass 1 and 2 equals 1/6, 5/6 of starting distance. Possible snafu though is since the distance decreases with each iteration, does that mean the force has to be recalculated after each step since it will get progressively smaller each step?
  6. Jul 5, 2016 #5


    User Avatar

    Staff: Mentor

    Actually, this problem not as simple as looks at first glance. The catch is that as soon as either mass moves even slightly, the distance between them changes, and this changes the force, which changes the acceleration, which changes the speed, which changes the amount they move, which changes the distance, and so forth. Doing this problem correctly requires a fair amount of calculus and solving a differential equation... and that's probably not the answer that you were hoping for. (Although you can google for "gravitational two-body problem" and "reduced mass" if you want to go that way).

    However, if you're just looking for a javascript application that will illustrate the motion, you can do an approximate simulation. Suppose at time ##t_1## the masses are a distance ##r## apart and they are moving with speeds ##v_1## and ##v_2## respectively. You can calculate the force between them, and that will give you the accelerations ##a_1## and ##a_2## at that time ##t_1##. Now you can approximate the new positions and speeds at time ##t_2=t_1+\Delta{t}##:
    The speed of each object changes by the acceleration times ##\Delta{t}##; this is just ##v=at##.
    The position of each object changes by the average speed (the sum of the old speed and the new speed divided by two) times ##\Delta{t}##; this just distance equals speed times time.

    Now you have the new positions and speeds, which you can use to calculate the new distance between them and the new forces, and you can just run this in a loop, updating the time by ##\Delta{t}## each time through. The smaller you make ##\Delta{t}##, the more iterations it will take to simulate the same amount of time and the more accurate the simulation will be. With a bit of care (and if you are familiar with vectors) you will be able to make this work even in three dimensions and even if the objects start out moving in arbitrary directions.
  7. Jul 5, 2016 #6
    Thanks for the search tip.

    Luckily, since the end product is a javascript animation, and I'm limited to screen resolution, it's actually better, for my purposes, to perform each step in a loop rather than resorting to differentials (which is good since I never mastered them). Thanks again and If I get it working, I'll post a link.
  8. Jul 5, 2016 #7


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Sorry. I overlooked the 1 second limitation. That doesn't change the proportions of the motion, but it does change the total.
  9. Jul 5, 2016 #8


    User Avatar
    Homework Helper

    There is a closed form solution for velocity versus current position, and for time versus current position, but no apparent closed form solution to get position, velocity, or acceleration versus time:


    Getting back to a numerical method, you could use something like RK4 to improve the accuracy:


    Last edited: Jul 6, 2016
  10. Jul 6, 2016 #9
    Well, I got it working. Here's the relevant Javascript method

    proto.getPVTable = function(inP1, inP2)
    var t = this;

    // get particle centers
    var ax = inP1.getCX();
    var ay = inP1.getCY();
    var bx = inP2.getCX();
    var by = inP2.getCY();

    // compute triangle sides
    var dx = ax - bx;
    var dy = ay - by;
    var distPixels = Math.sqrt(dx * dx + dy * dy);

    // compute sines and cosines
    var acos = dx / distPixels;
    var asin = dy / distPixels;
    var bcos = -acos;
    var bsin = -asin;

    // mass
    var amass = inP1.mass;
    var bmass = inP2.mass;

    // radiaii
    var arad = inP1.getRad();
    var brad = inP2.getRad();

    // pre compute fmass (G * m1 * m2)
    var fmass = (.0000000000667408 * amass * bmass);
    var lim = arad + brad;

    // declare other vars
    var distMeters = distPixels * MetersPerPixel;
    var result = [];
    var force;

    while (distPixels > lim)
    // compute force
    force = fmass / (distMeters * distMeters);

    // compute acceleration
    aacc = force / amass; // meters per second
    bacc = force / bmass; // meters per second

    // convert meters to pixels
    appm = aacc * PixelsPerMeter;
    bppm = bacc * PixelsPerMeter;

    // check for overshoot
    if ((distPixels - appm - bppm) < lim)

    // compute x and y deltas and add to result
    // (not sure why, but had to add
    // '-' signs to make it work correctly)
    result.push({ax: -appm * acos,
    ay: -appm * asin,
    bx: -bppm * bcos,
    by: -bppm * bsin});

    // update dist vars
    distPixels -= (appm + bppm);
    distMeters = distPixels * MetersPerPixel;

    // handle leftovers
    if (distPixels > lim)
    var tmass = amass + bmass;
    var rem = distPixels - lim;

    // divide remainder by mass proportion
    appm = rem * amass / tmass;
    bppm = rem * bmass / tmass;

    // compute point deltas and add to result
    result.push({ax: -appm * acos,
    ay: -appm * asin,
    bx: -bppm * bcos,
    by: -bppm * bsin});

    return result;

    Works pretty well with two particles. Now to add multi particle functionality...
  11. Jul 7, 2016 #10


    User Avatar
    Homework Helper

    I don't quite follow the code. It appears it calculates distance moved based on acceleration, as opposed to calculating change in velocity based on acceleration, then calculating distance based on velocity. The RK4 method I mentioned previously would be more accurate, but since this is a 2 body (and perhaps later an n body) problem, you can also use the fact that total energy of the system is constant. Any decrease in gravitational potential energy corresponds to an increase in kinetic energy. This can be used to adjust the results so that total energy remains constant, which further improves the accuracy.

    Handling multiple particles will be difficult.


    In addition to the closed form solution I linked to before, a closed form solution based on Kepler's law produces the same result. From another thread, showing links to the two key posts:


    Last edited: Jul 7, 2016
  12. Jul 7, 2016 #11
    I looked them over, but will have to expend much more effort to understand them. (I never took calculus and all efforts to learn it on my own have failed. Just don't get the whole "make things really really tiny and viola! Magic happens" thing.)

    I will try, but for the mean time, here's a link to what I have so far.


    It has only been tested in Firefox, so If you take a look, you should use that browser. All you do is click anywhere in the white framed area to deposit the forst "particle" (100,000 kilos) and click somewhrere else to deposit the second particle. Once they collide, you can drag the dots around to other locations. Dragging the big dot doesn't reset the motions, but dragging the small one does.

    I'm probably missing something, but to my eye, it looks pretty natural.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted