Activity › Forums › Adobe After Effects Expressions › bouncing ball expression
-
bouncing ball expression
Posted by Lbtaubma on August 6, 2007 at 3:41 pmDoes anyone have a simple bouncing ball expression?
I just need a ball to bounce onto the screen and bouncing once or twice across and then bounce off. Any suggestions?
ThanksDan Ebberts replied 11 years, 9 months ago 11 Members · 15 Replies -
15 Replies
-
Mike Clasby
August 6, 2007 at 5:31 pmI used search and it wouldn’t find this oldie but goodie. Luckily I had it filed away. If you drag your ball offscreen left, then add the expressions to position and scale it will take off from wherever you place it. The initial x and y velocity and elasticity must be the same in both expressions for the squish and squash (scale) to coordinate with the bounce (position). The Floor is also adjustable.
This gem from Dan Ebberts.
Name: Dan Ebberts
Date: Sep 14, 2004 at 11:40:45 am
Subject: Re: bouncing animation
Here are a couple of bounce simulation expressions. The first one is for position and the second is for scale. With the parameters set the way they are, your object will drop until it reaches y = 400 (“floor”) and then bounce. You can control everything with the parameters (even better- assign them to sliders). You can use Vx0 and Vy0 to give your object an initial velocity – turning it into a projectile. For best results, move the anchor point of your object to the bottom. Here’s the expression for position:
Vy0 = 500; //initial y velocity (pixels/second)
Vx0 = 300; // initial x velocity (pixels/second)
g = 2500; // gravity (pixels/second/second)
floor = 400;
e = .85; //elasticityb = floor – position[1];
h = b + Vy0*Vy0/(2*g);
T = Vy0/g + Math.sqrt(2*h/g);if (time < T){ y = Vy0*time - g*time*time/2 + b; }else{ Vy = -(Vy0 - g*T); while (true){ Vy *= e; t = T; T += 2*Vy/g; if (time < T){ t = time - t; y = Vy*t - g*t*t/2; break; }else if (T - t < thisComp.frameDuration){ y = 0; break; } } } [position[0] + Vx0*time, floor - y] And for scale: Vy0 = 500; //initial y velocity (pixels/second) Vx0 = 300; // initial x velocity (pixels/second) g = 2500; // gravity (pixels/second/second) floor = 400; e = .85; //elasticity d = .02; //squash factor spd= 10; //speed of squash oscillation decay= 10.0; //how fast squash slows down b = floor - position.valueAtTime(0)[1]; h = b + Vy0*Vy0/(2*g); T = Vy0/g + Math.sqrt(2*h/g); if (time < T){ x = scale[0]; y = scale[1]; }else{ Vy = -(Vy0 - g*T); while (true){ max_dev = e*Vy*d; Vy *= e; t = T; T += 2*Vy/g; if (time < T){ t = time - t; x=scale[0]+max_dev*Math.cos(spd*(t))/Math.exp(decay*t); y=scale[0]*scale[1]/x; break; }else if (T - t < thisComp.frameDuration){ x = scale[0]; y = scale[1]; break; } } } [x,y] Dan
-
Filip Vandueren
August 6, 2007 at 6:31 pmcreate a circle, set it’s anchorPoint at the base.
Position the ball at it’s floor position.
You can just move the ball left to right to have a ‘walking’ bounce.
If you don’t go to fast, it shouldn’t be noticeable that the axis of bounce isn’t rotated along the path. it just stretches up and down.Position:
—bounceSpeed=1;
flight=0.85;
bounceHeight=250;t=Math.abs((time*2*bounceSpeed)%2-1);
t=linear(t,flight,0,0,1);
b=Math.cos(t*Math.PI/2);value – [0,bounceHeight*b];
—
Scale:
—bounceSpeed=1;
squash=0.5;
stretch=1.2;
flight=0.85;t=Math.abs((time*2*bounceSpeed)%2-1);
t= (t>flight) ? easeOut(t,flight,1,stretch,squash) : easeIn(t,0,flight,1,stretch);[value[0]/Math.sqrt(t),value[1]*t];
—
You might want to pickwhip those first lines to sliders for customizing.
The parameters are pretty self-explanatory, except “flight”:In a classic bouncing ball, there’s a squash at the moment of impact, meaning the ball is stretched at it’s longest when it hits the ground, then takes some time to compress down, and then elongate again _before_ it takes off again.
flight=0.85 means, that 15% of the time, the ball will have contact with the ground, squashing, and 85% it will be flying.
-
Geoff Towle
September 13, 2007 at 8:33 pmIs it possible to modify this expression so that the bounce occurs on each Position keyframe? In other words, i have made keyframes along the X-axis so that the ball would bounce on the lyrics of a song, and I’d like the ball to hit where the keyframes are located.
I’m new to expressions and am trying to avoid animating the manually entire song. Thanks.
-
Geoff Towle
September 13, 2007 at 10:02 pmWhen I first submitted this question I didn’t realize that I could scrub the bounce of the ball. Now knowing that, I have adjusted the speed to match the beat of the music, and am animating that way.
Thanks for the great expression, Dan.
-
Robert Elliott
July 24, 2008 at 4:44 pmWow great expression. Could rotation be added to the script so the ball has some spin as it bounces?
Thanks,
Rob -
Wim Roegels
September 16, 2008 at 11:46 amHey,
I am just trying to use this one in my comp and it says:
“Timeout while waiting for the engine”
The strange thing is, the line with the problem changes quite often: 17, 23, 18 … isn’t it strange? But there is always the T used in this line …
Has someone an idea, what can I do about it?
MacPro OSX 10.5.4 // AE CS3 8.0.2.27 // DV PAL Preset Comp
-
James Miller
May 28, 2010 at 4:46 pmHi I know this is old but expressions are new to me… So I’m trying out a bouncing ball for work. I need to drop a ball in and let it bounce around off walls and then come to a rest. I tried the first expression and got the same errors as the other guy. Tried the second expression and it works, but I want to control it a bit more. Saw the line ‘pickwhip the first lines of the expression to a slider’. Can anyone enlighten me here? I understand making the expression control and then grabbing the pickwhip, but it either says an error or dumps the code. How do I tell it to just effect the top few lines of the expression?
Cheers
-
Trent Armstrong
December 29, 2010 at 4:04 pmI am pretty sure what’s causing the timeout is that you are using a shape layer. The bounds of a shape layer are different from that of a solid. Try a solid or a precomp!
Trent
Trent Armstrong – Creative Cow Leader
https://www.dallasaeug.com -
Trent Armstrong
December 29, 2010 at 4:09 pmDan,
I’m having trouble with this one. I’m getting an error “missing )” at line 12. Any thoughts?
Thanks,
TrentTrent Armstrong – Creative Cow Leader
https://www.dallasaeug.com -
Dan Ebberts
December 29, 2010 at 5:20 pmTrent,
I just tried copying and pasting both (position and scale) expressions and they work fine for me. You’ll get the timeout error if you start with your layer below the “floor”, but it sounds like you’re seeing something different. What browser are you using? Sometimes lines with a less than character (like line 11) get interpreted as html and the code gets chopped off.
Dan
Reply to this Discussion! Login or Sign Up