Fixes to ContinualAnimation

This commit is contained in:
Grant Sanderson 2017-08-24 12:38:37 -07:00
parent af38dd1095
commit a954a09857
4 changed files with 42 additions and 15 deletions

View file

@ -5,22 +5,40 @@ class ContinualAnimation(object):
CONFIG = {
"start_up_time" : 1,
"wind_down_time" : 1,
"end_time" : np.inf,
}
def __init__(self, mobject, **kwargs):
mobject = instantiate(mobject)
assert(isinstance(mobject, Mobject))
digest_config(self, kwargs, locals())
self.total_time = 0
self.internal_time = 0
self.external_time = 0
self.setup()
self.update(0)
def setup(self):
#To implement in subclass
pass
def begin_wind_down(self, wind_down_time = None):
if wind_down_time is not None:
self.wind_down_time = wind_down_time
self.end_time = self.external_time + self.wind_down_time
def update(self, dt):
if self.total_time < self.start_up_time:
dt *= float(self.total_time+dt)/self.start_time
self.total_time += dt
#TODO, currenty time moves slower for a
#continual animation during its start up
#to help smooth things out. Does this have
#unwanted consequences?
self.external_time += dt
if self.external_time < self.start_up_time:
dt *= float(self.external_time)/self.start_up_time
elif self.external_time > self.end_time - self.wind_down_time:
dt *= np.clip(
float(self.end_time - self.external_time)/self.wind_down_time,
0, 1
)
self.internal_time += dt
self.update_mobject(dt)
def update_mobject(self, dt):

View file

@ -63,7 +63,8 @@ THIS_DIR = os.path.dirname(os.path.realpath(__file__))
FILE_DIR = os.path.join(THIS_DIR, "files")
IMAGE_DIR = os.path.join(FILE_DIR, "images")
GIF_DIR = os.path.join(FILE_DIR, "gifs")
MOVIE_DIR = os.path.join(FILE_DIR, "movies")
# MOVIE_DIR = os.path.join(FILE_DIR, "movies")
MOVIE_DIR = os.path.join("/Users/grant/Dropbox/Bell's Project/", "movies")
STAGED_SCENES_DIR = os.path.join(FILE_DIR, "staged_scenes")
TEX_DIR = os.path.join(FILE_DIR, "Tex")
TEX_IMAGE_DIR = os.path.join(IMAGE_DIR, "Tex")

View file

@ -126,12 +126,16 @@ class Scene(object):
continual_animation.update(dt_multiplier*self.frame_duration)
def wind_down(self, *continual_animations, **kwargs):
run_time = kwargs.get("run_time", 1)
wind_down_time = kwargs.get("wind_down_time", 1)
for continual_animation in continual_animations:
continual_animation.begin_wind_down(run_time)
self.dither(run_time)
##TODO
continual_animation.begin_wind_down(wind_down_time)
self.dither(wind_down_time)
#TODO, this is not done with the remove method so as to
#keep the relevant mobjects. Better way?
self.continual_animations = filter(
lambda ca : ca in continual_animations,
self.continual_animations
)
#
def extract_mobject_family_members(self, *mobjects):

View file

@ -45,7 +45,7 @@ class OscillatingVector(ContinualAnimation):
def update_mobject(self, dt):
f = self.frequency
t = self.total_time
t = self.internal_time
angle = 2*np.pi*f*t
vect = np.array([
self.A_x*np.exp(complex(0, angle + self.phi_x)),
@ -73,10 +73,14 @@ class Test(Scene):
)
randy = Randolph()
self.add(randy, wiggle)
self.dither(2)
self.play(FadeOut(E_vect))
self.dither(2)
self.add(randy)
self.play(ShowCreation(wiggle.mobject))
self.add(wiggle)
self.dither(4)
self.wind_down(wiggle)
self.dither()
# self.play(FadeOut(E_vect))
# self.dither(2)