Creating a more realistic black hole simulation involves incorporating gravitational physics, tidal forces (leading to spaghettification), and allowing user interaction to modify the black hole's strength dynamically. Below is an enhanced version of your initial Pygame simulation that includes these features:
- Gravitational Influence: Particles (representing matter) are influenced by the black hole's gravity, altering their trajectories.
- Spaghettification: As particles approach the black hole, they experience tidal forces that stretch them along the direction of gravity.
- Accretion Disk: A visually appealing accretion disk around the black hole is added.
- User Control: Users can modify the black hole's mass (strength) in real-time using keyboard inputs.
Enhanced Black Hole Simulation
'''
Enhanced Black Hole Simulation with Gravity, Spaghettification, and Adjustable Strength
'''
import pygame
import random
import math
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
ACC_DISK_COLOR = (255, 140, 0) # Orange for accretion disk
FPS = 60
# Black Hole Parameters
BLACK_HOLE_POS = (WIDTH // 2, HEIGHT // 2)
BLACK_HOLE_MASS = 1000 # Adjustable mass
G = 6.67430e-1 # Gravitational constant (scaled for simulation)
# Spaghettification Parameters
SPAG_SCALE = 0.1 # Scale of stretching effect
# Initialize Screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Enhanced Black Hole Simulation")
clock = pygame.time.Clock()
# Particle Class
class Particle:
def __init__(self, x, y, color, radius=3):
self.x = x
self.y = y
self.radius = radius
self.color = color
# Assign random initial velocity
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(1, 3)
self.vx = speed * math.cos(angle)
self.vy = speed * math.sin(angle)
self.length = 1 # For spaghettification effect
def update(self, black_hole_mass):
# Calculate distance to black hole
dx = BLACK_HOLE_POS[0] - self.x
dy = BLACK_HOLE_POS[1] - self.y
distance_sq = dx**2 + dy**2
distance = math.sqrt(distance_sq)
if distance < 5:
# Avoid singularity
distance = 5
distance_sq = 25
# Gravitational force magnitude
force = G * black_hole_mass / distance_sq
# Compute force components
fx = force * dx / distance
fy = force * dy / distance
# Update velocity
self.vx += fx / 100 # Divided by arbitrary value to scale acceleration
self.vy += fy / 100
# Update position
self.x += self.vx
self.y += self.vy
# Spaghettification effect: stretch particle based on proximity
self.length = max(1, (black_hole_mass / distance_sq) * SPAG_SCALE)
def draw(self, surface):
# Draw stretched particle as a line to represent spaghettification
end_x = self.x + self.vx * self.length
end_y = self.y + self.vy * self.length
pygame.draw.line(surface, self.color, (self.x, self.y), (end_x, end_y), 2)
# Optionally, draw a small circle at the base
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 2)
# Accretion Disk Class
class AccretionDisk:
def __init__(self, pos, inner_radius, outer_radius, color):
self.pos = pos
self.inner_radius = inner_radius
self.outer_radius = outer_radius
self.color = color
def draw(self, surface):
for i in range(self.inner_radius, self.outer_radius, 2):
alpha = int(255 * (1 - i / self.outer_radius))
disk_color = (*self.color, alpha)
pygame.draw.circle(surface, self.color, self.pos, i, 1)
# Function to Create Particles
def create_particles(num_particles):
particles = []
for _ in range(num_particles):
# Spawn particles in a ring around the black hole
angle = random.uniform(0, 2 * math.pi)
radius = random.uniform(100, 250)
x = BLACK_HOLE_POS[0] + radius * math.cos(angle)
y = BLACK_HOLE_POS[1] + radius * math.sin(angle)
color = (255, random.randint(100, 255), random.randint(50, 150))
particle = Particle(x, y, color)
particles.append(particle)
return particles
# Initialize Particles
particles = create_particles(200)
# Initialize Accretion Disk
accretion_disk = AccretionDisk(BLACK_HOLE_POS, 80, 150, ACC_DISK_COLOR)
# Font for Display
font = pygame.font.SysFont(None, 24)
# Main Loop
running = True
while running:
clock.tick(FPS)
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Adjust black hole mass with keyboard
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
BLACK_HOLE_MASS += 100
elif event.key == pygame.K_DOWN:
BLACK_HOLE_MASS = max(100, BLACK_HOLE_MASS - 100)
# Update Particles
for particle in particles[:]:
particle.update(BLACK_HOLE_MASS)
# Remove particles that pass too close (simulating absorption)
if math.hypot(particle.x - BLACK_HOLE_POS[0], particle.y - BLACK_HOLE_POS[1]) < 10:
particles.remove(particle)
# Optionally, respawn particles to maintain count
particles.append(create_particles(1)[0])
# Drawing
screen.fill(BLACK)
# Draw Accretion Disk
accretion_disk.draw(screen)
# Draw Black Hole (as a filled circle)
pygame.draw.circle(screen, WHITE, BLACK_HOLE_POS, 15)
# Draw Particles
for particle in particles:
particle.draw(screen)
# Display Black Hole Mass
mass_text = font.render(f'Black Hole Mass: {BLACK_HOLE_MASS}', True, WHITE)
screen.blit(mass_text, (10, 10))
instructions = font.render('Use UP/DOWN arrows to adjust mass', True, WHITE)
screen.blit(instructions, (10, 30))
# Update Display
pygame.display.flip()
# Quit Pygame
pygame.quit()
sys.exit()
Key Enhancements Explained
-
Gravitational Influence:
- Each
Particle now calculates the gravitational force exerted by the black hole based on Newton's law of universal gravitation.
- The gravitational constant
G is scaled appropriately for the simulation.
- Particles accelerate towards the black hole, altering their velocities and trajectories over time.
-
Spaghettification Effect:
- As particles approach the black hole, they experience tidal forces that stretch them along the direction of gravity.
- This is visually represented by drawing each particle as a line whose length increases as the particle gets closer to the black hole.
- The
self.length attribute in the Particle class determines the stretching based on proximity.
-
Accretion Disk:
- An
AccretionDisk class is added to render a visually appealing disk around the black hole.
- The disk is drawn as concentric circles with varying opacities to simulate the glowing effect of hot matter spiraling into the black hole.
-
Adjustable Black Hole Strength:
- The black hole's mass (
BLACK_HOLE_MASS) determines the strength of its gravitational pull.
- Users can increase or decrease the mass in real-time using the UP and DOWN arrow keys.
- The current mass is displayed on the screen for reference.
-
Particle Management:
- Particles that fall too close to the black hole (within a certain radius) are removed from the simulation, simulating being absorbed.
- To maintain a consistent number of particles, new particles are spawned as others are absorbed.
-
Visual Enhancements:
- Particles are given varying colors to enhance the visual richness of the simulation.
- The black hole is represented as a solid white circle at the center.
- The accretion disk adds a layer of realism by showing the orbiting matter around the black hole.
How to Modify Black Hole Strength
- Increase Mass: Press the UP arrow key to increase the black hole's mass by 100 units.
- Decrease Mass: Press the DOWN arrow key to decrease the black hole's mass by 100 units (minimum mass is capped at 100 to prevent unrealistic behavior).
The mass directly influences the gravitational pull exerted on the particles:
- Higher Mass: Stronger gravitational force, more pronounced spaghettification, and particles spiral in faster.
- Lower Mass: Weaker gravitational force, less stretching, and particles maintain more stable orbits.
Running the Simulation
-
Install Pygame: Ensure you have Pygame installed. You can install it using pip:
pip install pygame
-
Run the Script: Save the provided code in a file, for example, black_hole_simulation.py, and run it using Python:
python black_hole_simulation.py
-
Interact: Use the UP and DOWN arrow keys to adjust the black hole's mass and observe how the particles react to the changing gravitational strength.
Additional Enhancements (Optional)
- Gravitational Lensing: Implement visual distortion effects around the black hole to simulate how gravity bends light.
- Event Horizon Visualization: Draw the event horizon with appropriate shading to indicate the boundary beyond which nothing can escape.
- Particle Collision and Merging: Allow particles to collide and merge, adding complexity to the simulation.
- User Interface Improvements: Add sliders or buttons for adjusting parameters instead of using keyboard inputs.
These enhancements can further increase the realism and interactivity of the simulation but may require more advanced programming techniques and optimizations.