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 = { CONFIG = {
"start_up_time" : 1, "start_up_time" : 1,
"wind_down_time" : 1, "wind_down_time" : 1,
"end_time" : np.inf,
} }
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
mobject = instantiate(mobject) mobject = instantiate(mobject)
assert(isinstance(mobject, Mobject)) assert(isinstance(mobject, Mobject))
digest_config(self, kwargs, locals()) digest_config(self, kwargs, locals())
self.total_time = 0 self.internal_time = 0
self.external_time = 0
self.setup() self.setup()
self.update(0)
def setup(self): def setup(self):
#To implement in subclass #To implement in subclass
pass 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): def update(self, dt):
if self.total_time < self.start_up_time: #TODO, currenty time moves slower for a
dt *= float(self.total_time+dt)/self.start_time #continual animation during its start up
self.total_time += dt #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) self.update_mobject(dt)
def update_mobject(self, 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") FILE_DIR = os.path.join(THIS_DIR, "files")
IMAGE_DIR = os.path.join(FILE_DIR, "images") IMAGE_DIR = os.path.join(FILE_DIR, "images")
GIF_DIR = os.path.join(FILE_DIR, "gifs") 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") STAGED_SCENES_DIR = os.path.join(FILE_DIR, "staged_scenes")
TEX_DIR = os.path.join(FILE_DIR, "Tex") TEX_DIR = os.path.join(FILE_DIR, "Tex")
TEX_IMAGE_DIR = os.path.join(IMAGE_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) continual_animation.update(dt_multiplier*self.frame_duration)
def wind_down(self, *continual_animations, **kwargs): 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: for continual_animation in continual_animations:
continual_animation.begin_wind_down(run_time) continual_animation.begin_wind_down(wind_down_time)
self.dither(run_time) self.dither(wind_down_time)
##TODO #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): def extract_mobject_family_members(self, *mobjects):

View File

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