Pymunk是一個用於2D物理模擬的Python庫,它基於Chipmunk物理引擎。Pymunk提供了一種方便的方式來模擬剛體、碰撞和約束,並且可以與Pygame等Python遊戲開發庫結合使用。
安裝:
pip install pymunk
1.模擬物理球體自由跌落
import pygame
import pymunk
import random
pygame.init()
screen_width, screen_height = 600, 600
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = (0, 900)
def create_ball():
mass = 1
radius = 20
x = random.randint(radius, screen_width - radius)
y = random.randint(radius, screen_height - radius)
body = pymunk.Body(mass, pymunk.moment_for_circle(mass, 0, radius))
body.position = x, y
shape = pymunk.Circle(body, radius)
shape.elasticity = 0.95
space.add(body, shape)
for _ inrange(10):
create_ball()
while True:
foreventin pygame.event.get():
ifevent.type == pygame.QUIT:
pygame.quit()
screen.fill((255, 255, 255))
space.step(1/60.0)
for body in space.bodies:
for shape in body.shapes:
ifisinstance(shape, pymunk.Circle):
pos = int(body.position.x), int(body.position.y)
pygame.draw.circle(screen, (0, 0, 0), pos, int(shape.radius))
# 檢測碰撞邊界
if pos[0] < shape.radius or pos[0] > screen_width - shape.radius:
body.velocity = (-body.velocity.x, body.velocity.y)
if pos[1] < shape.radius or pos[1] > screen_height - shape.radius:
body.velocity = (body.velocity.x, -body.velocity.y)
pygame.display.flip()
clock.tick(60)
2.模擬流水
import pygame
import pymunk
# 初始化 Pygame
pygame.init()
# 設定螢幕大小
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Water Effect")
# 顏色
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
# 初始化 Pymunk 空間和重力
space = pymunk.Space()
space.gravity = (0, -1000)
# 建立地面
ground = pymunk.Segment(space.static_body, (0, 50), (WIDTH, 50), 5)
ground.friction = 0.5
space.add(ground)
# 建立水滴粒子
def create_particle(x, y):
body = pymunk.Body(1, 1)
body.position = x, y
shape = pymunk.Circle(body, 2)
shape.elasticity = 0.5
shape.friction = 0.5
space.add(body, shape)
return body
# 遊戲迴圈
running = True
clock = pygame.time.Clock()
while running:
foreventin pygame.event.get():
ifevent.type == pygame.QUIT:
running = False
# 建立新的水滴
mouse_pos = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0]:
create_particle(*mouse_pos)
# 更新物理空間
space.step(1/60)
# 繪制螢幕
screen.fill(WHITE)
for body in space.bodies:
for shape in body.shapes:
ifisinstance(shape, pymunk.Circle):
pos = int(body.position.x), int(HEIGHT - body.position.y)
pygame.draw.circle(screen, BLUE, pos, int(shape.radius), 0)
# 重新整理螢幕
pygame.display.flip()
clock.tick(60)
# 結束 Pygame
pygame.quit()
參數簡介:
品質 (mass):物體的品質,影響其受到的力和加速度。用法:body.mass = 5 # 將物體的品質設定為5
慣性 (moment):物體的慣性,決定了其對旋轉的響應程度。用法:body.moment = 10 # 將物體的慣性設定為10
彈性 (elasticity):用於描述碰撞後能量損失的程度。用法:shape.elasticity = 0.8 # 設定碰撞形狀的彈性為0.8
摩擦系數 (friction):描述兩個物體之間相互滑動的難度程度。用法:shape.friction = 0.5 # 設定碰撞形狀的摩擦系數為0.5
線速度 (velocity):物體在空間中的線速度,即速度的大小和方向。用法:body.velocity = (10, 0) # 設定物體的線速度為(10, 0)
角速度 (angular_velocity):物體的角速度,即繞中心點的旋轉速度。用法:body.angular_velocity = 2.5 # 設定物體的角速度為2.5
位置 (position):物體在空間中的位置。用法:body.position = (100, 200) # 將物體移動到座標(100, 200)處
彈簧常數 (stiffness):彈簧的剛度,影響彈簧的形變程度。用法:spring.stiffness = 1000 # 設定彈簧的剛度為1000
阻尼系數 (damping):用於減少物體的速度,模擬物體在空氣或液體中運動時受到的阻力。用法:body.damping = 0.2 # 設定阻尼系數為0.2
轉動慣量 (moment_of_inertia):表示物體抵抗旋轉的能力。用法:body.moment_of_inertia = 20 # 設定物體的轉動慣量為20
形狀 (shape):描述物體的形狀,可以是圓形、矩形、多邊形等。用法:circle_shape = pymunk.Circle(body, radius) # 建立一個圓形形狀
碰撞組 (collision_type):用於標識不同型別的碰撞物件,以便進行碰撞檢測和處理。用法:shape.collision_type = 1 # 將碰撞形狀的碰撞組設定為1