about summary refs log tree commit diff
path: root/simulation.py
diff options
context:
space:
mode:
authorBaitinq <you@example.com>2022-02-06 22:52:22 +0000
committerBaitinq <you@example.com>2022-02-06 22:52:22 +0000
commit521da01f6da6c32ccd834c20b7ea457a619361c8 (patch)
tree6f95f89ac0369f4c0e85e6b2a9b6f9c0a1f67008 /simulation.py
downloadOSLS-521da01f6da6c32ccd834c20b7ea457a619361c8.tar.gz
OSLS-521da01f6da6c32ccd834c20b7ea457a619361c8.tar.bz2
OSLS-521da01f6da6c32ccd834c20b7ea457a619361c8.zip
Initial Commit
Diffstat (limited to 'simulation.py')
-rw-r--r--simulation.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/simulation.py b/simulation.py
new file mode 100644
index 0000000..a93a263
--- /dev/null
+++ b/simulation.py
@@ -0,0 +1,85 @@
+from dataclasses import dataclass
+
+from universe import Universe
+from body import Body
+from rocket import Rocket
+
+@dataclass
+class Simulation_Snapshot:
+    universe: type[Universe]
+    body: type[Body]
+    rocket: type[Rocket]
+
+class Simulation():
+    def __init__(self, universe: type[Universe], body: type[Body], rocket: type[Rocket]):
+        self.ticks = 0
+        self.time = 0
+        self.universe = universe
+        self.body = body
+        self.rocket = rocket
+        self.y = 0
+        self.speed_y = 0
+
+    #simulation logic
+    def tick(self, delta: int) -> None:
+        #calculate upwards force by fuel       
+        # TODO able to turn engine on and off 
+        fuel_used = self.rocket.total_fuel_used(delta)
+        if self.rocket.fuel_mass < fuel_used:
+            fuel_used = self.rocket.fuel_mass
+        self.rocket.fuel_mass -= fuel_used
+        print("Fuel remaining: " + str(self.rocket.fuel_mass))
+        
+        #upwards_force = fuel_used * self.rocket.fuel_type.energy_density #we should calculate thrust based on this
+        upwards_force = 0
+        if fuel_used > 0:
+            upwards_force = self.rocket.total_thrust()
+        print("Upwards force: " + str(upwards_force))
+
+        print("g: " + str(self.body.g(G=self.universe.G, height=self.y)))
+
+        #calculate downwards force by drag and gravity
+        gravitational_force = self.body.g(G=self.universe.G, height=self.y) * self.rocket.total_mass()
+        print("Gravity: " + str(gravitational_force))
+
+        print("BODY MASS: " + str(self.body.mass()))
+        print("ROCKET TOTAL MASS: " + str(self.rocket.total_mass()))
+
+        print("Atmosphere density: " + str(self.body.atmosphere.density_at_height(self.y, self.body.g(G=self.universe.G, height=self.y))))
+
+        #https://www.grc.nasa.gov/www/k-12/airplane/drageq.html
+        drag_force = (1/2) * self.body.atmosphere.density_at_height(self.y, self.body.g(G=self.universe.G, height=self.y)) * (self.speed_y ** 2) * self.rocket.drag_coefficient * self.rocket.cross_sectional_area
+        print("Drag: " + str(drag_force)) #drag can be negative too?
+
+        downwards_force = gravitational_force + drag_force #shouldnt delta influence, TODO: WAIT DRAG COULD BE POSITIVE OR NEGATIVE
+        print("Downwards force: " + str(downwards_force))
+
+        #update velocity based on resultant force
+        total_force = upwards_force - downwards_force
+        print("Total force: " + str(total_force))
+
+        acceleration = total_force / self.rocket.total_mass() #mayb we need momentum??
+        print("Acceleration: " + str(acceleration))
+        self.speed_y = self.speed_y + (acceleration * delta) #i think thir swrong
+
+        #update position based on velocity and delta
+        self.y += self.speed_y * delta
+        if self.y < 0:
+            self.y = 0
+            self.speed_y = 0
+            
+        print("Speed: " + str(self.speed_y))
+        print("Height: " + str(self.y))
+
+        print("")
+
+        self.ticks += 1
+        self.time += delta
+
+    def snapshot(self) -> Simulation_Snapshot:
+        return Simulation_Snapshot(self.universe, self.body, self.rocket)
+
+    def str_snapshot(self) -> str:
+        return str(self.universe) + "\n" + \
+               str(self.body) + "\n" + \
+               str(self.rocket)
\ No newline at end of file