pymunk space.remove 方法出错 [英] Error with pymunk space.remove method
问题描述
我有一个球生成器,它生成"并将球(圆圈)添加到模拟中.
I have a ball generator, that "generates" and adds balls(circles) to the simulation.
当球击中列表 s_boxes
中的静态多边形时,球将被移除.
这是由碰撞处理程序 ball_wall_collision
完成的.
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
我的代码:
球发生器
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
我在碰撞处理中做错了什么??
What am I doing wrong in the collision handling??
- 我是否在对
space.remove
的调用中遗漏了什么? - 该功能是否无法按照我的意愿运行??或者是其他地方的错误(我认为不是)...
- 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)...
推荐答案
问题似乎在于您在模拟步骤期间尝试从碰撞处理程序中的空间中移除对象.
It looks like the problem is that you try to remove objects from the space in a collision handler during the simulation step.
相反,您可以尝试手动将所有球收集到一个列表中,然后在该步骤之后调用 remove,或者像这样使用 post 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)
(未经测试的代码)
我可能应该尝试在 API 文档中使这一点更加明显.我想知道自动安排删除直到步骤结束或侵入性较小的选项是否是一个好主意,在 python 中触发断言,以便您不要收到 C++ 错误.
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.
这篇关于pymunk space.remove 方法出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!