Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dan Ebbert’s expression Boucing Ball + Squash & Stretch

  • Dan Ebbert’s expression Boucing Ball + Squash & Stretch

    Posted by Antoine Hecker on May 2, 2019 at 3:10 pm

    Hi guys,
    I found Dan Ebbert’s expression for the position of the boucing ball :

    Position :
    e = .7;
    g = 5000;
    nMax = 9;

    n = 0;
    if (numKeys > 0){
    n = nearestKey(time).index;
    if (key(n).time > time) n–;
    }
    if (n > 0){
    t = time – key(n).time;
    v = -velocityAtTime(key(n).time – .001)*e;
    vl = length(v);
    if (value instanceof Array){
    vu = (vl > 0) ? normalize(v) : [0,0,0];
    }else{
    vu = (v < 0) ? -1 : 1;
    }
    tCur = 0;
    segDur = 2*vl/g;
    tNext = segDur;
    nb = 1; // number of bounces
    while (tNext < t && nb <= nMax){
    vl *= e;
    segDur *= e;
    tCur = tNext;
    tNext += segDur;
    nb++
    }
    if(nb <= nMax){
    delta = t – tCur;
    value + vu*delta*(vl – g*delta/2);
    }else{
    value
    }
    }else
    value

    Working pretty fine even if I doesn’t works with x & y dimension I think only bouncing on Y but I guess it’s part of the deal.
    However I’d like to combine it with a scale expression like this one (from a previous expression) to make a squash and bounce :

    Scale :
    Vy0 = 500; //initial y velocity (pixels/second)
    Vx0 = 300; // initial x velocity (pixels/second)
    g = 2500; // gravity (pixels/second/second)
    floor = 400;
    e = .85; //elasticity

    d = .02; //squash factor
    spd= 10; //speed of squash oscillation
    decay= 10.0; //how fast squash slows down

    b = floor – position.valueAtTime(0)[1];
    h = b + Vy0*Vy0/(2*g);
    T = Vy0/g + Math.sqrt(2*h/g);

    if (time < T){
    x = scale[0];
    y = scale[1];
    }else{
    Vy = -(Vy0 – g*T);
    while (true){
    max_dev = e*Vy*d;
    Vy *= e;
    t = T;
    T += 2*Vy/g;
    if (time < T){
    t = time – t;
    x=scale[0]+max_dev*Math.cos(spd*(t))/Math.exp(decay*t);
    y=scale[0]*scale[1]/x;
    break;
    }else if (T – t < thisComp.frameDuration){
    x = scale[0];
    y = scale[1];
    break;
    }
    }
    }
    [x,y]

    Working with this one

    position :
    Vy0 = 500; //initial y velocity (pixels/second)
    Vx0 = 300; // initial x velocity (pixels/second)
    g = 2500; // gravity (pixels/second/second)
    floor = 400;
    e = .85; //elasticity

    b = floor – position[1];
    h = b + Vy0*Vy0/(2*g);
    T = Vy0/g + Math.sqrt(2*h/g);

    if (time < T){
    y = Vy0*time – g*time*time/2 + b;
    }else{
    Vy = -(Vy0 – g*T);
    while (true){
    Vy *= e;
    t = T;
    T += 2*Vy/g;
    if (time < T){
    t = time – t;
    y = Vy*t – g*t*t/2;
    break;
    }else if (T – t < thisComp.frameDuration){
    y = 0;
    break;
    }
    }
    }
    [position[0] + Vx0*time, floor – y]

    I don’t know if I’m clear enough.
    Let me know,
    Cheers !

    Kevin Camp replied 4 years, 2 months ago 4 Members · 4 Replies
  • 4 Replies
  • Breton Brander

    May 3, 2019 at 11:53 pm

    You could try something like this. It should work pretty will with Dan Ebbert’s bounce expression:

    var influence = 450; //How much do you want to influence the rotation;
    var squahLimit = 200; // What’s the maximum allowed rotation;

    //var newSquash = transform.scale + transform.position.velocityAtTime(time)/(10000/influence);

    var newSquash = transform.scale + [transform.position.velocityAtTime(time)[0]/(10000/influence),transform.position.velocityAtTime(time)[1]/(10000/influence)];

    if (newSquash > squahLimit) newSquash = squahLimit
    else if (newSquash &lt; squahLimit*-1) newSquash = squahLimit*-1;

    transform.scale + newSquash - [100,100]

  • Antoine Hecker

    May 6, 2019 at 2:54 pm

    Thank you Bret for your answer.
    Your expression seems to work but the timing and the deformation feels weird to me compared to the combination of the 2 “old expressions” I talked about earlier.
    I like the “new” position expression Dan made but with the expression you made for me, it feels unnatural, couldn’t explain.
    Thank you though.
    Any idea about the rotation expression we could make ?

    Thanks again

  • Eric Liss

    March 18, 2022 at 9:12 pm

    I get an error at this line…

    < else if (newSquash &lt; squahLimit*-1) newSquash = squahLimit*-1; >

    I believe its the &lt which Im not sure what’s going on here.
    Typo?

  • Kevin Camp

    March 18, 2022 at 10:05 pm

    it should be a less-than sign:

    else if (newSquash < squahLimit*-1) newSquash = squahLimit*-1;

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