Error with pymunk space.remove method

走远了吗. 提交于 2019-12-24 16:15:30

问题


I have a ball generator, that "generates" and adds balls(circles) to the simulation.

The ball is to be removed when it hits a static poly in list s_boxes.
This is done by a collision handler ball_wall_collision.

The Error:
The following pop-up window does what it's name says, it pops-up

My code:
Ball Generator

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

Collision handler

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

What am I doing wrong in the collision handling??

  • Am I missing something in the call to space.remove?
  • Is the function not working as I want it to?? Or is the error elsewhere (which I don't think it is)...

回答1:


It looks like the problem is that you try to remove objects from the space in a collision handler during the simulation step.

Instead you can try with either manually collect all the balls into a list and then call remove after the step, or queue up removes with the post step callback like this:

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

(untested code)

I should probably try and make this more obvious in the API docs.. I wonder if it would be a good idea to automatically schedule the remove until end of step, or the less intrusive option, trigger a assert in python so you dont get the c++ error.



来源:https://stackoverflow.com/questions/15268599/error-with-pymunk-space-remove-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!