Java-Gaming.org Hi !
 Featured games (88) games approved by the League of Dukes Games in Showcase (681) Games in Android Showcase (196) games submitted by our members Games in WIP (744) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 any experience with gravity simulations?  (Read 7758 times) 0 Members and 1 Guest are viewing this topic.
Rakiayn

Senior Newbie

 « Posted 2013-11-09 15:35:52 »

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)
saucymeatman
 « Reply #1 - Posted 2013-11-09 16:21:29 »

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]
 1  2  3  4  5  6 `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.
pploco1996

Junior Devvie

Medals: 2
Exp: 1 year

 « Reply #2 - Posted 2013-11-09 17:52:50 »

Well you can always use box2d for that, but it's understandable if you want to create your own physics engine for learning purposes.
 Games published by our own members! Check 'em out!
Rakiayn

Senior Newbie

 « Reply #3 - Posted 2013-11-09 18:01:49 »

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.
davedes
 « Reply #4 - Posted 2013-11-09 18:32:32 »

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.

Rakiayn

Senior Newbie

 « Reply #5 - Posted 2013-11-09 19:00:56 »

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.

Danny02
 « Reply #6 - Posted 2013-11-09 19:06:24 »

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
quew8

JGO Knight

Medals: 51

 « Reply #7 - Posted 2013-11-09 19:42:06 »

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

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.
Danny02
 « Reply #8 - Posted 2013-11-09 20:29:29 »

yeah right, I would like to see how you crash two solar systems together ...
lcass
 « Reply #9 - Posted 2013-11-09 20:34:33 »

yeah right, I would like to see how you crash two solar systems together ...
Challenge accepted lol!
 Games published by our own members! Check 'em out!
kramin42
 « Reply #10 - Posted 2013-11-10 06:08:53 »

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.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Roquen
 « Reply #11 - Posted 2013-11-10 11:04:32 »

Orbits = Kepler
Rakiayn

Senior Newbie

 « Reply #12 - Posted 2013-11-10 12:43:23 »

yeah right, I would like to see how you crash two solar systems together ...
Challenge accepted lol!

I would like to see that too

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.

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.

Danny02
 « Reply #13 - Posted 2013-11-10 12:56:49 »

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
Rakiayn

Senior Newbie

 « Reply #14 - Posted 2013-11-10 17:31:40 »

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.

Roquen
 « Reply #15 - Posted 2013-11-10 19:20:06 »

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

JGO Ninja

Medals: 108
Exp: 18 years

Computers can do that?

 « Reply #16 - Posted 2013-11-12 08:27:37 »

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.

I have no special talents. I am only passionately curious.--Albert Einstein
Rakiayn

Senior Newbie

 « Reply #17 - Posted 2013-11-15 03:37:41 »

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

JGO Ninja

Medals: 108
Exp: 18 years

Computers can do that?

 « Reply #18 - Posted 2013-11-15 13:32:24 »

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.

I have no special talents. I am only passionately curious.--Albert Einstein
jmguillemette
 « Reply #19 - Posted 2013-11-15 15:07:23 »

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.

-=Like a post.. give the author a medal!=-
trollwarrior1
 « Reply #20 - Posted 2013-11-15 16:45:00 »

If the moon crashes into the planet, because it is attracted by the sun, you should move the planet + moon away from the sun. Or make the moon smaller. Or make the planet bigger.
Roquen
 « Reply #21 - Posted 2013-11-15 17:08:05 »

I repeat: Kepler's.  Don't waste your time making simple problems hard.
delt0r

JGO Ninja

Medals: 108
Exp: 18 years

Computers can do that?

 « Reply #22 - Posted 2013-11-15 17:44:08 »

To back up Roquen's point. NASA and co use these simple parametric orbits for everything from space probes to knowing where to point that dish to get signals of a satellite. That is you know they are ellipsis and how fast it goes at different parts of the ellipses. Of course i think that is too much. Just stick to circles. If its good enough for multi million dollar space missions surely its enough for a game.

In Eve planets don't even move.

I have no special talents. I am only passionately curious.--Albert Einstein
jmguillemette
 « Reply #23 - Posted 2013-11-15 18:34:52 »

interesting point about eve...

didnt know that .. barely ever played the game.. but its a gorgeous looking one.

-=Like a post.. give the author a medal!=-
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 CopyableCougar4 (42 views) 2016-06-25 16:56:52 Hydroque (78 views) 2016-06-22 02:17:53 SwampChicken (79 views) 2016-06-20 13:22:57 SwampChicken (79 views) 2016-06-20 13:22:49 SwampChicken (74 views) 2016-06-20 13:22:26 Hydroque (117 views) 2016-06-15 08:22:50 Hydroque (110 views) 2016-06-13 06:40:55 DarkCart (232 views) 2016-05-29 02:30:33 Hydroque (194 views) 2016-05-26 14:45:46 Mac70 (184 views) 2016-05-24 21:16:33
 Spasi 49x theagentd 45x NegativeZero 39x KaiHH 28x LiquidNitrogen 23x princec 19x kingroka123 15x CoDi^R 15x ags1 14x ra4king 13x Opiop 10x purenickery 9x Hydroque 8x Archive 8x SHC 8x czak 7x
 Making a Dynamic Plugin Systemby Hydroque2016-06-25 00:13:25Java Data structures2016-06-13 21:22:09Java Data structures2016-06-13 21:20:42FPS Camera Tutorialby Hydroque2016-05-22 05:40:58Website offering 3D Models specifically for games for freeby vusman2016-05-18 17:23:09Website offering 3D Models specifically for games for freeby vusman2016-05-09 08:50:56Website offering 3D Models specifically for games for freeby vusman2016-05-06 11:10:21Website offering 3D Models specifically for games for freeby vusman2016-04-29 12:56:17
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org