玩了一段时间后,我想出了如何使用ManimCE (v0.3.0) 做到这一点。这还没有很好的记录,但基本上你可以使用mobject updaters。我不确定这是否是最好的方法(在我看来这太冗长和低级),但它有效:
代码
import numpy as np
from manim import *
class DeplayedTrains(Scene):
def construct(self):
# create both trains
trains = (
Rectangle(height=1, width=4),
Rectangle(height=2, width=2),
)
# indicate start and end points
start_points_X, end_points_X = ((-5, 0), (5, 5))
# compute movement distances for both trains
distances = (
(end_points_X[0] - start_points_X[0]),
(end_points_X[1] - start_points_X[1]),
)
# place trains at start points and add to the scene
for train, start_point in zip(trains, start_points_X):
train.move_to(np.array([start_point, 0, 0]))
self.add(train)
# deifine durations of movements for both trains, get FPS from config
durations, fps = ((5, 3), config["frame_rate"])
# create updaters
updaters = (
# add to the current position in X the difference for each frame,
# given the distance and duration defined
lambda mobj, dt: mobj.set_x(mobj.get_x() + (distances[0] / fps / durations[0])),
lambda mobj, dt: mobj.set_x(mobj.get_x() + (distances[1] / fps / durations[1])),
)
# add updaters to trains objects, movement begins
trains[0].add_updater(updaters[0])
# wait 2 seconds
self.wait(2)
# start the movement of the second train and wait 3 seconds
trains[1].add_updater(updaters[1])
self.wait(3)
# remove the updaters
trains[0].clear_updaters() # you can also call trains[0].remove_updater(updaters[0])
trains[1].clear_updaters()