# Implementing gravitational pull to planets in a 3D scene

for a group project our team is to make a replicated solar system with very basic 3D graphics in a 3D scene/space.
I have been tasked with the job of implementing a method for the planets to have their own gravitational pull, for, if an object were to pass the planets,
their course of movement will be effected by the planets pull.

I’ve done some searching around but have yet to find a definitive answer. How could one go about implementing this method.

Thanks,

SiB

This has nothing to do with OpenGL, which is only a rendering library. You should look into a Physics Engine like Bullet (http://bulletphysics.org/wordpress/) or Open Dynamics Engine (http://www.ode.org/).

That’s a tough one because of scale.

I mean, real gravity is pretty straight forward. Earth pulls you to it’s center at basically 9.8 meters per second every second. In physics, that’s called an acceleration. Wind resistance causes terminal velocity, which you don’t have in outer space. But that 9.8m/s^2 assumes that you are practically already on the surface of the planet. The earth’s atmosphere is a super thin layer compared to the overall size of the planet. And the further you get from the surface the weaker the acceleration.

You could probably implement it by calculating a 3D vector between the planet and your object. That’s just a matter of making the 2 points in 3D space into vectors and then subtracting one from the other. Depending on which order you do it in, will determine which direction the vector points. What you want is a vector that points from the object to the planet. Done. One line of code with GLM.

To determine the actual acceleration, we have to dive deep into physics. Well, first semester physics anyway.

We need the acceleration.

Acceleration = Force/mass

The force of gravity is

Force = 6.674×10−11 N⋅m2/kg2 times (mass of first object times the mass of the second object divided by the square distance between the two objects)

Interesting fact, your gravity pulls the earth towards you. The difference in size between you in the earth is the reason that is inconsequential. Both objects pull on each other but a huge difference in mass can make the end result very one sided.

F = 6.674x10^-11(mxm/d^2)

But we need the acceleration, not the force. So, plugging that in to the previous equation, I believe you get

a=(6.674x10^-11(objectxplanet/d^2))/object

The mass of the object may cancel itself out there, but my algebra is rusty. The answer should be in meters per second the object will accelerate towards the planet every second. It will speed up by that amount.

In space, there will be no terminal velocity. But the distance is constantly changing. Just calculate a new acceleration rate every once in awhile to get a decent approximation. Once per second or less. That 9.8 m/s^2 assumes it doesn’t change at all and that works for most physics problems on earth. But you are talking about distance changes of maybe a billion kilometres away down to 1 kilometre away. THAT will make a difference. But it points out you don’t need to recalculate this value very often to get a good approximation.

At first, the acceleration is going to be minuscule. In a space simulation, I don’t think I would even bother doing it unless you can go from space to the planet’s surface. Which programming that is much more complicated than this.

Let’s run through a quick example. The moon is something like 1/4th the size of the earth, to give you some idea of it’s scale. It appears as small as it does because of its distance. It is roughly 384,400,000 meters from earth. Let’s assume we have a space ship that is that distance from the earth at a complete stand still. Google “earth moon size comparison” to get an idea of how much bigger the earth appears than the moon when standing on the surface of the moon. The earth looks far more than 4 times bigger at the same distance. It would be a very noticeable object in the scene.

Now let’s assume our space ship is 20 tons. That’s 18143.7 kilograms.

There’s a nice handy calculator here. And if I entered all the values correctly the acceleration rate is

0.00270 meters per second every second

I believe that’s 2.7 millimetres per second every second.

You need to accelerate faster than that to escape the earth’s gravity, which won’t take much.

So, first second you are being pulled towards earth at 2.7 mm/sec. After two seconds 5.4 mm/sec. After four seconds you’ll be moving 10.8 mm/sec. After eight seconds you’ll be moving 21.6 mm/sec. After 16 seconds 43.2 mm/sec. After 32 seconds you’ll be moving 86.4 mm/sec. After a minute and 4 seconds you’ll be moving at a whopping 172.8 mm/sec or 0.1728 meters per second.

So, you can see that you are speeding up very rapidly. But after a full minute of speeding up, you’re only moving about 1/5th of a meter per second which is about the speed of maple syrup (although given some time this will become a serious velocity). Ok, maybe it’s kind of runny maple syrup. But it’s still pretty slow, because remember, we have 384,400,000 meters to cross and we’re crossing it after a minute of speeding up at a rate of 0.1728 meters per second. We’ve probably only crossed a few meters in a minute. So now we probably have about 384,399,997 meters left to go! Woohoo! At this rate we’ll get there in another 100 million minutes or so! Let’s see how many minutes in a year? About half a million. So, about 100 years give or take?

Of course, with this constant acceleration it will happen much quicker than 100 years. After the first year you would be moving at a pretty good clip. Without doing the math, I would take a wild guess and imagine it to be less than a few years.

But keep in mind that all this assumes that you don’t try to move the ship. All it takes to stop the acceleration is an acceleration of 2mm/sec in the opposite direction. If you stop it early, the ship’s going nowhere. Once it picks up speed, you have to apply that full velocity in the opposite direction to stop.

Also, notice that the distance changed over a minute, but probably not enough to make an actual noticeable difference in the calculation, especially since I was rounding numbers like crazy in the first place. After a day or two you may want to recalculate the acceleration rate. Eventually the distance will be short enough and the velocity fast enough you may need to recalculate it every minute or so.

If you actually turn the ship’s engines on and the ship is facing mostly away from the earth, the ships engines are probably going to completely undo anything that’s going on here over the first few hours. If the engines have any force at all that is. If you’re using solar sails, maybe not. But actual rocket engines? You’re probably talking acceleration rates of hundreds or thousands of meters per second compared to the acceleration of gravity in the opposite direction of millimetres per second. If you wait a week and let that velocity build up, you may need to leave those engines running for awhile to come to a stop, but in the first hour or two, turning them on for a second or two will probably give you escape velocity and have you permanently out of the gravitational field. Once you reach escape velocity, you’re not coming back due to gravity anyway.

So, I guess part of my point here is that in your game, you’re probably not going to make the space ship full scale. And even if you do, the earth’s scale will be so much larger than the ship’s scale, that it will be impractical to do everything to scale. I think you’re going to have to fudge the scale somewhere.

But when you start running through the numbers you realize that you’re never going to see the scale change. After a day, you look out the window and the earth looks to be the exact same size it was yesterday. If it takes you a year to get there, you’re going to need to take pictures or measurements every week to notice a change. Month to month you’ll probably see the earth get bigger.

I can’t imagine a game where the player just sits there for a month. And if you only need to adjust the model size once a month for the player to simulate a change in distance? None of the other planets in the solar system will appear to have really changed in size during that time when you look out the window.

Now, if you’re mainly talking orbital mechanics applying the gravitational acceleration makes a lot more sense. You’re just going to greatly accelerate the time scale. Each planet will pull the ship in that direction, which you can calculate like I started out saying. You need to re-calc the acceleration rate often since the increased time scale will make you cross vast distances in relatively short amounts of time. And you’ll have to constantly apply the acceleration rate of gravity as a vector against the velocity vector of the ship, which could also be affected by an acceleration vector from the ship’s engines.

[QUOTE=RealSiB;1282650]for a group project our team is to make a replicated solar system with very basic 3D graphics in a 3D scene/space.
I have been tasked with the job of implementing a method for the planets to have their own gravitational pull, for, if an object were to pass the planets,
their course of movement will be effected by the planets pull.

I’ve done some searching around but have yet to find a definitive answer. How could one go about implementing this method.[/QUOTE]

first, you have to decouple the physics and the visual representation, because if you try to render a sphere (r = 1) in about 100million km away from the camera, it will be clipped away
assuming your perspective’s zfar = 100.0f, you have to scale the sphere using:
https://en.wikipedia.org/wiki/Intercept_theorem

scalefactor/ zfar = planetradius / planetdistancetocamera
==> scalefactor = planetradius / planetdistancetocamera * zfar

so you have so use glm::scale(glm::vec3(scalefactor)) as the planets scale matrix
just to put the planet into the perspective …

1 thing that you’ll find out is that space is reeeally big, that said planets will be VERY SMALL

regarding gravitation: (use double instead of float !!!)
you must have all the planets (initial) position, their (initial) velocities (vec3), their radius and their mass, then:

a = F / m, using F = g0 * m1 * m2 / d^2

first calculate all the accelerations (vec3) on each planet (a for loop within a for loop)
then calculate their velocities:

newvelocity = oldvelocity + acceleration * timestep // timestep = usually 1 / 60fps = 0.016

then move them:

newposition = oldposition + velocity * timestep

repeat all …

or another way: scale the whole solar system down to the size of your zfar, scale also their mass / radius down

Hi,

I don’t know if this is still actual, but in case it helps you, I implemented a solar system simulator with OpenGL, using velocity Verlet for ‘molecular dynamics’. I cannot post a link but you could find the blog by searching ‘computational physics blog’ on google.
On the bottom of the page there is a link to GitHub, too. Or you could enter compphys dot go dot ro in the browser.