ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Trying to Bounce a Ball Using an Expression and Sliders

FAQ   •   VIEW ALL
 Trying to Bounce a Ball Using an Expression and Sliders on Jun 22, 2018 at 8:24:49 pm

I'm trying to create an expression that gets values from sliders and uses them to create a ball bouncing animation. I've created two separate expressions that each produce a result that I'm looking for, but combining them produces an animation graph that is entirely unintelligible. Looking for someone to hopefully point out where I am going wrong.

I'm using three sliders to create the animation, they are 'Bounce Freq' (the number of times the ball bounces per second), 'Bounce Decay' (A percentage of how much of the bounce's height is maintained from one bounce to the next), and 'Bounce Initial Height' (the number of pixels above the 'ground' that the animation starts from).

The first expression I created was used to create the sin wave of the ball bouncing. When applied to the Y Position of my ball layer, it bounces with the correct frequency at the correct height for an indefinite amount of time. The expression is as follows:
```layer = effect("Control Layer")("Layer"); freq = layer.effect("Bounce Freq")("Slider")*Math.PI; iniHi = layer.effect("Bounce Initial Height")("Slider"); cos = iniHi*Math.cos(freq*time); abs = Math.abs(cos)*-1; abs+value;```

The second expression is meant to be used to control the height of each bounce. I applied this expression to the Source Text property of a text layer for testing purposes. It correctly outputs the height in pixels of each bounce, halving each time the ball touches the ground. The expression is as follows:
```layer = effect("Control Layer")("Layer"); freq = layer.effect("Bounce Freq")("Slider") iniHi = layer.effect("Bounce Initial Height")("Slider"); numBounces = Math.floor(freq*(time-.5))+Math.ceil(freq/2); d = layer.effect("Bounce Decay")("Slider")/100; decay = Math.pow(d,numBounces); clamp(iniHi*decay,0,10000);```

Both of these expressions work correctly on their own, but when I combine them I get an entirely different result. The combined expression is as follows:
```layer = effect("Control Layer")("Layer"); freq = layer.effect("Bounce Freq")("Slider")*Math.PI; iniHi = layer.effect("Bounce Initial Height")("Slider"); numBounces = Math.floor(freq*(time-.5))+Math.ceil(freq/2); d = layer.effect("Bounce Decay")("Slider")/100; decay = Math.pow(d,numBounces); amp = clamp(iniHi*decay,0,10000); cos = amp*Math.cos(freq*time); abs = Math.abs(cos)*-1; abs+value;```

Can anyone help me with this and point me to where I went wrong? Thanks!

 Re: Trying to Bounce a Ball Using an Expression and Sliderson Jun 25, 2018 at 3:31:13 pm

Took me a minute to notice, but the variable freq has .MathPI used in one of the expressions, and not in one of the others.

```layer = effect("Control Layer")("Layer"); freq = layer.effect("Bounce Freq")("Slider"); iniHi = layer.effect("Bounce Initial Height")("Slider"); cos = iniHi*Math.cos(freq * Math.PI *time); abs = Math.abs(cos)*-1; numBounces = Math.floor(freq*(time-.5))+Math.ceil(freq/2); d = layer.effect("Bounce Decay")("Slider")/100; decay = Math.pow(d,numBounces); linear(abs,-iniHi,0,-clamp(iniHi*decay,0,10000),0)+value ```

 Re: Trying to Bounce a Ball Using an Expression and Sliderson Jun 25, 2018 at 5:31:10 pm

Excellent, Thank you! Moving the Math.PI to the different part of the expression did indeed fix the problem I was having with the periods of the two expressions not lining up. However, I have noticed a new problem that I was unable to observe beforehand.

The part of the expression that counts the number of times the ball has bounced (this part:)
```layer = effect("Control Layer")("Layer"); freq = layer.effect("Bounce Freq")("Slider"); numBounces = Math.floor(freq*(time-.5))+Math.ceil(freq/2);```
doesn't count correctly except when the Bounce Frequency slider is set to an odd whole number (3, 5, 7, etc). When set to an even number, the count goes up when the ball is at the highest point in its arc, instead of when it contacts the ground. Having a decimal value causes the same effect (ie, if the frequency is set to 3.25, the count will increment when the ball is 1/4 completed in its arc).
I'm again at a bit of a loss, so could someone help point me in the right direction? Thanks!