-
Trying to Bounce a Ball Using an Expression and Sliders
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!