Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Math.sin – how to vary the speed smoothly

  • Math.sin – how to vary the speed smoothly

    Posted by Paul Binns on June 2, 2009 at 12:28 pm

    I am trying to animate a butterfly wing whereby (using the Math.sin function) I can get the wingbeats to speed up and then to slow down.

    I am using this formula : Math.sin(time*thisComp.layer(“Null 1”).effect(“time_variable”)(“Slider”))*50 on the layers rotation property (in 3d)

    the time_variable parameter links to a keyframed expression slider moving from 1 to 5 and then back down again.

    Unfortunately somethings goes wierd in the maths and looking at the expression graph I can see that I am not just getting simple modulation from slow to fast and back again.

    What happens is that the moment the speed from the expression slider starts to reduce (ie drop from 5 back down to 1) the speed jumps right down immediately with no transition.

    see image below

    Raymond Rossell replied 13 years, 1 month ago 4 Members · 4 Replies
  • 4 Replies
  • Koby Goldberg

    June 2, 2009 at 3:18 pm

    What you’ve tried is indeed incorrect for such a motion.
    It’s a bit too complicated to explain.
    Try this instead:

    sum = 0;
    dt = thisComp.frameDuration;
    f = thisComp.layer("Null 1").effect("time_variable")("Slider");
    for (t=inPoint; t < time; t+=dt) {
    sum += f.valueAtTime(t);
    }
    50*Math.sin(sum)*dt

    Koby.

  • Dan Ebberts

    June 2, 2009 at 3:27 pm

    It’s complicated. The problem is that when you change the frequency, the result you get from Math.sin() is the same as if the frequency had always been the new value. You need to perform an integration. If you’re using linear keyframes, you can do it like this:

    myProp = thisComp.layer(“Null 1”).effect(“time_variable”)(“Slider”);
    v1 = myProp.value;
    t1 = time;
    accum = 0;

    n = 0;
    if (myProp.numKeys > 0){
    n = myProp.nearestKey(time).index;
    if (myProp.key(n).time > time){
    n–;
    }
    }

    if (n > 0){

    t0 = myProp.key(n).time;
    v0 = myProp.key(n).value;
    accum += ((v0 + v1)/2)*(t1-t0);
    t1 = t0;
    v1 = v0;

    while (n > 1){

    t0 = myProp.key(n-1).time;
    v0 = myProp.key(n-1).value;

    accum += ((v0 + v1)/2)*(t1-t0);
    t1 = t0;
    v1 = v0;
    n–

    }

    }

    accum += ((myProp.valueAtTime(0) + v1)/2)*t1;

    50*Math.sin(accum)

    Otherwise, you’ll need to do a frame-by-frame integration which would be less code, but could bog down if your comp is long.

    Dan

  • Paul Binns

    June 3, 2009 at 12:00 am

    Dan and Koby

    thankyou so much for the effort you put into your answers.
    I will try them both later today.

    I figured out (while lying in bed thinking about the problem) that it had something to do with the way the sin values are being created, but there is no way that I would have been able to find a solution like what you both suggested.
    I am surprised that after effects doesn’t have a more elegant way to create modulation like this. Coming from a music synthesis background this kind of thing is incredibly easy to build in a modular synthesis environment.

  • Raymond Rossell

    March 23, 2013 at 10:46 pm

    A million thanks!! I learned now how to integrate a value using keyframes, even though i didn’t use the “trapezoid” integration because i’m using hold keyframes, so i simplified the math and used only the value at v0 to calculate the accumulator. Again, thanks. And of course, this method avoids frame-by-frame integration.

    dir=effect("dirección")("Ã

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy