Back to Blog

# How to Use the Bounce Expression in After Effects

By Ryan Plummer

## Quickly give your layers organic movement with the Bounce Expression in After Effects.

What if you dropped a basketball and it didn't bounce? You'd probably think something was off, right? Well, the same is true in animation. Motion Design is all about the communication of ideas, and replicating movements found in the real world is an essential part of telling a compelling story. This is why it's so important to give your animations weight and mass like objects found in the real world. And this my friend is where the bounce expression comes into play...
If you're looking for a quick way to add a bounce to any layer, then this After Effects bounce expression is just for you. At first glance it can seem very daunting, and honestly it's super complex. But, don't let the complexity of it scare you away! I'm going to break down what you need to know so that you know how to use the bounce expression in your After Effects projects.
Credit to Dan Ebberts, a coding wizard, who created this bounce expression.

### The After Effects Bounce Expression

The bounce expression is great because it only takes two keyframes to create a bounce. After Effects will interpolate the velocity of your layers' movement to help determine how the bounce will work. The math that goes into making this bounce expression is pretty darn nerdy.
Feel free to copy and paste this After Effects Bounce Expression below. Don't worry, you don't have to know how this entire expression works to use it.
```e = .7; //elasticity
g = 5000; //gravity
nMax = 9; //number of bounces allowed

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```
Don't let that scary expression monster scare you away. I'm going to show you the parts of the expression that you'll need to worry about and what they do to affect the bounce. So in the end we'll only be focusing on the top three lines. It's not that scary...

### Controlling the Bounce Expression

When working with the bounce expression in After Effects there are three different portions that you will want to make changes to:
• variable e - controls the elasticity of the bounce
• variable g - controls the gravity acting on your object
• variable nMax - the maximum amount of bounces that are allowed

### What Does Elasticity Mean?

For elasticity, imagine you have a bungee chord attached to your object. The lower the number you give for e the more stiff the bounce will look. If you're looking for a bounce that feels loose, just raise this value.
The example below bounces better than a Mega Bounce XTR which is the Rolls Royce of bouncy balls, but I personally prefer like a Wham-O Superball because it has a similar coefficient of restitution for a better price... but I digress.

### What is Gravity in the Bounce Expression?

In the bounce expression gravity works just how you would think gravity should work, the higher the gravity the heavier the object will feel. If you increase the gravity value you will make the object seem heavier. Once your object completes its initial contact it will start to finish the remainder of your bounce quicker and quicker.

If you want to see the bounce expression in-action, feel free to download the project file below. The project file includes the two examples above. Have fun playing with the numbers.