Expression Library

Drop Bounce

Author: Dan Ebberts

Applied to the position, scale and rotation properties, this expression causes the mimics the physics of a falling object bouncing off a surface. As Dan Ebberts points out in a blog on the subject (link below), bounce is not to be confused with Keyframe Overshoot, which mimics the physics of a spring or pendulum.

Use Note: Adjust e (the elasticity variable) and g (the gravity strength) to change the characteristics of the bounce. 

Code Sample

Hover over the code and click the copy button.

				
					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