FORUMS: list search recent posts

# The Famous Pendulum

FAQ   •   VIEW ALL
 The Famous Pendulum on Jun 8, 2013 at 4:47:58 am

I have searched and searched.... And I've finally gotten to a place where I'm not entirely sure what to ask, but here it goes. On my quest to master the art of expressions I am currently attempting to control the motion of a pendulum. Not the typical way however(or at least what I've found in my searches). What I'm attempting to do is have the pendulum swing and then with a check box control(animated) switch to the motion with the decay. To describe this, If the checkbox == 0 the pendulum is not swinging. When the checkbox == 1 the pendulum swings and when the checkbox is unchecked(pendulum still swinging) the pendulum receives the decay parameter...

Here is what I've come up with, however the result I get causes the pendulum to switch to just being off. Expression is applied to null rotation.

Now the expression works with either the check box ticked on or off. However I want this transition to be seamless.

```Null Rotation Expression: freq = slider control; amp = slider control; dec = slider control; t= time; Math.sin(t*freq*Math.PI)*amp/Math.exp(t*dec) for Each of the slider controls: freq = slider control; cBox = checkbox; if ( cbox == 1){ freq = 2; }else{ 2 } In the case of the decay value: dec = slider Control; cBox = checkbox; if (cBox == 1){ dec = 1; }else{ 0 } ```

 Re: The Famous Pendulumon Jun 8, 2013 at 5:35:58 pm

That's interesting, but a little tricky. The current phase of the oscillation would be determined by the time that has elapsed since the checkbox was turned on, but the decay amount is determined by the time elapsed since the checkbox was turned off. Something like this should be close:

``` cbox = effect("Checkbox Control")("Checkbox"); freq = 1; amp = 100; dec = 3; val = 0; if (cbox.numKeys > 0){ n = 0; n = cbox.nearestKey(time).index; if (cbox.key(n).time > time) n--; if (n > 0){ t = time - cbox.key(n).time; if (cbox.value){ val = amp*Math.sin(t*freq*Math.PI*2); }else{ if (n > 1){ t0 = time - cbox.key(n-1).time; val = amp*Math.sin(t0*freq*Math.PI*2)/Math.exp(t*dec); } } } } val ```

This code assumes that an off checkbox keyframe is preceded by an on keyframe (except for the first keyframe).

Dan