Sign In
Deliverability

How to use the bounce expression in after effects

Mastering the Bounce Expression in After Effects

The bounce expression in After Effects is a powerful tool for adding dynamic and realistic movement to your animations. It simulates the physics of a bouncing object, creating visually appealing results with minimal keyframing. This article will guide you through the fundamentals of the bounce expression, demonstrating how to apply it effectively and customize it to achieve different bouncing behaviors. We’ll cover everything from basic implementation to advanced customization techniques, enabling you to bring your animations to life.

Table of Contents:

Understanding the Fundamentals of the Bounce Expression

The bounce expression in After Effects leverages mathematical formulas to simulate the physical properties of a bouncing object. It’s not a single, pre-built effect; rather, it’s a snippet of code written in After Effects’ expression language (JavaScript-based) that dynamically calculates the position, rotation, or scale of a layer based on time and user-defined parameters. Understanding the underlying principles will allow you to fine-tune the expression for various animation scenarios.

The core of the bounce expression relies on the concept of damped oscillation. Imagine a weight hanging from a spring. When you pull the weight down and release it, it oscillates (moves up and down) until the oscillation gradually diminishes due to friction and air resistance. This diminishing effect is known as damping. Similarly, the bounce expression simulates this behavior, with the initial “pull” being the force that initiates the bounce, and the damping representing the energy loss with each subsequent bounce.

Here’s a breakdown of the key parameters typically used within a bounce expression:

  • Amplitude (amp): This controls the initial height or intensity of the bounce. A higher amplitude results in a larger initial bounce.
  • Frequency (freq): This determines how quickly the object bounces. A higher frequency results in more bounces per unit of time.
  • Decay (decay): This dictates how quickly the bounces diminish in height. A higher decay value results in faster damping, causing the bounces to stop sooner.
  • Min Value (minVal): This sets the minimum value that the property will bounce towards. Often defaults to zero.
  • Velocity (velocity): Initial speed or direction of movement

These parameters can be adjusted to achieve a wide range of bouncing effects, from a slow, gentle wobble to a fast, energetic bounce that quickly settles. The specific implementation of the bounce expression can vary, but these parameters are commonly found in most variations.

Example 1: A Simple Bounce Expression Breakdown

Let’s examine a common, simplified bounce expression to understand its components:

amp = 100;
freq = 3.0;
decay = 2.0;

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}

Explanation:

  • `amp = 100; freq = 3.0; decay = 2.0;`: This section defines the amplitude, frequency, and decay variables. You can adjust these values to modify the bounce behavior.
  • `n = 0; …`: This section finds the nearest keyframe to the current time. It’s used to determine the starting point for the bounce calculation.
  • `t = time – key(n).time;`: Calculates the time since the nearest keyframe.
  • `v = velocityAtTime(key(n).time);`: Obtains the velocity of the property at the nearest keyframe. This is crucial for determining the initial force of the bounce.
  • `value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;`: This is the core of the bounce calculation. It uses the amplitude, velocity, frequency, decay, and time to compute the bouncing motion. The `Math.exp(-decay*t)` part handles the decay effect, and `Math.sin(freq*t*2*Math.PI)` creates the oscillating motion.
  • `value;`: If no keyframes are found, the expression simply returns the original value of the property.

This example highlights how the expression uses time, keyframe information, and mathematical functions to create the bounce effect. You need at least two keyframes for the expression to work correctly – the first keyframe sets the starting point, and the second triggers the bounce.

Example 2: Alternative Bounce Expression Formula

Here’s another slightly different bounce expression offering similar functionality:

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time)
  n--;
}
if (n > 0){
  t = time - key(n).time;
  amp = .1;
  freq = 2.0;
  decay = 2.0;
  v = velocityAtTime(key(n).time);
  value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);
}else{
  value
}

Explanation:

  • This expression is very similar to the previous one, but it places the exponential decay in the denominator instead of multiplying. This can subtly alter the decay behavior. The important factor is the manipulation of amplitude, frequency and decay based on the time differences between keyframes and the velocity.

It’s important to note that you can define `amp`, `freq`, and `decay` either globally (outside the `if` statement) or locally (inside the `if` statement). Defining them locally allows you to change these values at different points in the timeline by adding more keyframes. We’ll explore this further in the customization section.

Basic Implementation: Applying the Bounce Expression to Position

Now that we understand the fundamentals, let’s apply the bounce expression to a layer’s Position property in After Effects. This is the most common use case for the bounce expression, creating the illusion of a bouncing object.

Step-by-Step Guide:

  • Create a New Composition: Start by creating a new composition in After Effects. Choose a suitable resolution and frame rate for your project.
  • Add a Layer: Add a layer to the composition. This could be a shape layer, a text layer, or any other type of layer that you want to animate. For simplicity, let’s use a shape layer (Layer > New > Shape Layer). Draw a simple circle using the Ellipse Tool.
  • Add Position Keyframes: Animate the position. Move the playhead to the beginning of the timeline and set a keyframe for the layer’s Position property by clicking the stopwatch icon next to “Position” in the Timeline panel. Move the playhead forward in time (e.g., 2 seconds) and change the layer’s position. This creates a second keyframe. The distance and direction between these keyframes will influence the initial “velocity” of the bounce. Move the object vertically down.
  • Add the Bounce Expression: Alt-click (or Option-click on macOS) the stopwatch icon next to the Position property. This opens the Expression Editor. Paste the following bounce expression into the Expression Editor:
amp = 100;
freq = 3.0;
decay = 2.0;

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}
  • Preview the Animation: Press the spacebar to preview the animation. You should see the layer bounce after reaching the second keyframe. The bounce will continue until the decay effect diminishes the movement.

Example 1: Creating a Ball Bounce

Let’s apply the steps above to create a realistic ball bounce:

  • Create a new Composition.
  • Create a Shape Layer (Ellipse) and make it look like a ball.
  • Set a keyframe for the ball’s Position at the top of the composition.
  • Move the playhead forward 1 second and set another keyframe at the bottom of the composition, simulating the ball falling.
  • Add the standard bounce expression from above to the Position property.
  • Adjust the `amp`, `freq`, and `decay` values until you achieve a realistic bounce. Try `amp = 80; freq = 4; decay = 3;` as a starting point. A higher `decay` will make the ball stop bouncing faster.

Experiment with different keyframe distances and timing to influence the initial velocity and the overall bounce behavior.

Example 2: Bouncing Text

You can easily adapt the same process to animate text:

  • Create a new Composition.
  • Add a Text Layer and type some text.
  • Set a keyframe for the text layer’s Position at the top of the composition (or wherever you want the bounce to start).
  • Move the playhead forward 1.5 seconds and set another keyframe where you want the text to “land.”
  • Apply the bounce expression to the text layer’s Position property.
  • Adjust the `amp`, `freq`, and `decay` values to suit the size and weight of your text. A smaller `amp` might be appropriate for smaller text.

By adjusting the keyframe positions, you can create different landing points for the text, making the bounce more dynamic and interesting.

Customizing the Bounce: Amplitude, Frequency, and Decay

The real power of the bounce expression lies in its customizability. By adjusting the `amp`, `freq`, and `decay` parameters, you can fine-tune the bounce behavior to match the specific characteristics of your animation. Let’s explore how each of these parameters affects the bounce and how to use them effectively.

Amplitude (amp):

As mentioned earlier, amplitude controls the initial height or intensity of the bounce. Increasing the amplitude will make the object bounce higher, while decreasing it will result in a smaller bounce. The amplitude value is relative to the velocity at the keyframe, so it works in conjunction with the keyframe spacing.

Frequency (freq):

Frequency determines how quickly the object oscillates or bounces. A higher frequency means the object will bounce more times within a given time frame. Increasing the frequency makes the bounces appear faster and more frequent, while decreasing it makes them slower and less frequent.

Decay (decay):

Decay controls how quickly the bounces diminish in height. A higher decay value results in faster damping, causing the bounces to stop sooner. A lower decay value results in slower damping, allowing the bounces to continue for a longer period. This is probably the most important value to adjust for a realistic effect.

Example 1: Adjusting Parameters for Different Materials

Let’s say you want to simulate the bounce of different materials. A rubber ball will bounce differently than a bowling ball. Here’s how you can adjust the parameters:

  • Rubber Ball: Use a higher amplitude, a moderate frequency, and a lower decay. This will create a high, bouncy effect that lasts for a while. Example: `amp = 120; freq = 3.5; decay = 1.5;`
  • Bowling Ball: Use a lower amplitude, a lower frequency, and a higher decay. This will create a low, heavy bounce that quickly dampens. Example: `amp = 40; freq = 2; decay = 4;`

Experiment with these values to find the perfect balance for your desired material.

Example 2: Keyframing the Bounce Parameters

For even more control, you can keyframe the `amp`, `freq`, and `decay` values themselves. This allows you to dynamically change the bounce behavior over time. For example, you might want to gradually increase the decay to simulate the object losing energy more quickly.

  • Modify the initial expression code to declare and set the variables on separate lines, as in:
amp = 100;
freq = 3.0;
decay = 2.0;

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}
  • Set keyframes for the `amp`, `freq`, or `decay` variables directly within the Expression Editor. Click the stopwatch icon next to the variable name to enable keyframing. This is ONLY possible when the variables are declared outside of the if/else statement block, as in the example above.
  • Adjust the values of these parameters at different points in the timeline to create dynamic changes in the bounce behavior.

For instance, you could start with a low decay value to simulate a long, sustained bounce and then gradually increase the decay value to make the bounce dampen more quickly as the animation progresses. You can now keyframe `amp`, `freq`, and `decay` directly. This provides a much higher level of control and allows you to create very nuanced and realistic bounce effects. Add keyframes by alt-clicking on the variable name you want to keyframe, and changing the value will automatically create a keyframe. The same can be done with the variables at the top of the expression as well!

Advanced Techniques: Bouncing Rotation and Scale

While the bounce expression is most commonly used for position, it can also be applied to other properties like Rotation and Scale to create more complex and visually interesting effects. This section explores how to adapt the bounce expression for these properties.

Bouncing Rotation:

Applying the bounce expression to the Rotation property creates a spinning bounce effect. This can be useful for simulating objects that rotate as they bounce, such as a spinning coin or a tumbling object.

Steps:

  • Apply a standard bounce expression to the layer’s Rotation property, like the simplified example earlier, or:
amp = 30; // Adjust for rotation angle
freq = 3.0;
decay = 2.0;

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}
  • Set keyframes for the Rotation property to initiate the spinning motion. The difference in rotation between the keyframes will determine the initial spin velocity.
  • Adjust the `amp`, `freq`, and `decay` values to control the speed, frequency, and damping of the rotation.

Bouncing Scale:

Applying the bounce expression to the Scale property creates a pulsating or breathing effect. This can be used to simulate objects that expand and contract as they bounce, adding a sense of dynamic energy. This is where `minVal` really becomes handy.

Steps:

  • Apply the bounce expression to the layer’s Scale property, with minVal defined:
amp = [10,10]; // Adjust for Scale percentage
freq = 3.0;
decay = 2.0;
minVal = [100,100];

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}
  • Set keyframes for the Scale property to initiate the scaling motion. The difference in scale between the keyframes will determine the initial pulse. Set the initial scale to slightly larger than 100, then the second keyframe to 100.
  • Adjust the `amp`, `freq`, and `decay` values to control the intensity, frequency, and damping of the scaling effect. `minVal` is set to `[100,100]` which helps ensure that the scaling doesn’t cause the element to disappear.

Example 1: Combining Position and Rotation Bounce

To create a more realistic bouncing effect, you can combine the bounce expression on both the Position and Rotation properties:

  • Apply the bounce expression to the Position property as described earlier.
  • Apply a separate bounce expression to the Rotation property, adjusting the `amp`, `freq`, and `decay` values to complement the position bounce.
  • Experiment with different parameter combinations to create a natural and engaging bounce.

Example 2: Pulsating Text with Scale Bounce

You can create a visually appealing pulsating text effect by applying the bounce expression to the Scale property of a text layer:

  • Apply the bounce expression to the Scale property of the text layer.
  • Set a keyframe for the Scale property at 100%.
  • Move the playhead forward and set another keyframe with a slightly larger scale value (e.g., 110%).
  • Adjust the `amp`, `freq`, and `decay` values to control the pulsation effect. Using vector values are key here! If you do not use vector values, the scaling will not work correctly. The final value of the keyframe should be 100, so the animation returns to normal.

Troubleshooting Common Bounce Expression Issues

While the bounce expression is relatively straightforward, you might encounter some common issues during implementation. This section provides troubleshooting tips to help you resolve these problems and ensure your bounce expression works correctly.

1. Expression Errors:

The most common issue is syntax errors in the expression code. After Effects is very sensitive to syntax, so even a small typo can cause the expression to fail. Check the following:

  • Misspelled variable names: Ensure that you’ve spelled variable names like `amp`, `freq`, and `decay` correctly throughout the expression.
  • Missing semicolons: JavaScript requires semicolons at the end of most statements. Make sure you haven’t omitted any semicolons.
  • Incorrect parentheses or brackets: Check that all parentheses and brackets are properly opened and closed.
  • Division by zero: Be aware that if your `freq` value ever reaches zero (or a very small number), you might encounter a division by zero error.

If you encounter an error, After Effects will display an error message in the Composition panel or in the Timeline panel when the property with the expression is selected. Read the error message carefully; it often provides clues about the location and nature of the error.

2. No Bounce Effect:

If the expression is running without errors but you don’t see any bounce effect, check the following:

  • Keyframes: The bounce expression requires at least two keyframes to work correctly. Ensure that you’ve set keyframes for the property you’re animating (e.g., Position, Rotation, Scale).
  • Keyframe Timing: The timing and spacing of your keyframes significantly impact the bounce. Make sure your keyframes are spaced appropriately to create an initial velocity.
  • Parameter Values: The `amp`, `freq`, and `decay` values might be too small to produce a noticeable bounce. Try increasing these values to see if it makes a difference.
  • Incorrect Property: Double-check that you’ve applied the expression to the correct property. It’s easy to accidentally apply the expression to the wrong property.

3. Unrealistic Bounce:

If the bounce looks unnatural or unrealistic, adjust the `amp`, `freq`, and `decay` parameters to fine-tune the bounce behavior. Consider the following:

  • Decay Too Slow: If the bounces continue for too long, increase the `decay` value to make the bounces dampen more quickly.
  • Decay Too Fast: If the bounces stop too abruptly, decrease the `decay` value to make the bounces last longer.
  • Amplitude Too High: If the initial bounce is too large, decrease the `amp` value.
  • Frequency Too High or Low: Adjust the `freq` value to control the speed and frequency of the bounces.

4. Using `minVal` incorrectly:

If you use minVal, ensure it is declared correctly, and set appropriately, especially when using vector parameters such as Scale. If you find your object is disappearing, check that this is setup correctly.

Example 1: Debugging a Syntax Error

Let’s say you have the following expression:

amp = 100
freq = 3.0;
decay = 2.0;

n = 0;
if (numKeys > 0){
  n = nearestKey(time).index;
  if (key(n).time > time){
    n--;
  }
}
if (n == 0){
  t = 0;
}else{
  t = time - key(n).time;
}

if (n > 0 && t > 0){
  v = velocityAtTime(key(n).time);
  value + amp*v*Math.exp(-decay*t)*Math.sin(freq*t*2*Math.PI)/freq;
}else{
  value;
}

After Effects will display an error message because the first line is missing a semicolon: `amp = 100`. Adding the semicolon will resolve the error.

Example 2: Adjusting Parameters for a Realistic Bounce

Suppose you’re animating a ball bounce, but the ball keeps bouncing for an unreasonably long time. To fix this, increase the `decay` value. For example, change `decay = 2.0;` to `decay = 4.0;`. This will cause the bounces to dampen more quickly and create a more realistic effect. Experiment with different values to find the best setting.

Share this article