I'll create a Python program using pygame that shows two balls bouncing inside intersecting rotating hexagons with physics simulation.
import pygame
import math
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
GRAVITY = 0.3
FRICTION = 0.98
BOUNCE_DAMPING = 0.8
# Colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
class Ball:
def __init__(self, x, y, radius, color):
self.x = x
self.y = y
self.vx = random.uniform(-5, 5)
self.vy = random.uniform(-5, 5)
self.radius = radius
self.color = color
def update(self):
# Apply gravity
self.vy += GRAVITY
# Apply friction
self.vx *= FRICTION
self.vy *= FRICTION
# Update position
self.x += self.vx
self.y += self.vy
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
class Hexagon:
def __init__(self, center_x, center_y, radius, rotation_speed):
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.rotation_speed = rotation_speed
self.angle = 0
def get_vertices(self):
vertices = []
for i in range(6):
angle = self.angle + i * math.pi / 3
x = self.center_x + self.radius * math.cos(angle)
y = self.center_y + self.radius * math.sin(angle)
vertices.append((x, y))
return vertices
def get_edges(self):
vertices = self.get_vertices()
edges = []
for i in range(6):
start = vertices[i]
end = vertices[(i + 1) % 6]
edges.append((start, end))
return edges
def update(self):
self.angle += self.rotation_speed
def draw(self, screen, color):
vertices = self.get_vertices()
pygame.draw.polygon(screen, color, vertices, 3)
def point_to_line_distance(px, py, x1, y1, x2, y2):
"""Calculate the shortest distance from a point to a line segment"""
A = px - x1
B = py - y1
C = x2 - x1
D = y2 - y1
dot = A * C + B * D
len_sq = C * C + D * D
if len_sq == 0:
return math.sqrt(A * A + B * B)
param = dot / len_sq
if param < 0:
xx, yy = x1, y1
elif param > 1:
xx, yy = x2, y2
else:
xx = x1 + param * C
yy = y1 + param * D
dx = px - xx
dy = py - yy
return math.sqrt(dx * dx + dy * dy), xx, yy
def get_line_normal(x1, y1, x2, y2):
"""Get the normal vector of a line"""
dx = x2 - x1
dy = y2 - y1
length = math.sqrt(dx * dx + dy * dy)
if length == 0:
return 0, 0
# Return perpendicular vector (rotated 90 degrees)
return -dy / length, dx / length
def check_collision_with_hexagons(ball, hexagons):
"""Check collision between ball and hexagon edges"""
for hexagon in hexagons:
edges = hexagon.get_edges()
for edge in edges:
x1, y1 = edge[0]
x2, y2 = edge[1]
distance, closest_x, closest_y = point_to_line_distance(ball.x, ball.y, x1, y1, x2, y2)
if distance < ball.radius:
# Collision detected
# Get normal vector
nx, ny = get_line_normal(x1, y1, x2, y2)
# Determine which side of the line the ball is on
cross = (ball.x - x1) * (y2 - y1) - (ball.y - y1) * (x2 - x1)
if cross < 0:
nx, ny = -nx, -ny
# Move ball out of collision
overlap = ball.radius - distance
ball.x += nx * overlap
ball.y += ny * overlap
# Reflect velocity
dot_product = ball.vx * nx + ball.vy * ny
ball.vx -= 2 * dot_product * nx * BOUNCE_DAMPING
ball.vy -= 2 * dot_product * ny * BOUNCE_DAMPING
def main():
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncing Balls in Rotating Hexagons")
clock = pygame.time.Clock()
# Create balls
red_ball = Ball(WIDTH // 2 - 50, HEIGHT // 2, 15, RED)
blue_ball = Ball(WIDTH // 2 + 50, HEIGHT // 2, 15, BLUE)
balls = [red_ball, blue_ball]
# Create hexagons
hexagon1 = Hexagon(WIDTH // 2 - 80, HEIGHT // 2, 150, 0.02)
hexagon2 = Hexagon(WIDTH // 2 + 80, HEIGHT // 2, 150, -0.015)
hexagons = [hexagon1, hexagon2]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# Reset balls
for ball in balls:
ball.x = WIDTH // 2 + random.randint(-100, 100)
ball.y = HEIGHT // 2 + random.randint(-100, 100)
ball.vx = random.uniform(-8, 8)
ball.vy = random.uniform(-8, 8)
# Clear screen
screen.fill(BLACK)
# Update hexagons
for hexagon in hexagons:
hexagon.update()
# Update balls
for ball in balls:
ball.update()
# Check collisions with hexagons
check_collision_with_hexagons(ball, hexagons)
# Keep balls roughly in screen bounds (soft constraint)
if ball.x < 0 or ball.x > WIDTH:
ball.vx *= -0.5
ball.x = max(ball.radius, min(WIDTH - ball.radius, ball.x))
if ball.y < 0 or ball.y > HEIGHT:
ball.vy *= -0.5
ball.y = max(ball.radius, min(HEIGHT - ball.radius, ball.y))
# Draw hexagons
hexagon1.draw(screen, GREEN)
hexagon2.draw(screen, WHITE)
# Draw balls
for ball in balls:
ball.draw(screen)
# Draw instructions
font = pygame.font.Font(None, 36)
text = font.render("Press SPACE to reset balls", True, WHITE)
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
main()
This program creates a physics simulation with the following features:
Key Components:
- Two balls (red and blue) with realistic physics
- Two intersecting hexagons that rotate at different speeds
- Gravity pulls the balls downward
- Friction gradually slows the balls down
- Collision detection with the rotating hexagon walls
Physics Implementation:
- Gravity: Constant downward acceleration
- Friction: Velocity is multiplied by a friction coefficient each frame
- Collision Detection: Uses point-to-line distance calculations to detect when balls hit hexagon edges
- Bounce Physics: Reflects velocity based on the surface normal with damping
Controls:
- Press SPACE to reset the balls to new random positions and velocities
- Close the window to exit
How it Works:
- The hexagons rotate continuously at different speeds, creating a dynamic boundary
- Balls bounce off the hexagon walls using proper reflection physics
- The collision system calculates the normal vector of each hexagon edge and reflects the ball's velocity accordingly
- Gravity and friction create realistic movement patterns
The balls will bounce around inside the intersecting hexagonal boundaries, creating an interesting visual effect as the walls continuously rotate around them.