Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Using the delta of Math.sin to make non-linear motion

  • Using the delta of Math.sin to make non-linear motion

    Posted by Bruno Barwise on September 26, 2010 at 1:42 pm

    Not sure if the title is clear so here goes :

    I’m animating a snake like layer using Math.sin on the Corner Pin effect of this same layer. This means that the layer oscillates in a squeeze-stretch fashion, like an accordion, and this is great because I can use slider controls to modify amplitude and frequency at will.

    What I would now like to do is to take the delta of the Math.sin function (with Math.abs?) and use that to move the layer over the Y axis in a non-linear way, meaning that as the snake squeezes it moves slower, and as it stretches back out it accelerates, until it reaches a certain point.

    I’ve gone as far as feeding the delta value into a text layer, but for the life of me I cannot figure out how to have a variable of some sort increase over time using the Math.abs of the delta of the Math.sin (!!). Whatever I do the variable oscillates around it’s original value, instead of increasing steadily over time…

    I’ve pasted my delta function below for reference (I’m working in 25fps hence the (time-0.04).

    Any help will be greatly appreciated !

    delta=Math.abs(thisComp.layer("controls").transform.xPosition-thisComp.layer("controls").transform.xPosition.valueAtTime(time-0.04))

    Darby Edelen replied 15 years, 7 months ago 2 Members · 2 Replies
  • 2 Replies
  • Bruno Barwise

    September 27, 2010 at 8:47 am

    Maybe I’m thinking of this in the wrong way, can I animate the speed or velocity of a layer rather than it’s position ? Then I would only need a multiple of the Math.sin function instead of having to calculate and store the delta of that function ?

  • Darby Edelen

    September 28, 2010 at 12:10 am

    You can’t change a layer’s speed or velocity directly with expressions. The only option you have in this scenario is to change the position of the layer.

    However, since you want to change the position at different rates over time (which depend on another expression outside of the position expression itself) you’ll have to calculate what that value is at every frame before the current one and then iterate through those frames calculating the position at every frame before the current one.

    This can get out of hand quickly. I can’t tell you exactly what your code will look like without knowing how you’re calculating the ‘stretch/squeeze’ factor, but it will look something like this:


    spos = transform.position.value; //the keyframed position value
    offset_min = [0, -1]; //the smallest amount the layer will move on a frame
    offset_max = [0, -5]; //the largest amount the layer will move on a frame
    p = [0,0]; //this variable will keep track of the previous frames movement
    a = 1; //this should be the amplitude you're using for the sine function elsewhere
    f = 1; //this should be the frequency you're using elsewhere (link these to your other expression, or to the sliders it is referencing)

    for(x = 0; x <= timeToFrames(time); x++){ //loop through all the frames prior to this one
    squeeze = a * Math.sin(f * Math.PI * 2 * framesToTime(x)); //...or whatever your 'squeeze' function is
    p += linear(Math.abs(squeeze), 0, 1, offset_min, offset_max); //increment the tracking variable
    }

    spos + p //add the variable to the keyframed position

    Darby Edelen

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