blackholesarecool
- 14
- 2
- TL;DR Summary
- i simulated relativity, specifically special relativity, in python
[CODE lang="python" title="relativity simulator"]import pygame
# Initialize Pygame
pygame.init()
# Get the screen resolution
info = pygame.display.Info()
WIDTH, HEIGHT = info.current_w, info.current_h # Set to the current screen size
# Constants
WHITE = (255, 255, 255)
LIGHT_COLOR = (255, 255, 100)
OBJECT_COLOR = (100, 255, 100)
DETECTOR_COLOR = (255, 100, 100)
GLOW_COLOR = (255, 200, 100)
LIGHT_SPEED = 1 # Light speed
OBJECT_SPEED = 2
FADE_DURATION = 20 # Duration in terms of frames for light to fully fade away
# Setup screen with the maximum resolution
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE) # Allow resizing
clock = pygame.time.Clock()
# Object properties
object_x = 0
object_y = 100
# Detector properties (single point)
detector_x = WIDTH // 2
detector_y = HEIGHT - 50
# Light list (stores [x, y, angle, emitted_time, intensity, emitted_x, emitted_y])
lights = []
glows = [] # Stores glow positions and intensity
running = True
while running:
screen.fill((0, 0, 0))
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
WIDTH, HEIGHT = event.w, event.h # Update width & height on resize
detector_x = WIDTH // 2
detector_y = HEIGHT - 50 # Keep detector at bottom
# Move object (does NOT wrap around)
object_x += OBJECT_SPEED # Moves infinitely off-screen if not resized
# Emit light aimed at the detector
dx = detector_x - object_x
dy = detector_y - object_y
angle = pygame.math.Vector2(dx, dy).normalize() # Normalize direction
lights.append([object_x, object_y, angle, 0, 255, object_x, object_y]) # Store emitted position (object position when light emitted)
# Move lights
for light in lights[:]: # Iterate over a copy of the list to avoid modifying while looping
# Update position
light[0] += light[2].x * LIGHT_SPEED
light[1] += light[2].y * LIGHT_SPEED
# Increase emitted_time to track how many frames the light has existed
light[3] += 0.02
# Calculate intensity based on how long the light has been traveling
light[4] = max(0, 255 - int((light[3] / FADE_DURATION) * 255)) # Fade intensity over time
# Check if the light is below the detector and add glow
if light[1] >= detector_y: # Light has passed or reached below the detector
glows.append([light[5], light[6], light[4]]) # Store the emitted position as the glow's position
lights.remove(light) # Remove the light after it passes the detector
# Draw object (if visible on screen)
if 0 <= object_x <= WIDTH:
pygame.draw.circle(screen, OBJECT_COLOR, (int(object_x), int(object_y)), 10)
# Draw lights (if visible on screen)
for light in lights:
if 0 <= light[0] <= WIDTH and 0 <= light[1] <= HEIGHT:
# Apply fading effect by modifying color intensity
faded_color = (
max(0, LIGHT_COLOR[0] * light[4] // 255),
max(0, LIGHT_COLOR[1] * light[4] // 255),
max(0, LIGHT_COLOR[2] * light[4] // 255),
)
pygame.draw.circle(screen, faded_color, (int(light[0]), int(light[1])), 5)
# Draw detector (single point)
pygame.draw.circle(screen, DETECTOR_COLOR, (detector_x, detector_y), 5)
# Draw glows (lasts only one frame) with intensity
for glow in glows:
glow_x, glow_y, intensity = glow
# Use intensity to modify the brightness of the glow
faded_glow_color = (
max(0, GLOW_COLOR[0] * intensity // 255),
max(0, GLOW_COLOR[1] * intensity // 255),
max(0, GLOW_COLOR[2] * intensity // 255),
)
pygame.draw.circle(screen, faded_glow_color, (int(glow_x), int(glow_y)), 10)
glows.clear() # Remove glows after one frame
pygame.display.flip()
clock.tick(60)
pygame.quit()
[/CODE]
# Initialize Pygame
pygame.init()
# Get the screen resolution
info = pygame.display.Info()
WIDTH, HEIGHT = info.current_w, info.current_h # Set to the current screen size
# Constants
WHITE = (255, 255, 255)
LIGHT_COLOR = (255, 255, 100)
OBJECT_COLOR = (100, 255, 100)
DETECTOR_COLOR = (255, 100, 100)
GLOW_COLOR = (255, 200, 100)
LIGHT_SPEED = 1 # Light speed
OBJECT_SPEED = 2
FADE_DURATION = 20 # Duration in terms of frames for light to fully fade away
# Setup screen with the maximum resolution
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE) # Allow resizing
clock = pygame.time.Clock()
# Object properties
object_x = 0
object_y = 100
# Detector properties (single point)
detector_x = WIDTH // 2
detector_y = HEIGHT - 50
# Light list (stores [x, y, angle, emitted_time, intensity, emitted_x, emitted_y])
lights = []
glows = [] # Stores glow positions and intensity
running = True
while running:
screen.fill((0, 0, 0))
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
WIDTH, HEIGHT = event.w, event.h # Update width & height on resize
detector_x = WIDTH // 2
detector_y = HEIGHT - 50 # Keep detector at bottom
# Move object (does NOT wrap around)
object_x += OBJECT_SPEED # Moves infinitely off-screen if not resized
# Emit light aimed at the detector
dx = detector_x - object_x
dy = detector_y - object_y
angle = pygame.math.Vector2(dx, dy).normalize() # Normalize direction
lights.append([object_x, object_y, angle, 0, 255, object_x, object_y]) # Store emitted position (object position when light emitted)
# Move lights
for light in lights[:]: # Iterate over a copy of the list to avoid modifying while looping
# Update position
light[0] += light[2].x * LIGHT_SPEED
light[1] += light[2].y * LIGHT_SPEED
# Increase emitted_time to track how many frames the light has existed
light[3] += 0.02
# Calculate intensity based on how long the light has been traveling
light[4] = max(0, 255 - int((light[3] / FADE_DURATION) * 255)) # Fade intensity over time
# Check if the light is below the detector and add glow
if light[1] >= detector_y: # Light has passed or reached below the detector
glows.append([light[5], light[6], light[4]]) # Store the emitted position as the glow's position
lights.remove(light) # Remove the light after it passes the detector
# Draw object (if visible on screen)
if 0 <= object_x <= WIDTH:
pygame.draw.circle(screen, OBJECT_COLOR, (int(object_x), int(object_y)), 10)
# Draw lights (if visible on screen)
for light in lights:
if 0 <= light[0] <= WIDTH and 0 <= light[1] <= HEIGHT:
# Apply fading effect by modifying color intensity
faded_color = (
max(0, LIGHT_COLOR[0] * light[4] // 255),
max(0, LIGHT_COLOR[1] * light[4] // 255),
max(0, LIGHT_COLOR[2] * light[4] // 255),
)
pygame.draw.circle(screen, faded_color, (int(light[0]), int(light[1])), 5)
# Draw detector (single point)
pygame.draw.circle(screen, DETECTOR_COLOR, (detector_x, detector_y), 5)
# Draw glows (lasts only one frame) with intensity
for glow in glows:
glow_x, glow_y, intensity = glow
# Use intensity to modify the brightness of the glow
faded_glow_color = (
max(0, GLOW_COLOR[0] * intensity // 255),
max(0, GLOW_COLOR[1] * intensity // 255),
max(0, GLOW_COLOR[2] * intensity // 255),
)
pygame.draw.circle(screen, faded_glow_color, (int(glow_x), int(glow_y)), 10)
glows.clear() # Remove glows after one frame
pygame.display.flip()
clock.tick(60)
pygame.quit()
[/CODE]