├── .idea ├── .gitignore ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── rocket_simulation.iml └── vcs.xml ├── README.md ├── background.jpg ├── jupiter.png └── main.py /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/rocket_simulation.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | https://github.com/user-attachments/assets/fbe36a0d-df1c-44af-b9a0-4c8a982aeb63 2 | # Gravitational Slingshot Effect Simulation 3 | This is a gravity-based space simulation where you control the trajectory of a spacecraft by tinkering with the gravitational force of a planet. The game allows you to visualize the gravitational slingshot effect, which is used in real-world space missions for altering spacecraft velocities. You can adjust the initial velocity and direction of the spacecraft using mouse clicks and observe how gravity affects its motion around the planet. 4 | 5 | 6 | # Features 7 | 1. Realistic Gravity Simulation: The game simulates gravitational forces between a planet and a spacecraft based on Newton's Law of Universal Gravitation. 8 | 2. Customizable Launch Trajectories: Launch the spacecraft in any direction and see how it reacts to the gravitational pull of the planet. 9 | 3. Collision Detection: Spacecraft can collide with the planet or go off the screen. 10 | 4. Dynamic Gameplay: The spacecraft's motion is governed by physics, making each launch unique depending on its velocity and angle. 11 | 12 | # Prerequisites 13 | To run this game, you need to have the following installed on your system: 14 | 15 | 1. Python 3 16 | 2. Pygame library 17 | You can install Pygame using pip: pip install pygame 18 | 19 | 20 | # Installation 21 | 1. Clone or download the repository to your local machine: https://github.com/Hamadabcn/rocket_simulation.git 22 | 2. Ensure that all the image assets (background.jpg and jupiter.png) are in the same directory as the Python script. 23 | 24 | # How to Run 25 | 1. Open a terminal or command prompt. 26 | 2. Navigate to the directory containing the Python script. 27 | 3. Run the game using the following command: python main.py 28 | 29 | # How to Play 30 | 1. Set the Launch Position: Click once anywhere on the screen to set the starting position of the spacecraft. 31 | 2. Set the Launch Direction: Click again to set the trajectory and velocity by clicking a second time where you want the spacecraft to head. 32 | 3. Observe the Gravitational Pull: The spacecraft will move according to the gravitational pull of the planet located at the center of the screen. You can launch multiple spacecrafts and see how each behaves differently. 33 | 4. Collisions and Boundaries: The spacecraft will be removed from the screen if it collides with the planet or exits the screen boundary. 34 | 35 | # Controls 36 | 1. Mouse Click: Set the starting point and direction for the spacecraft. 37 | 2. Close Button: Quit the game. 38 | 39 | # Game Mechanics 40 | 1. Gravitational Force: The spacecraft is pulled towards the planet using the formula: 41 | F=GMm/r^2 42 | where F is the gravitational force, G is the gravitational constant, and r is the distance between the planet and the spacecraft. 43 | 44 | 2. Planet and Spacecraft: The planet is located at the center of the screen, and the spacecraft can be launched from anywhere on the screen with customizable velocities based on the mouse clicks. 45 | 46 | # Customization 47 | You can customize various parameters in the main.py file: 48 | 49 | 1. Gravitational Constant (G): Adjust the gravitational pull by changing the value of G. 50 | 2. Planet Mass (PLANET_MASS): Modify the mass of the planet to change how strongly it attracts the spacecraft. 51 | 3. Ship Mass (SHIP_MASS): Change the spacecraft mass to influence its response to gravitational forces. 52 | 53 | # License 54 | This project is licensed under the MIT License - see the LICENSE file for details. 55 | -------------------------------------------------------------------------------- /background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hamadabcn/rocket_simulation/7bc0c532c9fdbda8c9c84422d5d3a971a776f90f/background.jpg -------------------------------------------------------------------------------- /jupiter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hamadabcn/rocket_simulation/7bc0c532c9fdbda8c9c84422d5d3a971a776f90f/jupiter.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import math 3 | 4 | pygame.init() 5 | 6 | WIDTH, HEIGHT = 1000, 800 7 | win = pygame.display.set_mode((WIDTH, HEIGHT)) 8 | pygame.display.set_caption("Gravitational Slingshot Effect") 9 | 10 | PLANET_MASS = 500 11 | SHIP_MASS = 15 12 | G = 5 13 | FPS = 60 14 | PLANET_SIZE = 50 15 | OBJ_SIZE = 5 16 | VEL_SCALE = 100 17 | 18 | BG = pygame.transform.scale(pygame.image.load("background.jpg"), (WIDTH, HEIGHT)) 19 | PLANET = pygame.transform.scale(pygame.image.load("jupiter.png"),(PLANET_SIZE * 2, PLANET_SIZE * 2)) 20 | 21 | WHITE = (255, 255, 255) 22 | RED = (255, 0, 0) 23 | BLUE = (0, 0, 255) 24 | 25 | class Planet: 26 | def __init__(self, x, y, mass): 27 | self.x = x 28 | self.y = y 29 | self.mass = mass 30 | 31 | def draw(self): 32 | win.blit(PLANET, (self.x - PLANET_SIZE, self.y - PLANET_SIZE)) 33 | 34 | class Spacecraft: 35 | def __init__(self, x, y, vel_x, vel_y, mass): 36 | self.x = x 37 | self.y = y 38 | self.vel_x = vel_x 39 | self.vel_y = vel_y 40 | self.mass = mass 41 | 42 | def move(self, planet=None): 43 | distance = math.sqrt((self.x - planet.x)**2 + (self.y - planet.y)**2) 44 | force = (G * self.mass * planet.mass) / distance **2 45 | 46 | acceleration = force / self.mass 47 | angle = math.atan2(planet.y - self.y, planet.x - self.x) 48 | 49 | acceleration_x = acceleration * math.cos(angle) 50 | acceleration_y = acceleration * math.sin(angle) 51 | 52 | self.vel_x += acceleration_x 53 | self.vel_y += acceleration_y 54 | 55 | self.x += self.vel_x 56 | self.y += self.vel_y 57 | 58 | def draw(self): 59 | pygame.draw.circle(win, RED, (int(self.x), int(self.y)), OBJ_SIZE) 60 | 61 | 62 | def create_ship(location, mouse): 63 | t_x, t_y = location 64 | m_x, m_y = mouse 65 | vel_x = (m_x - t_x) / VEL_SCALE 66 | vel_y = (m_y - t_y) / VEL_SCALE 67 | obj = Spacecraft(t_x, t_y, vel_x, vel_y, SHIP_MASS) 68 | return obj 69 | 70 | def main(): 71 | running = True 72 | clock = pygame.time.Clock() 73 | 74 | planet = Planet(WIDTH // 2, HEIGHT // 2, PLANET_MASS) 75 | objects = [] 76 | temp_obj_pos = None 77 | 78 | while running: 79 | clock.tick(FPS) 80 | 81 | mouse_pos = pygame.mouse.get_pos() 82 | for event in pygame.event.get(): 83 | if event.type == pygame.QUIT: 84 | running = False 85 | 86 | if event.type == pygame.MOUSEBUTTONDOWN: 87 | if temp_obj_pos: 88 | obj = create_ship(temp_obj_pos, mouse_pos) 89 | objects.append(obj) 90 | temp_obj_pos = None 91 | else: 92 | temp_obj_pos = mouse_pos 93 | 94 | win.blit(BG, (0, 0)) 95 | 96 | if temp_obj_pos: 97 | pygame.draw.line(win, WHITE, temp_obj_pos, mouse_pos, 2) 98 | pygame.draw.circle(win, RED, temp_obj_pos, OBJ_SIZE) 99 | 100 | for obj in objects[:]: 101 | obj.draw() 102 | obj.move(planet) 103 | of_screen = obj.x < 0 or obj.x > WIDTH or obj.y < 0 or obj.y > HEIGHT 104 | collided = math.sqrt((obj.x - planet.x)**2 + (obj.y - planet.y)**2) <= PLANET_SIZE 105 | if of_screen or collided: 106 | objects.remove(obj) 107 | 108 | planet.draw() 109 | 110 | pygame.display.update() 111 | 112 | pygame.quit() 113 | 114 | if __name__ == "__main__": 115 | main() 116 | --------------------------------------------------------------------------------