any experience with gravity simulations?

So I have a gravity simulation in which multiple objects are created and are attracted to eachothers by the laws of gravity.
now I am having problems to create stable systems with more then 2 objects.
the goal of my program is to create a stable solar system with multiple planets and moons.

for test purposes I create 1 star, and one planet with a moon.
with some code I let them orbit eachother as they should.
now it looks good in the beginning. the planet orbits the star and the moon orbits the planet
but the problem is that the star has a gravitational pull on the moon. that causes the moon to be pulled out of its circulair orbit. the moon eventually crashes on the planet.

so my question is. is there anyone here who has experience with creating stable systems in a gravity simulations? do you have any tips for me? or is not really possible to make (which I suspect now)

Ive spent most of the last couple weeks working on physics for my game engine.
I do think its possible, but I dont think it would be easy. I find that the best way to start is with momentum.
I established a momentum controller in my physics object like so(psuedocode, obviously) :

[assume airResistance = .97 and xMomentum = 10]
[this runs ever 5 miliseconds]
[handle xMomentum and yMomentum seperatly]


addYMomentum(-gravity);
if (yMomentum != 0) 
  move(this, yMomentum);
  yMomentum *= airResistance;
  if (yMomentum [is close to] 0)
    yMomentum = 0;

from there, maybe you could modify my momentum strategy to work with a center of gravity, and apply momentum respectively.
I you want to use any of my code, feel free. You dont have to give me credit.
https://github.com/Saucymeatman/EntropyGameEngine

I know this dosent solve you problom, but I hope that this helps at least.

Well you can always use box2d for that, but it’s understandable if you want to create your own physics engine for learning purposes.

I already wrote the physics for it and it is working fine.
the problem is I am looking for the starting velocity of the moon,star and planet so that a stable system emerges.
a system with 2 objects is no problem. even a system with 1 large object and 2 equally massed smaller objects orbitting eachother works fine.
that is because the 2 smaller objects have a center of gravity so it is in equilibrium with the 3rd larger object.
but when the mass of these 2 objects are not equal (like earth and moon) the system becomes unstable rather quickly.

Perfectly stable physics is kind of difficult.

Have you considered “faking” the orbit with something like verlet constraints? I guess it depends on the level of realism you’re trying to achieve.

It doesnt have to be a perfectly stable system.
I just want it to be semi stable so that body’s do not collide or get swung out of the system before a descent amount of simulation time has gone by.
faking is probally the solution I have to go for.

No it is not possible to have a stable calculation. You might get some very good one, but why bother?

You could just calculate all the ellipses for the planets and be done with it. No real simulation just a pre-calculated solar system.

here a cool blog post describing different gravity integration methods

Perfect is never attainable. Very good is all we can ever get. I think the reason one should bother is to produce a dynamic simulation capable of working under all kinds of situations. For example, maybe one day you get the urge to collide a couple of solar systems together - in that situation I don’t think that simple orbital equations are going to cut it.

As for starting off with a velocities, masses and radii to create stable orbits, I’m sure you can create a few equations, make up some masses and radii and then brute force the solution for a set of velocities. You don’t need to rely on all these computationally simpler methods so you can do the integration properly.

yeah right, I would like to see how you crash two solar systems together …

Challenge accepted lol!

Depending on the masses and distances some systems could be inherently unstable too (no matter how you set the velocities). e.g. most orbits around the Moon are unstable. Try setting all the masses/distances/velocities etc. in proportion to the Sun/Earth/Moon system and see if it is stable in your simulation.

Orbits = Kepler

I would like to see that too :slight_smile:

good idea, I will try it. but I suspect it will still be unstable, although it might be stable for a longer time because the suns influence is much weaker. I suspect that in the case of the earth and moon other factor play a role that stabilizes the orbit. like the tides or something. google doesnt give me an answer to this question.

My point was that you wouldn’t simulate such thing with some floating rigid spheres. When two orbital bodies collide they don’t just bounce off each other, they get deformed and ripped apart, something which you would handle as some special case if at all.

So as I said just calculate the orbits and let the planets position be calculated by their functions. And as roquen said use this => en.wikipedia.org/wiki/Kepler_orbit

thank you all for your input.
I will either use kepler, or use newton as I currently do. but then I will have to accept more simple systems without moons.

Earth’s moon can be modeled by Kepler’s. The two bodies are the active and dominate bodies.

So not to negate what was already said. But even with super accurate integrators, or even perfect math, under the laws of gravity 3 body systems are not stable. There are like 3 exact configurations of 3 body’s that are stable as far as mathematics are concerned but perturb them in any way…

Its called the 3 body problem. http://en.wikipedia.org/wiki/Three-body_problem

However in reality there is typically only one important body and the rest can be ignored. For example when considering the earths orbit around the sun, we can even ignore the earths mass and we have approximately a 1 body system with an infinitely light mass orbiting it. When considering earth, moon and sun we can consider earth moon as a system, then the earth/moon center of mass orbits the sun. If we want to add Jupiter to the mix, we ignore the effects of earth on Jupiter etc.

However full simulations are also possible, BUT you do it different from just brute force accuracy. You need the integration method to be reversible to conserve important properties of the system (mostly momentum). So a 2nd order leapfrog is typically way better than a 4th order RK method.

But do you need this accuracy for a game? Using parametric orbits probably works better anyway.

very interresting.
can you explain more about that last part?. I dont understand it.

I assume you talking to me and you are asking about the simulation stuff. Basically you can do a PhD thesis on this stuff. Its that hard to get accurate and right.

But a simple integrator, just use a 2nd order leap frog. If you don’t know what that is. Stick with parametric representations.

is the purpose of your game to model a solar system… or to give the user the impression the solar system has gravity…

If its the first… then your in for some challenges (hard ones) but it is do able.

if the later, then dont base the orbits of your planets on real physics … fake it.
Instead have the planets rotate each other in a fixed way… and use collision boxes to determine when gravity will affect other objects in the game. In this way the hard
part of establishing stable orbits is gone… but the illusion of physics is maintained.

j.