Planetary simulation

Hi guys,

as far as I am new here, I would like to ask for advice, I am working on a project and I am supposed to make simulation of our planetary system as you can see on link below.

http://csep10.phys.utk.edu/astr161/lect/solarsys/planet_orbits.gif

I am working in JBox2D with JavaFX and my problem is to make planets to rotate in directions as they should (circle).
Do you have any ideas how is this possible to do? (if it is possible in JBox2D).

Thanks in advance.
Hello!

You can definitely do this in jbox2d, either with sensors or just by iterating through all the bodies and applying forces.

You might want to just role your own physics though, as jbox2d can fudge things a bit for performance, and if you're looking for a super accurate simulation then it won't really be the best.

If you're only doing rotation around a point, rolling your own physics is super super easy, as you don't need collisions or anything. Just have a position and a velocity, and apply a gravitation force to the velocity every step.
This code may work. (From http://stackoverflow.com/questions/6758060/simulate-newtons-law-of-universal-gravitation-using-box2d )

Code: Select all

for ( int i = 0; i < numBodies; i++ ) {

    b2Body* bi = bodies[i];
    b2Vec2 pi = bi->GetWorldCenter();
    float mi = bi->GetMass();

    for ( int k = i; k < numBodies; k++ ) {

        b2Body* bk = bodies[k];
        b2Vec2 pk = bk->GetWorldCenter();
        float mk = bk->GetMass();

        b2Vec2 delta = pk - pi;
        float r = delta.Length();
        float force = G * mi * mk / (r*r);

        delta.Normalize();
        bi->ApplyForce(  force * delta, pi );
        bk->ApplyForce( -force * delta, pk );
    }
}
That code will make every body interact with all other bodies, which will probably cause the orbits to become unstable after a short while. For this case you would probably only want to make each body interact with the sun in the center.

Original thread: https://box2d.org/forum/viewtopic.php?t=9302