write a Python program that shows two balls (red & blue) bouncing inside a spinning two hexagons intersecting in each other. The ball should be affected by gravity and friction, and it must bounce off the rotating walls re
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python program using Pygame to simulate two balls (red and blue) bouncing inside a pair of intersecting, rotating hexagons. The balls are affected by gravity, friction, and bounce off the rotating walls.
✅ Features:
Two rotating hexagons intersecting each other.
Two balls (red and blue) that bounce inside the hexagons.
Gravity pulls the balls downward.
Friction reduces the balls' velocity over time.
Collision detection with the edges of the hexagons.
🧠 Key Concepts:
Hexagon Rotation: Each hexagon rotates around its center.
Collision Detection: For each ball, check if it intersects with the edges of the hexagons.
Reflection: Adjust the ball's velocity based on the normal vector of the edge it collides with.
Friction & Gravity: Applied to the ball's velocity in each frame.
🧾 Code:
python
import pygame
import math
import sys
pygame.init()
# Screen setup
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Bouncing Balls in Rotating Hexagons")
clock = pygame.time.Clock()
# Constants
GRAVITY = 0.5
FRICTION = 0.99
RESTITUTION = 0.8
BALL_RADIUS = 10
# Colors
RED = (255, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
# Hexagon class
class Hexagon:
def __init__(self, center_x, center_y, radius, rotation_angle, angular_velocity):
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.rotation_angle = rotation_angle
self.angular_velocity = angular_velocity
def get_vertices(self):
vertices = []
for i in range(6):
angle = 2 * math.pi * i / 6 + self.rotation_angle
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):
a = vertices[i]
b = vertices[(i + 1) % 6]
edges.append((a, b))
return edges
# Ball class
class Ball:
def __init__(self, x, y, vx, vy, radius, color):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.radius = radius
self.color = color
def update(self):
self.vx *= FRICTION
self.vy *= FRICTION
self.vy += GRAVITY
self.x += self.vx
self.y += self.vy
def draw(self, surface):
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)
# Helper functions
def closest_point_on_segment(A, B, C):
ax, ay = A
bx, by = B
cx, cy = C
abx = bx - ax
aby = by - ay
acx = cx - ax
acy = cy - ay
dot_product = acx * abx + acy * aby
ab_len_squared = abx**2 + aby**2
if ab_len_squared == 0:
return A
t = dot_product / ab_len_squared
t = max(0, min(1, t))
px = ax + t * abx
py = ay + t * aby
return (px, py)
def check_collision(ball, segment):
A, B = segment
closest = closest_point_on_segment(A, B, (ball.x, ball.y))
dx = ball.x - closest[0]
dy = ball.y - closest[1]
distance = math.hypot(dx, dy)
if distance < ball.radius:
normal_x = dx / distance
normal_y = dy / distance
# Adjust ball position to be on the edge
ball.x = closest[0] + normal_x * ball.radius
ball.y = closest[1] + normal_y * ball.radius
# Reflect velocity
dot = ball.vx * normal_x + ball.vy * normal_y
ball.vx -= (1 + RESTITUTION) * dot * normal_x
ball.vy -= (1 + RESTITUTION) * dot * normal_y
return True
return False
# Initialize hexagons
center_x, center_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
hex1 = Hexagon(center_x, center_y, 200, 0, 0.01)
hex2 = Hexagon(center_x, center_y, 200, math.pi / 6, -0.01)
# Initialize balls
ball1 = Ball(400, 300, 2, 2, BALL_RADIUS, RED)
ball2 = Ball(400, 300, -2, -2, BALL_RADIUS, BLUE)
# Main loop
running = True
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update hexagons
hex1.rotation_angle += hex1.angular_velocity
hex2.rotation_angle += hex2.angular_velocity
# Get edges
edges = []
edges.extend(hex1.get_edges())
edges.extend(hex2.get_edges())
# Update balls
ball1.update()
ball2.update()
# Check collisions
for ball in [ball1, ball2]:
for edge in edges:
check_collision(ball, edge)
# Draw hexagons
for hexagon in [hex1, hex2]:
vertices = hexagon.get_vertices()
pygame.draw.polygon(screen, WHITE, vertices, 2)
# Draw balls
ball1.draw(screen)
ball2.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
📌 How to Run:
Install Pygame:
bash
pip install pygame
Save the code to a file, e.g., hexagon_balls.py.
Run the script:
bash
python hexagon_balls.py
🎮 Controls:
The simulation runs automatically.
You can close the window by clicking the close button.
🧪 Notes:
The hexagons rotate in opposite directions for visual interest.
The balls bounce elastically with a coefficient of restitution of 0.8.
Friction and gravity are applied every frame.
This program provides a fun and interactive visualization of physics in action! 🎉