gpt4 book ai didi

python - Pymunk (花栗鼠) - 如何暂时关闭具体对象的物理/碰撞

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:49 27 4
gpt4 key购买 nike

如何在 python 中使用 pymunk lib 关闭某些对象的碰撞然后再次打开它?

让我根据下面的代码向您展示示例。我希望所有红球都穿过第一个边界线并停在下边界。蓝球仍应与上边界碰撞。

代码中需要更改什么?

import pygame
from pygame.locals import *
from pygame.color import *
import pymunk as pm
from pymunk import Vec2d
import math, sys, random

def to_pygame(p):
"""Small hack to convert pymunk to pygame coordinates"""
return int(p.x), int(-p.y+600)

pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
running = True

### Physics stuff
space = pm.Space()
space.gravity = (0.0, -900.0)

## Balls
balls = []

### walls
static_body = pm.Body()
static_lines = [pm.Segment(static_body, (111.0, 280.0), (407.0, 246.0), 0.0),
pm.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0),
pm.Segment(static_body, (111.0, 420.0), (407.0, 386.0), 0.0),
pm.Segment(static_body, (407.0, 386.0), (407.0, 493.0), 0.0)]
for line in static_lines:
line.elasticity = 0.95
space.add(static_lines)

ticks_to_next_ball = 10

while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False

ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 100
mass = 10
radius = random.randint(10,40)
inertia = pm.moment_for_circle(mass, 0, radius, (0,0))
body = pm.Body(mass, inertia)
x = random.randint(115,350)
body.position = x, 600
shape = pm.Circle(body, radius, (0,0))
shape.elasticity = 0.95
space.add(body, shape)
balls.append(shape)

### Clear screen
screen.fill(THECOLORS["white"])

### Draw stuff
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 200: balls_to_remove.append(ball)

p = to_pygame(ball.body.position)
if ball.radius > 25:
color = THECOLORS["blue"]
else:
color = THECOLORS["red"]
pygame.draw.circle(screen, color, p, int(ball.radius), 2)

for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)

for line in static_lines:
body = line.body
pv1 = body.position + line.a.rotated(body.angle)
pv2 = body.position + line.b.rotated(body.angle)
p1 = to_pygame(pv1)
p2 = to_pygame(pv2)
pygame.draw.lines(screen, THECOLORS["lightgray"], False, [p1,p2])

### Update physics
dt = 1.0/60.0
for x in range(1):
space.step(dt)

### Flip screen
pygame.display.flip()
clock.tick(50)
pygame.display.set_caption("fps: " + str(clock.get_fps()))

最佳答案

Chipmunk 有几个过滤碰撞的选项: http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpShape-Filtering

不过听起来您只需要使用图层位掩码即可。

例如:

# This layer bit is for balls colliding with other balls
# I'm only guessing that you want this though.
ball_layer = 1
# This layer bit is for things that collide with red balls only.
red_ball_layer = 2
# This layer bit is for things that collide with blue balls only.
blue_ball_layer = 4

# Bitwise OR the layer bits together
red_ball_shape.layers = ball_layer | red_ball_layer
blue_ball_shape.layers = ball_layer | blue_ball_layer

# Lower border should collide with red only
upper_border_shape.layers = red_ball_layer

#Upper border with blue balls only
lower_border_shape.layers = blue_ball_layer

我个人从未实际使用过 Pymunk,但我猜测它只是将 Chipmunk 层属性公开为 .layers

关于python - Pymunk (花栗鼠) - 如何暂时关闭具体对象的物理/碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com