Welcome back to a new post about physics. Last time I talked about Ballistic Motion in it’s simplest form: Dropping a ball under the influence of gravity. This time I will explain Orbital motion or more specifically, the n-body problem. Just like last time, let’s take a look at what wikipedia has to say about it:

About orbit:

In physics, an orbit is the gravitationally curved path of an object around a point in space, for example the orbit of a planet around the center of a star system, such as the Solar System.[1][2] Orbits of planets are typically elliptical.

Current understanding of the mechanics of orbital motion is based on Albert Einstein‘s general theory of relativity, which accounts for gravity as due to curvature of space-time, with orbits following geodesics. For ease of calculation, relativity is commonly approximated by the force-based theory of universal gravitationbased on Kepler’s laws of planetary motion[3]

About the n-body problem:

The n-body problem is the problem of predicting the motion of a group of celestial objects that interact with each other gravitationally. Solving this problem has been motivated by the need to understand the motion of the Sun, planets and the visible stars. Its first complete mathematical formulation appeared in Isaac Newton‘s Principia (the n-body problem in general relativity is considerably more difficult).[citation needed] Since gravity was responsible for the motion of planets and stars, Newton had to express gravitational interactions in terms of differential equations. Newton proved in thePrincipia that a spherically-symmetric body can be modelled as a point mass.

So what we’ll be doing here is create a set of masses (spheres) that will orbit around each other due to gravitational attraction. In my case I will also put in one big mass so the effect will be more clear, but that shouldn’t stop you from playing around with it!

Just like last time, let’s see what we will be needing of our masses to create the simulation: As it contains moving masses we will be needing the good old Acceleration, Velocity and a Position. Next to those, we need something that will cause a change in velocity. This something are 2 things actually and they are called Mass and Force. Force is a thing that will cause acceleration and thus changing velocity and position. Mass is a property of our spheres that resist the change in Velocity.

The first thing that we want to do is to find out the force that is acting between 2 masses, this is done with the following formula:

Where F is the force, G is the gravitational constant m1 and m2 are the 2 masses and r is the distance between the 2 masses. Everything is pretty much a given as you can fill them in with the properties given except for the gravitational constant. I’m not going to dive into that at this time, just know that this is a very small fixed number and if you want to find out more you can check wikipedia. For now I will just use a small number that works for me to get something stable. You can obviously play around with it if you want to make something realistic.

Now that we know how to figure out the force, let’s do some coding to get things moving!

First we will need to figure out the distance between the 2 objects which is easily done by just subtracting between the 2 positions:

I’m gonna assume that whatever vector class you use, has some way of finding out the length. As we define our own masses we don’t need to calculate anything except the distance, which we just did. So now we can work out the force:

Now we have a float called force and we can figure out the acceleration with the formula:

F = m A ( A = F / m with some algebra)

As you might notice there is one problem here dividing 2 floats will not result in a vector which Acceleration is. So we need to make force a vector first and we do that this way:

Now that we have the force as a vector, we can use the previous formula to figure out the Acceleration of the 2 bodies:

And now that we know the Acceleration, it’s just a matter of integrating to get the Velocity and Position of the object and you have objects orbiting! Here’s the complete source that loops through all the objects:

And if everything worked out fine, you should get something like the following video, but as it’s sometimes hard to get something stable out of it, you might want to play around with it a bit.

In the next part I will go into soft body mechanics using spring systems.