Bounce

02 Feb 2014 //

Tags: After Effects Expressions

Bounce 2 
This expression has been kicking around the internet for a while now but its one of my favourites, although at first glance it looks complicated all you really need to know is the 3 variables at the top of the expression control its responsiveness. 

var elastic = .25; //adjust elasticity
var gravity = 8000; //adjust gravity
var nMax = 9; //adjust frequency of bounces
 
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)*elastic;
  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/gravity;
  tNext = segDur;
  nb = 1;
  while (tNext < t && nb <= nMax){
    vl *= elastic;
    segDur *= elastic;
    tCur = tNext;
    tNext += segDur;
    nb++
  }
  if(nb <= nMax){
    delta = t - tCur;
    value +  vu*delta*(vl - gravity*delta/2);
  }else{
    value
  }
}else
  value
comments powered by Disqus