Concept | Orbital Physics Simulation

This was something I had tried a while ago, and had some success at, but it ultimately did not do what I wanted.

Since then, it has been re-made, and optimized a ton.

What we’ve done here is simulate the gravitational pull between masses in the attempts to get little gum-balls to orbit around bigger gum-balls :slight_smile:


Not very impressive in screen-shot form, this needs to be seen in action to really be appreciated. So here is the blend file.

Download

Just hit P to play. The script will generate ~60 randomly-sized planets around the static sun in the center, then attempt to kickstart each planet into orbital velocity (about 99% of escape velocity). Utter Chaos ensues as the planets throw each other out into the void. Usually though (it’s randomly generated) you will get 2-3 of the larger bodies still making stable orbits after a few minutes of running the simulation.

Move the mouse to rotate your view. Just a simple mouse actuator, so it’s not great but does its job.

The whole thing runs off of one script, most of which deals with the initial setting up of the system. Each body has a (random) property Radius and Density, which is used to calculate volume and mass.
Once that’s done, it keeps a list of all active bodies (killing bodies that get too far away from the sun)
Iterates through each body, which in turn iterates through each other body, calculating pull based on the two bodies masses and distance to one another. This along with a vector gives you how much force to apply XYZ direction.
There is no collision detection, but a system for Accretion (smaller bodies smashing into/melding with larger bodies on collision) could probably be fathomed.

This is not a game, and has no gameplay elements. This is just a cool concept I wanted to share with the world. I’m sure it’s been done before, and better than this.

I don’t plan on using this for anything. Anybody who wants to use any part of this is free to rip whatever they like from this.

I love it! I just can’t stop of playing with values of the central star and the planets. As you said, after a time you can see how It turn into an stable solar system, even there’s “exiled planets”. Keep work!

Hey, I have made one version that uses render.drawLine to draw the orbites:


File:
orbital physics sim 1.1 with draw line 1.blend (110 KB)

The DRAWLINES variable say if you want to draw lines, and DRAWLINE_LIFETIME control how many frames one line will exists until disappear.

really cool. I wish I was better at math so I could understand what’ going down with the gravity etc…

The trails on the planets is a good idea! I wanted to do something like this, but never thought of using drawLine to pull it off!

@Smoking:
The math behind calculating the gravity is actually pretty simple!
The Gravitate() method does all the work for that.

The force of gravity is equal to your mass times the other body’s mass, divided by the square of your distance from that body’s center.
If we get your Vector to the other body’s center, we can multiply that by Force to get the amount of XYZ force to apply to push you toward the center with the appropriate force.

Here is one of the sites I used as reference. It explains Newton’s Law of Universal Gravitation, which is what this is demonstrating.

Thanks, after looking at the site and going through your code it does seem much more understandable. I’ve recently been trying to simulate Earth type gravity in game (without using the built in physics) but didn’t have much luck. I wanted it for particles since enabling physics for large numbers of particles is a real drain on resources, but when I try it they accelerate much too quickly. Math isn’t really my strong suit. :slight_smile:

I have improved a little bit the draw lines, I added a tick system, just put an int in the “LINE_TICK” variable, so the current position of the planet will be added only every “x” frames (so there’s less line to draw), it increase perfomance. And just for fun I added a random force applied to the star, just to simulate the way how the stars move through the galaxy.

Look the orbits of those tree purple planets LOL, they behave like two moons around a planet.

File:
orbital physics sim 1.1 with draw line 2.blend (115 KB)

Hey I like drawline I never knew that existed!