Code me a Python program that shows a ball bouncing inside a spinning hexagon. The ball should be affected by gravity and friction, and it must bounce off the rotating walls realistically. Dont do internet research to assist you in coding this.
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 that simulates a ball bouncing inside a spinning hexagon, with the ball affected by gravity and friction, and realistically bouncing off the rotating walls.
This simulation uses the Pygame library to handle graphics and physics. If you don't have Pygame installed, install it first:
bash
pip install pygame
✅ Features:
A ball is affected by gravity.
Ball loses energy due to friction (bounces less high over time).
A hexagon spins around its center.
Ball bounces realistically off the rotating walls of the hexagon.
🧠 Physics Overview:
The hexagon rotates at a constant angular velocity.
Each wall is represented as a line segment.
For each frame, we check if the ball collides with any wall.
Upon collision, we reflect the ball’s velocity using vector math.
💻 Code:
python
import pygame
import sys
import math
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 800
FPS = 60
GRAVITY = 0.3
FRICTION = 0.99
BALL_RADIUS = 10
HEX_SIDE = 250
ROTATION_SPEED = 0.01 # radians per frame
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BALL_COLOR = (255, 50, 50)
# Setup screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball Bouncing Inside Spinning Hexagon")
clock = pygame.time.Clock()
# Ball properties
ball_pos = [WIDTH // 2, HEIGHT // 2 - 100]
ball_vel = [2, 0]
def rotate_point(point, angle, origin):
"""Rotate point around origin by angle (in radians)."""
x, y = point
ox, oy = origin
qx = ox + math.cos(angle) * (x - ox) - math.sin(angle) * (y - oy)
qy = oy + math.sin(angle) * (x - ox) + math.cos(angle) * (y - oy)
return [qx, qy]
def get_hexagon_points(center, side_length, angle):
"""Return the 6 vertices of a hexagon centered at center, rotated by angle."""
points = []
for i in range(6):
theta = 2 * math.pi * i / 6
x = center[0] + side_length * math.cos(theta)
y = center[1] + side_length * math.sin(theta)
rotated = rotate_point((x, y), angle, center)
points.append(rotated)
return points
def reflect_velocity(velocity, normal):
"""Reflect velocity vector across a normal vector."""
normal = normal.normalize()
dot = velocity.x * normal.x + velocity.y * normal.y
reflection = pygame.Vector2(
velocity.x - 2 * dot * normal.x,
velocity.y - 2 * dot * normal.y
)
return reflection * FRICTION
def distance_point_to_line_segment(p, a, b):
"""Compute the shortest distance from point p to line segment ab."""
ap = pygame.Vector2(p[0] - a[0], p[1] - a[1])
ab = pygame.Vector2(b[0] - a[0], b[1] - a[1])
t = max(0, min(1, ap.dot(ab) / ab.length_squared()))
closest = pygame.Vector2(a[0] + t * ab.x, a[1] + t * ab.y)
return (p[0] - closest.x)**2 + (p[1] - closest.y)**2, closest
# Main loop
hex_angle = 0
hex_center = [WIDTH // 2, HEIGHT // 2]
running = True
while running:
dt = clock.tick(FPS) / 1000 # Time step
screen.fill(BLACK)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update rotation
hex_angle += ROTATION_SPEED
# Draw hexagon
hex_points = get_hexagon_points(hex_center, HEX_SIDE, hex_angle)
pygame.draw.polygon(screen, WHITE, hex_points, width=2)
# Apply gravity
ball_vel[1] += GRAVITY
# Update ball position
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# Collision detection with hexagon walls
for i in range(6):
a = hex_points[i]
b = hex_points[(i + 1) % 6]
dist_sq, closest = distance_point_to_line_segment(ball_pos, a, b)
if dist_sq < BALL_RADIUS ** 2:
# Compute normal pointing inward
normal = pygame.Vector2(ball_pos[0] - closest.x, ball_pos[1] - closest.y)
if normal.length() == 0:
continue
normal = normal.normalize()
# Move ball out of wall
penetration = BALL_RADIUS - normal.length()
ball_pos[0] += normal.x * penetration
ball_pos[1] += normal.y * penetration
# Reflect velocity
ball_vel_vec = pygame.Vector2(ball_vel[0], ball_vel[1])
reflected = reflect_velocity(ball_vel_vec, normal)
ball_vel[0], ball_vel[1] = reflected.x, reflected.y
# Draw ball
pygame.draw.circle(screen, BALL_COLOR, (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)
# Update display
pygame.display.flip()
pygame.quit()
sys.exit()
🔁 How It Works:
The hexagon rotates by updating the angle each frame.
The ball moves under gravity.
On each frame, we test for collisions between the ball and each edge of the hexagon.
When a collision occurs:
The ball is pushed slightly outward to avoid getting stuck.
Its velocity is reflected based on the surface normal.
Friction reduces speed after each bounce.
🚀 Tips to Try:
Change ROTATION_SPEED to make the hexagon spin faster or slower.
Adjust GRAVITY, FRICTION, or BALL_RADIUS to see how they affect motion.
Add multiple balls for more complex interactions.
Let me know if you'd like enhancements like trails, sound effects, or more realistic physics!