ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# how can I set this expression up to stop when it lands on a surface.

FAQ   •   VIEW ALL
 how can I set this expression up to stop when it lands on a surface. on Sep 7, 2015 at 10:27:28 am

A falling leaf can be a pretty complicated simulation - sometimes they just
fall gently, sometimes they do barrel-rolls, sometimes glide back and forth, or
any combination. These expression roughly simulate the glide back and forth
variation. I tried this on a 3D solid positioned above the camera view,
oriented with Z pointing down and Y pointing in the positive Z direction
(orientation [90,0,0]).

Then I added these three expressions:

POSITION:

yVelocity = 200; //pixels per second
oscFreq = 1.5; //oscillations per second
oscDepth = 35; //oscillation depth (pixels)
drift = 25; // drift (wind?) (pixels per second: - = left, + = right)

value + [oscDepth*Math.sin(oscFreq*Math.PI*2*time) + drift *time,
yVelocity*time,0]

Z ROTATION:

seed_random(index,true);
random(360);

Y ROTATION:

oscFreq = 1.5;
maxTilt = 15; //degrees

maxTilt*Math.cos(oscFreq*Math.PI*2*time)

If you edit the parameters, you need to make sure that the oscillating
frequency is the same in both the position and y-rotation expressions (or map
it to a slider control). If you wanted to apply this to a bunch of leaves,
you'd want to introduce some randomness into the various parameters along with
a random starting phase.

 Re: how can I set this expression up to stop when it lands on a surface.on Sep 8, 2015 at 7:34:20 am

So, do you want to know how to gracefully stop it when it hits a certain y position?

 Re: how can I set this expression up to stop when it lands on a surface.on Sep 8, 2015 at 9:41:57 am

 Re: how can I set this expression up to stop when it lands on a surface.on Sep 9, 2015 at 1:23:17 pmLast Edited By Kalle Kannisto on Sep 9, 2015 at 1:29:28 pm

Here's my approach:

Add a layer named "control" with two sliders, "ground" and "ease". Set ground to the desired y coordinate for the ground plane and ease to how many pixels above the ground plane you want to start the transition.

Then use this for the Position value expression

```yVelocity = 200; //pixels per second oscFreq = 1.5; //oscillations per second oscDepth = 35; //oscillation depth (pixels) drift = 25; // drift (pixels per second: - = left, + = right) ground = thisComp.layer("control").effect("ground")("Slider"); ease = thisComp.layer("control").effect("ease")("Slider"); y_origin = transform.position.valueAtTime(0)[1]; x_position = transform.position[0]; y_position = y_origin+yVelocity*time; z_position = transform.position[2]; if (y_position>ground-ease && y_position<ground){ ease_progress = (y_position-(ground-ease))/ease*100; // how far into the ease we are (0-100) y_position = (y_position*(100-ease_progress)+ground*ease_progress)/100; oscDepth = oscDepth*(100-ease_progress)/100; } if (y_position>ground){ y_position=ground; oscDepth = 0; } y_distance = y_position-y_origin; pos = [x_position+oscDepth*Math.sin(oscFreq*Math.PI*2*time) + drift*y_distance/100, y_position, z_position]; pos```

and this for the Y Rotation

``` yVelocity = 200; //pixels per second oscFreq = 1.5; maxTilt = 15; //degrees ground = thisComp.layer("control").effect("ground")("Slider"); ease = thisComp.layer("control").effect("ease")("Slider"); y_origin = transform.position.valueAtTime(0)[1]; y_position = y_origin+yVelocity*time; if (y_position>ground-ease && y_position<ground){ ease_progress = (y_position-(ground-ease))/ease*100; // how far into the ease we are (0-100) maxTilt = maxTilt*(100-ease_progress)/100; } if (y_position>ground){ maxTilt = 0; } tilt=maxTilt*Math.cos(oscFreq*Math.PI*2*time); tilt ```

Now the animation will smoothly stop at the ground place regardless of starting position.

If you duplicate this to multiple layers the oscillation and motion will be synchronized, so you'll want to add other random values for some of the variables. Or change the settings for each layer.