📜 ⬆️ ⬇️

Solar system simulator. The key to start!

In the first part of the simulator, I described the rules of the game and their simplest implementation.

I am grateful to everyone who left constructive comments of the first version. It helped me appreciate the depth of the problem. Special thanks to kahi4 , Ethril , Uri and lexasss users


Now the simulator has grown. All bodies affect each other according to the general rules, the Euler method is a thing of the past, it became possible to choose systems for simulation, etc.
')
It is time to move on - to the conquest of the vastness of the space controlled apparatus.



The trajectory is clear
What is your flight coming to an end
We remember you, grieve and love.
Your PCO.

Bubbles




Our goal is communism. Missile Plant


So, the space systems simulator Spacesim allows you to set the initial state of the system and simulate its behavior.

Imagine now that we have a classic ideal one-stage rocket rocket

Let the rocket has a dry mass m,
mf fuel is poured into it
engine thrust - z
and fuel consumption - n
Let the engine has an unlimited resource and an unlimited number of inclusions.

Suppose that our rocket is on the surface of a planet of mass M and radius H. Our main task will be to remove the rocket into a circular orbit around the planet.

heaven ahead


Before launching into a circular orbit, let's launch our rocket vertically up and watch its movement.
Two forces act on the rocket:
1. Engine thrust (up): F = n * z
2. Planet gravity (down): F = G * M * (m + mf) / r ^ 2, m + mf is the total mass of the rocket, r is the distance from the rocket to the center of the planet.

Then we will assume everywhere G = 1

The resulting force acting on the rocket will be: n * z - G * M * (m + mf) / r ^ 2
From here you can find the acceleration of the rocket:

a = n * z / (m + mf) - M / r ^ 2

Now it is easy to calculate the motion of a rocket vertically upwards under the influence of the planet’s gravity:

Euler method:

H=409.0 #Start level above sea t=0 m=2 #Mass of equipment mf=9 #Mass of Fuel M=600000 #Planet mass y=H + 1 #Initial position a=0 #accel v=0 #speed f=0 #engine accel n=1 #Fuel consumption z=40.0 #Fuel impulse cnt = 0 #Step count dt = .1 maxy = 0 while(y > H and cnt < 300000000): if mf > 0: f = n*z/(m + mf) #Engine gives acceleration to mf -= dt*n #Fuel goes down else: f = 0 #Out of fuel a = f - M/y**2 #Total = engine - gravity v += dt*a #new speed y += dt*v #new altitude maxy = max(maxy, y) print("Step: ", cnt, " Height: ", y, " VSpeed: ", v) cnt += 1 print(dt ,maxy) 


Runge Kutta Method

 H=409.0 #Start level above sea t=0 m=2 #Mass of equipment mf=9 #Mass of Fuel M=600000 #Planet mass x=H + 1 #Initial position a=0 #accel v=0 #speed f=0 #engine accel n=1 #Fuel consumption z=40.0 #Fuel impulse cnt = 0 #Step count dt = .1 maxy = 0 def f(t, x, v): global m,mf,n,z if mf > 0: f = n*z/(m + mf) #Engine gives acceleration to else: f = 0 #Out of fuel a = f - M/x**2 #Total = engine - gravity #We'll use Runge-Kutta method return a #new speed def g(t, x, v): return v while(x > H and cnt < 30000): maxy = max(maxy, x) k1 = dt * f(t, x, v) q1 = dt * g(t, x, v) k2 = dt * f(t + dt/2, x + q1/2, v + k1/2) q2 = dt * g(t + dt/2, x + q1/2, v + k1/2) k3 = dt * f(t + dt/2, x + q2/2, v + k2/2) q3 = dt * g(t + dt/2, x + q2/2, v + k2/2) k4 = dt * f(t + dt, x + q3, v + k3) q4 = dt * g(t + dt, x + q3, v + k3) v1 = v + (k1 + 2*k2 + 2*k3 + k4)/6 x1 = x + (q1 + 2*q2 + 2*q3 + q4)/6 print("Step: ", cnt, " Height: ", x1, " Speed: ", v1) cnt += 1 t += dt v = v1 x = x1 if mf > 0: mf -= dt*n #Fuel goes down print(dt ,maxy) 


As you can see, the results coincide with great accuracy.


Putting it all together

So, we learned how to launch a jet engine and fly up vertically.
We also have a simulator from the last part, in which the bodies move under the action of gravitational forces relative to each other.

Combine them! Add a rocket to the simulator.

Add to the rocket onboard computer, working on the program.
To go into a circular orbit, will act according to this algorithm:

1. We start the engine and the rocket starts to fly upwards.
2. Turn off the engine. Rocket flies up by inertia
3. Rotate the rocket body 90 degrees.
4. At the moment when the vertical speed becomes zero, we turn on the engine
5. After a short period of time, turn off the engine.

This is how the implementation looks like:

 class EarthOrbiter(Rocket): def flightProgram(self): #Take off and turn 90" right if self.mode == 0: self.engineOn() self.mode = 1 if self.t > 12.0 and self.mode == 1: self.engineOff() self.setHead(90) self.mode = 2 #Go to round orbit if self.t > 20 and self.mode == 2: self.engineOn() self.mode = 3 if self.t >= 27 and self.mode == 3: self.engineOff() self.mode = 4 

Events in our rocket occur depending on the flight time and the previous state.

Bingo! The flight is normal. The trajectory is stable.

Here is a short first flight fideo:
Flight video 1
Flight video 2
Fly to the moon!

The next step is to add the moon to the system and fly to it.
There are also multi-stage rockets in the plans.

Sources - here

Update1: flight to the moon added.
Added the possibility of landing rocket planets.

Update2: added ability to dock two spacecraft.

Source: https://habr.com/ru/post/197788/


All Articles