Neat little Euler gravity simulation coded by me

General physics and astronomy discussions not directly related to Celestia
Topic author
Liquidscript
Posts: 8
Joined: 13.01.2003
With us: 21 years 10 months
Location: Arizona

Neat little Euler gravity simulation coded by me

Post #1by Liquidscript » 04.05.2004, 05:46

Hey guys,
I like this little physics offshoot forum. It's interesting.

A few days ago I decided to learn how to program with OpenGL just for fun, instead of studying for AP tests (i know, bad choice, but whatever).

So I decided to make a simple simulation that approximates more than 2 orbiting bodies by calculating the force of gravity between each body and summing them together at each timestep, to be applied as accelerations, which in turn change the velocities, and then the positions, blah blah blah.

After a few iterations, I got it working in 3d. Use the arrows to rotate the view, the central "star" has 3 lines representing each axis. Enter resets the simulation with a semi-random initial setup. I say semi, because I've programmed it to attempt to make each nth planet orbit around the (n-1)th planet. Oh, and one more thing. The central star IS influenced by the planets that orbit it, but each frame i center the camera on the central star, so it appears not to move. If it wasn't centered on the star, the entire system would eventually move out of the view and it would be no fun to watch.

Looking now, I see that this board doesn't have upload capability, so i'll post a link shortly on some webspace i have for something else. Its at geocities, so if it doesn't work, yell at me and i can put it somewhere else.

http://www.geocities.com/jamesbonduhs/g ... avity.html

tony873004
Posts: 132
Joined: 07.12.2003
With us: 20 years 11 months
Location: San Francisco http://www.gravitysimulator.com

Post #2by tony873004 » 04.05.2004, 18:35

That's a fun application. I think your time steps are too large because the planet's moon keeps getting ejected from the system at a speed that seems to indicate too large of a time step rather than a genuine slingshot maneuver. I like the half phase of your planet.

I've got my own semi-Euler based gravity simulator. Soon I'm going to try to upgrade it to Runge-Kutta 4. It's available for free download at http://www.gravitysimulator.com.

You can make the central star move without the system leaving the screen if you compute the center of mass of the system, and focus your camera there. Gravity Simulator can do this, and its very cool to watch everything wobble.

Topic author
Liquidscript
Posts: 8
Joined: 13.01.2003
With us: 21 years 10 months
Location: Arizona

Post #3by Liquidscript » 05.05.2004, 05:07

Yeah, at first I centered the camera on the center of mass of the system, which worked very well when I just had 2 orbiting bodies, but a few of the other versions had randomly initialized groups of 20 or so orbiting planets which all interacted chaotically, resulting in their launches into deep space which in turn would skew the center of gravity so the rest of the system was off the screen. I decided to center on the "star" during one of these builds before I implemented something to create an orbiting structure where the nth body orbits around the (n-1)th body.

For the problem with timesteps, I've just been using round, unitless numbers for masses, velocities, forces, and time, and I just have a timestep of 1 unit of time currently. I might try changing the code so I can choose a timestep of 0.01 if I wanted, which would probably result in smoother orbits and less error. One of the other reasons that they jump out into deep space is because they can interpenetrate so the distance between them approaches zero, which results in an extremely high force between the objects. I haven't implemented any collision detection... yet. :)

Oh, I saw the neatest gravity simulation that came with a physics SDK that I read about once. The demo program had differently shaped 3d objects like boxes, spheres, cylinders, tubes, etc., randomly initialized in space with appropriate masses. Friction and collision detection was activated, and the resulting objects formed clumps of about 20 or 30 objects (there were about a total of 100 or 200 individual objects on screen) which began to orbit like planets. It was quite amazing and it gave an impression of how the tiny particles in gas clouds can eventually form entire solar systems. Some of the large clumps would orbit around each other. Then the spinning that resulted would cause the individual pieces to shift around, making the spinning clumps stretch out from spherical shapes into disks or cylindrical shapes and break into smaller clumps. I'll try to find it sometime to post a link, but right now I need to sleep, because I have AP exams tomorrow and thursday.

ElPelado
Posts: 862
Joined: 07.04.2003
With us: 21 years 7 months
Location: Born in Argentina
Contact:

Post #4by ElPelado » 05.05.2004, 09:47

Very nice work you did!
Once I run the simulation and the smallest body started to orbit first the big body and then the medium body, and then again the big body :lol: It was very interesting to see that...
---------X---------
EL XENTENARIO
1905-2005

My page:
http://www.urielpelado.com.ar
My Gallery:
http://www.celestiaproject.net/gallery/view_al ... y-Universe


Return to “Physics and Astronomy”