about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <you@example.com>2022-02-12 21:20:37 +0000
committerBaitinq <you@example.com>2022-02-12 21:20:37 +0000
commit5edac23f6ab3f37167c24331ac8b17c673cbf732 (patch)
treea26c8e99040aa1bb8a0fdf223b8c7ae7ed175f84
parentImplemented proper 2d gravity (diff)
downloadOSLS-5edac23f6ab3f37167c24331ac8b17c673cbf732.tar.gz
OSLS-5edac23f6ab3f37167c24331ac8b17c673cbf732.tar.bz2
OSLS-5edac23f6ab3f37167c24331ac8b17c673cbf732.zip
Fixed drag calculation and added orbit info sprite
-rw-r--r--src/gui/minimap_sprite.py27
-rw-r--r--src/main.py8
-rw-r--r--src/simulation.py24
3 files changed, 45 insertions, 14 deletions
diff --git a/src/gui/minimap_sprite.py b/src/gui/minimap_sprite.py
new file mode 100644
index 0000000..fc71f9f
--- /dev/null
+++ b/src/gui/minimap_sprite.py
@@ -0,0 +1,27 @@
+import math
+
+import pygame
+from pygame.locals import *
+
+from simulation import Simulation
+
+class Sprite_Minimap(pygame.sprite.Sprite):
+    def __init__(self, simulation: type[Simulation], radius: int):
+        super().__init__()
+        self.simulation = simulation
+        self.radius = radius
+
+    def update(self, display_width, display_height):
+        self.image = pygame.Surface([self.radius * 2, self.radius * 2], pygame.SRCALPHA)
+
+        #body
+        pygame.draw.circle(self.image, (0, 255, 0), (self.radius, self.radius), self.radius, width=2)
+        
+        #rocket position (TODO, show actual position in relation to surface. allow zoom?)
+        normalized_x = math.sin(math.radians(self.simulation.angle_of_position_with_respect_to_origin())) * (self.radius)
+        normalized_y = math.cos(math.radians(self.simulation.angle_of_position_with_respect_to_origin())) * (self.radius)
+        normalized_rocket_pos = [self.radius + normalized_x, self.radius - normalized_y]
+        pygame.draw.circle(self.image, (255, 0, 0), normalized_rocket_pos, 4)
+
+        self.rect = self.image.get_rect()
+        self.rect.topright = [display_width, 0]
\ No newline at end of file
diff --git a/src/main.py b/src/main.py
index 9942d84..b272571 100644
--- a/src/main.py
+++ b/src/main.py
@@ -16,6 +16,7 @@ from pygame.locals import *
 from gui.background_sprite import Sprite_Background
 from gui.rocket_sprite import Sprite_Rocket
 from gui.body_sprite import Sprite_Body
+from gui.minimap_sprite import Sprite_Minimap
 
 import numpy as np
 from matplotlib import pyplot as plot
@@ -76,8 +77,8 @@ def main(argv):
     pygame.display.set_caption("OSLS - Overly Simple Launch Simulator")
     clock = pygame.time.Clock()
 
-    SCREEN_WIDTH = 1024
-    SCREEN_HEIGHT = 720
+    SCREEN_WIDTH = 800
+    SCREEN_HEIGHT = 940
 
     sprite_group = pygame.sprite.Group()
 
@@ -87,7 +88,8 @@ def main(argv):
 
     sprite_group.add(Sprite_Background(simulation),
                      Sprite_Body(simulation, floor), 
-                     Sprite_Rocket(simulation, floor))
+                     Sprite_Rocket(simulation, floor),
+                     Sprite_Minimap(simulation, 100))
 
     simulation_display = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
     plot_coords = []
diff --git a/src/simulation.py b/src/simulation.py
index 2830769..99ac043 100644
--- a/src/simulation.py
+++ b/src/simulation.py
@@ -59,15 +59,10 @@ class Simulation():
         total_gravitational_force = g * self.rocket.total_mass()
         print("Total Gravity: " + str(total_gravitational_force))
 
-        ref_vec = (0, 1)
-        pos_vec = (self.x, self.y)
-        dot = (pos_vec[0] * ref_vec[0]) + (pos_vec[1] * ref_vec[1])
-        det = (pos_vec[0] * ref_vec[1]) - (pos_vec[1] * ref_vec[0])
-        angle_of_position_with_respect_to_origin = math.degrees(math.atan2(det, dot))
-        print("angle_of_position_with_respect_to_origin: " + str(angle_of_position_with_respect_to_origin))
+        print("angle_of_position_with_respect_to_origin: " + str(self.angle_of_position_with_respect_to_origin()))
 
-        gravitational_force_x = math.sin(math.radians(angle_of_position_with_respect_to_origin)) * total_gravitational_force
-        gravitational_force_y = math.cos(math.radians(angle_of_position_with_respect_to_origin)) * total_gravitational_force
+        gravitational_force_x = math.sin(math.radians(self.angle_of_position_with_respect_to_origin())) * total_gravitational_force
+        gravitational_force_y = math.cos(math.radians(self.angle_of_position_with_respect_to_origin())) * total_gravitational_force
 
         print("Gravity X: " + str(gravitational_force_x))
 
@@ -83,14 +78,14 @@ class Simulation():
         #TODO: cross sectional area and drag coef for x should b different
         drag_force_x = (1/2) * curr_atmospheric_density * (self.speed_x ** 2) * self.rocket.rocket_x_drag_coefficient() * self.rocket.rocket_x_cross_sectional_area()
         #drag goes against speed
-        if force_x < 0:
+        if self.speed_x < 0:
             drag_force_x *= -1
         print("Drag X: " + str(drag_force_x))
 
         #https://www.grc.nasa.gov/www/k-12/airplane/drageq.html
         drag_force_y = (1/2) * curr_atmospheric_density * (self.speed_y ** 2) * self.rocket.rocket_y_drag_coefficient() * self.rocket.rocket_y_cross_sectional_area()
         #drag goes against speed
-        if force_y < 0:
+        if self.speed_y < 0:
             drag_force_y *= -1
         print("Drag Y: " + str(drag_force_y))
 
@@ -143,7 +138,14 @@ class Simulation():
         #TODO: try and solve it using 2 sqrt instead of having such a big number in parameters which can crass with high timesx
         altitude = math.sqrt(self.x**2 + self.y**2)
         altitude -= self.body.radius
-        return altitude 
+        return altitude
+
+    def angle_of_position_with_respect_to_origin(self):
+        ref_vec = (0, 1)
+        pos_vec = (self.x, self.y)
+        dot = (pos_vec[0] * ref_vec[0]) + (pos_vec[1] * ref_vec[1])
+        det = (pos_vec[0] * ref_vec[1]) - (pos_vec[1] * ref_vec[0])
+        return math.degrees(math.atan2(det, dot))
 
     def snapshot(self) -> Simulation_Snapshot:
         return Simulation_Snapshot(self.universe, self.body, self.rocket)