# I lied, there’s more physics # I lied, there’s more physics

I

Last time we wrapped up the basic implementation of all the building blocks we need to be able to create a solar system and update it at a data layer level and then render it in Unity. This time we’ll start focusing on how we’re actually going to create these solar systems.
The end goal is to be able to procedurally generate them from scratch. Before that though, we need to be able to relatively easily create these solar systems.

Right now, in order to create a solar system, we would have to either spend a lot of time testing and trying different values for radius, surface gravity, position and initial velocity for each of our orbitals or calculate those values by hand from a different set of data such as orbital elements, henceforth known as COEs (classical orbital elements).
This needs to be massively simplified. Ideally, all we would need to input in order to get a stable system would be an initial position of the orbital and a reference to it’s main orbital effector.

We need to agree on is what is shaping an orbital’s orbit. For planets, asteroids and comets it is mainly the star of the solar system. For moons it is mainly the planet they’re orbiting. Now, you could simplify and apply the same logic I did where I encapsulated all the effects within the solar system into a sort of hierarhical system but that’s cheating in my view. The fact of the matter is that since the star is considerably more massive than any of the planets, and the planets considerably more massive than any of the moons, as long as the ratios between these hierarhical levels remain somewhat realistic that effect limitation should just pop out of the equations on its own.
In the interest of retaining some sanity, when calculating a planet’s parameters to conform with a given orbit, we’re going to disregard the effects other planets have on it. This should work fine most of the times if we keep the planets far apart. Similarly, when calculating the parameters of a moon we’re going to consider just the planet and that moon. This is what seems to be known as the two body problem. To quote from Wikipedia: “In classical mechanics, the two-body problem is to predict the motion of two massive objects which are abstractly viewed as point particles. The problem assumes that the two objects interact only with one another; the only force affecting each object arises from the other one, and all other objects are ignored.”
Due to wanting to keep this efficient, I will not allow the barycenter of these systems to be outside the larger body, in other words, everything orbits something and that something is considered immovable for that calculation. This allows us to deal with a simplified problem, namely the “central-force problem”.
Finally, whilst it would be “cool” to have orbits that lie on a plane outside of the XZ one, that makes the math a lot more complex to unravel and given that there will be enough complexity in the game as is, for now at least, I will be dealing with planar orbits only. I have a hunch that this will simplify quite a few things down the line, including gameplay whilst sacrificing complexity that only a niche of players would miss.

What we need first is a way of calculating the velocity a body needs to have in order to be able to circularily orbit another body. For that, we need the formula for calculating orbital velocities which you can find below.

v= \sqrt{\frac{G*M} d}

As expected by now the velocity depends only on “G”, the gravitational constant, “M” the mass of the body being orbited and “d” the distance between the two. With this new equation in the toolbag we can now very easily produce solar systems with multiple planets and moons and other objects revolving around a star. But this is too easy. Let’s look further into how we can get elliptical orbits.

To tackle this second part of the problem we need to talk about mecanical, kinetic and potential energy. First the easy bit, the total mecanical energy (T.M.E) is the sum of the potential and kinetic energy.
Kinetic energy (K.E) is the energy a moving body has due to its velocity. No velocity, no kinetic energy. Potential energy is the energy a body has due to its position relative to another body, the higher the distance, the more potential energy available. Now, there’s actually two kinds of potential energy, the simple kind that we use near the Earth’s surface and the one caused by gravity a.k.a. gravitational potential energy (G.P.E) which is the kind we’re interested in.

K.E = \frac {m * v^2} 2
\\~\\
G.P.E = -G*\frac{m_1*m_2} {d}
\\~\\
T.M.E = K.E + G.P.E

As you can probably guess, with stable orbits, the total mecanical energy needs to remain constant. The discussion is what happens with the kinetic and potential energy.
In a circular orbit the kinetic and potential energies are also constant as the speed doesn’t change and neither does the distance between the bodies.
In elliptical orbits they vary depending on the position in the orbit. When closer to eachother, the speed increases and distance decreases whilst when further apart, the speed decreases and distance increases, causing the energies to vary accordingly. As mentioned before however, the sum is still constant. If you will, imagine the body trading kinetic energy (speed in other words) for potential energy (distance) as it moves away and the other way around as it approaches.

Given all of the things mentioned so far, we now have all the tools needed to build basic solar systems with stars and planets and moons, all orbiting eachother nicely.