I like bouncyness and natural elesticity in my motion graphics, but I suck at tweaking the velocity curves by hand. Back in the flash days we used to do elesticity by scripting so I thought I’d do the same in AE.
The problem
After Effects has one big drawback comparing it to Flash Action Scripting, and that is the fact, that a script in AE runs on each frame but it has no ability to store variables from frame to frame. And doing elasticity we need to do this since each new position is calculated not only on the present position and the target position, but also on the present speed – ie. a variable that has to be passed from one frame to the next.
This is usually solved by forcing the program to recalculate the movements of every past frame on each new frame. So on frame 4 we don’t just calculate how to behave in this frame, we have to do the same calculations we did in frame 1, 2 and 3 to find out how we got where we are now. This increases the number of calculations as we move along the timeline, which is a time consuming process, but as of now I don’t know of any other sollution. It might be possible to use placeholder varaiables on other layers, but I haven’t cracked that one yet.
The script
In all it’s glory this is how the script reads:
//Constants
k=.2;
damp=.85;
x = position[0];
y = position[1];
tx = thisComp.layer(”target 2″).position[0];
ty = thisComp.layer(”target 2″).position[1];
ax = 0;
vx = 0;
ay = 0;
vy = 0;
end = 0;
while (end < time){
//X calculations
ax=(tx-x)*k;
vx+=ax;
vx*=damp;
x+=vx;
//Y calculations
ay=(ty-y)*k;
vy+=ay;
vy*=damp;
y+=vy;
end += thisComp.frameDuration;
}
[x,y]
How does it work?
Well you need two layers in your comp; the layer you want to move placed where you want the movement to start and a target layer (a Null Object) placed where you want the movement to stop.
The target object (in the code called ‘target 2′) is a Null Object placed on the stage. This is where the layer will end up. It’s the target position. You call this layer whatever you want end then edit the code to reflect that name.
The piece of code is added to the position parameter in the transform rolldown of the layer to be moved.
How long it will take the layer to reach the target position and calm down depends on the two constants k and damp. It’s prette hard to describe what the represent, but I think of it this way:
K is the viscocity of the material the layer is being dragged through. A low value (close to 0) renders like the object is being dragged through butter or honey, while a high value (close to 1) renders the situation like it’s moving in thin air. You could also (and it might be more correct) call k the springiness of the spring, but then you’d have (or at least I have) a hard time seperating it from damp..
Damp is the rebound of the rubber band pulling the object (well technically it is the dampening effect, hence the name, but rebound sounds funnier). The higher the value (closer to 1) the longer it will take the layer to reach a stand still. Values below .5 makes for prety uinteresting moves in many cases.
Credits
This script is based on a flash version I originally learned at Kieth Peters Elasticity Tutorial, and the AE motion scripting tutorials found at motionscript.com.
One Comment
If you want a squiggle scaling effect, just throw this script on the scale:
//Constants
k=.7;
damp=.70;
scaleXY = scale[0];
targetScale = 100;
aScale = 0;
vScale = 0;
end = 0;
while (end < time){
//X calculations
aScale=(targetScale-scaleXY)*k;
vScale+=aScale;
vScale*=damp;
scaleXY+=vScale;
end += thisComp.frameDuration;
}
[scaleXY,scaleXY]