我正在尝试修改本教程以使用 matplotlib_util 模块(FuncAnimation)。当我尝试运行下面的代码时,我得到了这个结果。我试图在动画函数中添加“ax.clear()”,但后来我什么也看不到。我找不到原因。
我也找不到任何同时使用“Pymuyk”和“Matplotlib FuncAnimation”的样本。如果您知道任何示例,请与我们分享。
import matplotlib.pyplot as plt
from matplotlib import animation
import pymunk
from pymunk.vec2d import Vec2d
import pymunk.matplotlib_util
def setup_space():
space = pymunk.Space()
space.gravity = 0,-9820
space.damping = 0.99
return space
def setup_balls(space):
width = 600
height = 600
for x in range(-100,150,50):
x += width / 2
offset_y = height/2
mass = 10
radius = 25
moment = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, moment)
body.position = x, -125+offset_y
body.start_position = Vec2d(body.position)
shape = pymunk.Circle(body, radius)
shape.elasticity = 0.9999999
space.add(body, shape)
pj = pymunk.PinJoint(space.static_body, body, (x, 125+offset_y), (0,0))
space.add(pj)
fig = plt.figure()
ax = plt.axes(xlim=(0, 600), ylim=(0, 600))
ax.set_aspect("equal")
space = setup_space()
setup_balls(space)
o = pymunk.matplotlib_util.DrawOptions(ax)
space.shapes[1].body.apply_impulse_at_local_point((-12000, 0))
def init():
space.debug_draw(o)
return []
def animate(dt):
#ax.clear()
for x in range(10):
space.step(1 / 50 / 10 / 2)
space.debug_draw(o)
return space,
frames = 30
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=frames, interval=20, blit=False)
plt.show()