好的,下面是一个使用 Python 和 Pygame 库编写的程序框架,用于演示一个球在旋转的六边形内弹跳的过程。这个程序包含了重力和简化的摩擦力,并尝试模拟球与旋转墙壁的碰撞。
请注意: 编写一个完全逼真的物理模拟是相当复杂的,尤其是在处理旋转物体碰撞时。下面的代码提供了一个基本的框架和简化的物理模型。更精确的模拟需要更复杂的碰撞检测和响应算法(例如,考虑碰撞点、物体的转动惯量等)。
要运行此代码,你需要先安装 Pygame 库:
pip install pygame
现在,这是 Python 代码:
import pygame
import sys
import math
# 初始化 Pygame
pygame.init()
# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball in Rotating Hexagon")
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# 物理常数
GRAVITY = 0.5 # 重力加速度
BOUNCE_COEFFICIENT = 0.8 # 弹性系数 (反弹时速度保留的比例)
FRICTION_COEFFICIENT = 0.95 # 摩擦系数 (碰撞后切向速度保留的比例)
# 球的属性
ball_radius = 15
ball_pos = [WIDTH // 2, HEIGHT // 4]
ball_velocity = [5, 0]
# 六边形属性
hexagon_center = (WIDTH // 2, HEIGHT // 2)
hexagon_size = 150 # 从中心到顶点的距离
hexagon_angle = 0 # 初始旋转角度 (弧度)
hexagon_angular_velocity = math.radians(0.5) # 旋转角速度 (弧度/帧)
# 生成六边形的顶点
def get_hexagon_vertices(center_x, center_y, size, angle):
vertices = []
for i in range(6):
vertex_angle = angle + math.radians(60 * i)
x = center_x + size * math.cos(vertex_angle)
y = center_y + size * math.sin(vertex_angle)
vertices.append((x, y))
return vertices
# 游戏主循环
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新球的位置 (应用重力)
ball_velocity[1] += GRAVITY
ball_pos[0] += ball_velocity[0]
ball_pos[1] += ball_velocity[1]
# 更新六边形的旋转角度
hexagon_angle += hexagon_angular_velocity
# 获取当前帧的六边形顶点
hexagon_vertices = get_hexagon_vertices(hexagon_center[0], hexagon_center[1], hexagon_size, hexagon_angle)
# 碰撞检测和响应
# 遍历六边形的每条边
for i in range(6):
p1 = hexagon_vertices[i]
p2 = hexagon_vertices[(i + 1) % 6]
# 计算边的法向量 (指向六边形内部)
edge_vector_x = p2[0] - p1[0]
edge_vector_y = p2[1] - p1[1]
edge_length_sq = edge_vector_x**2 + edge_vector_y**2
if edge_length_sq == 0:
continue
# 标准化边向量
edge_length = math.sqrt(edge_length_sq)
edge_normal_x = -edge_vector_y / edge_length # 法向量的 x 分量
edge_normal_y = edge_vector_x / edge_length # 法向量的 y 分量
# 确保法向量指向内部 (可以根据六边形中心和边的关系来判断)
center_to_edge_x = (p1[0] + p2[0]) / 2 - hexagon_center[0]
center_to_edge_y = (p1[1] + p2[1]) / 2 - hexagon_center[1]
dot_product_center_normal = center_to_edge_x * edge_normal_x + center_to_edge_y * edge_normal_y
if dot_product_center_normal < 0: # 如果法向量指向外部,翻转
edge_normal_x *= -1
edge_normal_y *= -1
# 计算球心到边的投影
ball_to_p1_x = ball_pos[0] - p1[0]
ball_to_p1_y = ball_pos[1] - p1[1]
# 在边向量上的投影长度 (参数 t)
t = (ball_to_p1_x * edge_vector_x + ball_to_p1_y * edge_vector_y) / edge_length_sq
# 将投影点限制在边段内
t = max(0, min(1, t))
# 边的最近点
closest_point_x = p1[0] + t * edge_vector_x
closest_point_y = p1[1] + t * edge_vector_y
# 球心到最近点的距离
dist_x = ball_pos[0] - closest_point_x
dist_y = ball_pos[1] - closest_point_y
distance = math.sqrt(dist_x**2 + dist_y**2)
# 检查碰撞
if distance < ball_radius:
# 碰撞发生
# 将球推出墙壁,防止粘连
overlap = ball_radius - distance
ball_pos[0] += edge_normal_x * overlap
ball_pos[1] += edge_normal_y * overlap
# 计算墙壁在碰撞点的线速度 (由于旋转)
# 碰撞点相对于六边形中心的向量
collision_point_rel_x = closest_point_x - hexagon_center[0]
collision_point_rel_y = closest_point_y - hexagon_center[1]
# 墙壁在碰撞点的速度 (v_wall = omega x r)
wall_velocity_x = -hexagon_angular_velocity * collision_point_rel_y
wall_velocity_y = hexagon_angular_velocity * collision_point_rel_x
# 计算球相对于墙壁的速度
relative_velocity_x = ball_velocity[0] - wall_velocity_x
relative_velocity_y = ball_velocity[1] - wall_velocity_y
# 计算沿法线方向的速度分量
dot_product_normal_vel = relative_velocity_x * edge_normal_x + relative_velocity_y * edge_normal_y
# 沿法线方向的冲量 (简化处理,不考虑质量比)
impulse_normal = -(1 + BOUNCE_COEFFICIENT) * dot_product_normal_vel
# 更新沿法线方向的速度
new_velocity_normal_x = relative_velocity_x + impulse_normal * edge_normal_x
new_velocity_normal_y = relative_velocity_y + impulse_normal * edge_normal_y
# 计算沿切线方向的速度分量
# 边的切向量
edge_tangent_x = edge_vector_x / edge_length
edge_tangent_y = edge_vector_y / edge_length
dot_product_tangent_vel = relative_velocity_x * edge_tangent_x + relative_velocity_y * edge_tangent_y
# 应用摩擦 (减少切向速度)
new_velocity_tangent_x = dot_product_tangent_vel * edge_tangent_x * FRICTION_COEFFICIENT
new_velocity_tangent_y = dot_product_tangent_vel * edge_tangent_y * FRICTION_COEFFICIENT
# 最终碰撞后的球的速度 (相对于地面)
ball_velocity[0] = new_velocity_normal_x + new_velocity_tangent_x + wall_velocity_x
ball_velocity[1] = new_velocity_normal_y + new_velocity_tangent_y + wall_velocity_y
# 绘制
screen.fill(BLACK)
# 绘制六边形
if len(hexagon_vertices) > 1:
pygame.draw.polygon(screen, WHITE, hexagon_vertices, 2) # 绘制边框
# 绘制球
pygame.draw.circle(screen, RED, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
# 更新显示
pygame.display.flip()
# 控制帧率
clock.tick(60)
pygame.quit()
sys.exit()
代码解释:
- 初始化和设置: 设置 Pygame 环境、屏幕大小和颜色。
- 物理常数: 定义重力、弹性系数和摩擦系数。
- 球和六边形属性: 初始化球的位置、速度、半径以及六边形的中心、大小、初始角度和角速度。
get_hexagon_vertices 函数: 根据中心、大小和角度计算六边形在当前帧的顶点坐标。
- 主循环:
- 处理退出事件。
- 更新球的位置: 根据当前速度和重力更新球的位置。
- 更新六边形角度: 根据角速度旋转六边形。
- 碰撞检测和响应:
- 遍历六边形的每条边。
- 计算边的法向量(垂直于边并指向内部)。确保法向量方向正确。
- 找到球心到当前边的最近点。
- 计算球心到最近点的距离。
- 如果距离小于球的半径,则发生碰撞。
- 碰撞响应:
- 将球稍微推出墙壁,防止陷入。
- 计算六边形在碰撞点处的线速度(由于旋转)。这是墙壁的速度。
- 计算球相对于墙壁的速度。
- 将相对速度分解为沿法线和切线方向的分量。
- 沿法线方向应用弹性(根据
BOUNCE_COEFFICIENT 反转并减小速度分量)。
- 沿切线方向应用摩擦(根据
FRICTION_COEFFICIENT 减小速度分量)。
- 将法线和切线方向的新速度分量相加,再加上墙壁的速度,得到球碰撞后的新速度(相对于地面)。
- 绘制: 清屏,绘制旋转的六边形和球。
- 更新显示: 将绘制的内容显示在屏幕上。
- 帧率控制: 使用
clock.tick(60) 控制游戏运行在每秒 60 帧左右。
简化之处和潜在改进:
- 碰撞响应: 这里的碰撞响应是基于球和墙壁的简单模型。更精确的模拟需要考虑动量守恒、能量守恒以及碰撞时球和墙壁的质量、转动惯量等。对于旋转物体,碰撞点的速度计算和冲量应用更为复杂。
- 连续碰撞: 如果球在一次更新中穿过了墙壁,简单的位置校正可能不够。高级的物理引擎使用更复杂的算法(如分离轴定理 SAT)来检测碰撞并在发生时准确计算碰撞点和时间。
- 多边形内部检测: 简单的碰撞检测只考虑了到边的距离。在某些情况下,球可能穿过一个角或同时与多条边发生碰撞。更鲁棒的方法是结合点在多边形内部检测。
- 数值稳定性: 简单的物理积分(如欧拉法)在处理快速移动或高弹性碰撞时可能存在数值不稳定问题,导致球穿过墙壁或抖动。更高级的积分方法(如 Verlet 积分或 Runge-Kutta 方法)可以提高稳定性。
这个程序提供了一个基础框架,你可以基于它进一步改进物理模拟,使其更加逼真。