将 Pymunk 与 matplotlib.animation.FuncAnimation() 一起使用
Using Pymunk with matplotlib.animation.FuncAnimation()
我正在尝试修改 this tutorial for using matplotlib_util module (FuncAnimation). When i try to run the code below, i get this result。我试图在动画函数中添加“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()
尝试 re-setting xlim 和 ylim。似乎至少当我使用 %matplotlib notebook
魔法时 ax.clear()
也清除了 xlim 和 ylim(我现在没有 ffmpeg 来尝试确切的牛顿代码)。所以没有显示的原因是因为 x 和 y 轴是 0,1 而不是 0,600。
所以,尝试这样的事情:
ax.clear()
ax.set_xlim(0,600)
ax.set_ylim(0,600)
请注意,pymunk 中的 matplotlib 助手 class 非常基础,更像是概念验证。根据您的需要,您可能会通过“手动”绘制 space/shapes 来获得更好的结果。
我正在尝试修改 this tutorial for using matplotlib_util module (FuncAnimation). When i try to run the code below, i get this result。我试图在动画函数中添加“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()
尝试 re-setting xlim 和 ylim。似乎至少当我使用 %matplotlib notebook
魔法时 ax.clear()
也清除了 xlim 和 ylim(我现在没有 ffmpeg 来尝试确切的牛顿代码)。所以没有显示的原因是因为 x 和 y 轴是 0,1 而不是 0,600。
所以,尝试这样的事情:
ax.clear()
ax.set_xlim(0,600)
ax.set_ylim(0,600)
请注意,pymunk 中的 matplotlib 助手 class 非常基础,更像是概念验证。根据您的需要,您可能会通过“手动”绘制 space/shapes 来获得更好的结果。