python
import pygame
import math
# --- 常量设置 ---
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 800
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (220, 50, 50)
BLUE = (50, 100, 220)
# --- 物理参数 ---
GRAVITY = 980.0 # 像素/秒^2
ELASTICITY = 0.85 # 碰撞能量保留率 (0-1)
FRICTION = 0.05 # 碰撞时切向速度损失 (0-1)
class Ball:
def __init__(self, x, y, radius):
self.pos = pygame.math.Vector2(x, y)
self.vel = pygame.math.Vector2(0, 0)
self.radius = radius
self.color = RED
def update(self, dt):
# 应用重力
self.vel.y += GRAVITY * dt
# 更新位置
self.pos += self.vel * dt
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.radius)
class RotatingHexagon:
def __init__(self, center_x, center_y, size, rot_speed):
self.center = pygame.math.Vector2(center_x, center_y)
self.size = size # 从中心到顶点的距离
self.rot_speed = rot_speed # 角度/秒
self.angle = 0 # 当前角度
self.vertices = []
self.update_vertices()
def update_vertices(self):
self.vertices = []
for i in range(6):
# 60度一个顶点
angle_rad = math.radians(60 * i + self.angle)
x = self.center.x + self.size * math.cos(angle_rad)
y = self.center.y + self.size * math.sin(angle_rad)
self.vertices.append(pygame.math.Vector2(x, y))
def rotate(self, dt):
self.angle += self.rot_speed * dt
self.angle %= 360
self.update_vertices()
def draw(self, screen):
pygame.draw.polygon(screen, BLUE, self.vertices, 4)
def handle_collision(self, ball):
# 遍历六边形的6条边
for i in range(6):
p1 = self.vertices[i]
p2 = self.vertices[(i + 1) % 6]
# 1. 找到球心到线段的最短距离点
line_vec = p2 - p1
point_vec = ball.pos - p1
# 投影长度比例 t
t = point_vec.dot(line_vec) / line_vec.length_squared()
t = max(0, min(1, t)) # 限制t在[0, 1]之间,确保最近点在线段上
closest_point = p1 + t * line_vec
distance_vec = ball.pos - closest_point
distance = distance_vec.length()
# 2. 检测碰撞
if distance < ball.radius:
# 碰撞发生
# 3. 位置修正 (防止球穿透墙壁)
overlap = ball.radius - distance
ball.pos += distance_vec.normalize() * overlap
# 4. 计算速度响应
# 法线向量 (垂直于墙壁向外)
normal = distance_vec.normalize()
# 计算墙壁在碰撞点的线速度
# 角速度 (弧度/秒)
angular_velocity_rad = math.radians(self.rot_speed)
# 碰撞点到中心的向量
r_vec = closest_point - self.center
# 墙壁的切向速度 v = ω × r
wall_velocity = pygame.math.Vector2(-r_vec.y, r_vec.x) * angular_velocity_rad
# 相对速度
relative_velocity = ball.vel - wall_velocity
# 如果球正在远离墙壁,则不处理 (避免重复碰撞)
if relative_velocity.dot(normal) > 0:
continue
# 沿法线方向的速度分量
v_normal = relative_velocity.dot(normal) * normal
# 沿切线方向的速度分量
v_tangent = relative_velocity - v_normal
# 更新速度
# 法线方向速度反转并乘以弹性系数
# 切线方向速度乘以摩擦力衰减
ball.vel -= (1 + ELASTICITY) * v_normal
ball.vel -= FRICTION * v_tangent
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Bouncing Ball in Rotating Hexagon")
clock = pygame.time.Clock()
# 创建对象
ball = Ball(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2, 15)
hexagon = RotatingHexagon(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 300, 30) # 中心, 大小, 旋转速度(度/秒)
running = True
while running:
# dt: 上一帧所花的时间(秒)
dt = clock.tick(FPS) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# --- 更新 ---
hexagon.rotate(dt)
ball.update(dt)
hexagon.handle_collision(ball)
# --- 绘制 ---
screen.fill(BLACK)
hexagon.draw(screen)
ball.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()