错误与pymunk space.remove方法(Error with pymunk space.r

2019-10-17 18:39发布

我有一个球发生器,即“生成”,并增加了球(圆)的模拟。

这个球是当它击中在列表中的静态聚被删除s_boxes
这是由冲突处理完成ball_wall_collision

错误:
下面的弹出窗口做什么它的名字一样,它弹出式

我的代码:
球发电机

class BallGenerator:
    def __init__(self, min_y, max_y, x):
        self.min = min_y
        self.max = max_y
        self.x = x

        self.counter = 0

    def bowl(self, balls):
        global ball_bowled
        y = random.randint(self.min, self.max)
        pos = to_pymunk((self.x,y))
        r = 10
        m = 15
        i = pm.moment_for_circle(m, 0, r)
        b = pm.Body(m,i)
        b.position = pos
        f_x = random.randint(-600000,-400000)
        b.apply_force( (f_x,0.0),(0,0) )

        ball = pm.Circle(b, r)
        ball.elasticity = 0.75
        ball.friction = 0.95
        balls.append(ball)
        space.add(ball,b)
        print 'bowled'

        ball_bowled += 1

    def handle(self, balls):
        if self.counter == FPS:
            self.bowl(balls)
            self.counter = 0
        self.counter += 1

冲突处理

def ball_wall_collision(space, arb, balls, s_boxes):
    shapes = arb.shapes
    boxes = [box[0] for box in s_boxes] # Get walls
    ball = None
    wall = None
    for ba in balls:
        if ba in shapes:
            ball = ba
            break
    for box in boxes:
        if box in shapes:
            wall = box
            break
    if wall and ball:
        print 'removing'
        space.remove(ball, ball.body) # Where the runtime problem happens
        balls.remove(ball)
        print 'removed'

        return False
    else:
        return True
space.add_collision_handler(0,0,begin=ball_wall_collision,
                            balls=balls,s_boxes=s_boxes) # Other args to function

那我在冲突处理做错了?

  • 我错过了在呼叫的东西space.remove
  • 不正常的功能,我希望它?? 或者是错误的其他地方(我不认为这是)...

Answer 1:

它看起来像问题是,你尝试在模拟工序,以去除在冲突处理的空间物体。

相反,你可以尝试手动收集所有的球到一个列表中,然后调用步骤之后删除或排队,像这样的后工序回调中移除了:

space.add_post_step_callback(space.remove, ball)
space.add_post_step_callback(space.remove, ball.body)

(未测试的代码)

我应该尝试使这个API文档更明显..我不知道这将是一个好主意,自动调度删除直到步骤结束,或较少干扰选项,触发Python中的断言所以你不要让C ++错误。



文章来源: Error with pymunk space.remove method