## Randomizing layer Y position

Scripting for After Effects

### Randomizing layer Y position

Hi!

I have made a heartbeat monitor, but wanted to make a bit more dynamic and real. So I created a null object with some slider controls, and I'm using the following expression for the heartbeats Y position:
Code: Select all
`minY = thisComp.layer("HoldTime").effect("Min Y pos")("Slider");maxY = thisComp.layer("HoldTime").effect("Max Y pos")("Slider");seed = Math.floor(time/thisComp.layer("HoldTime").effect("Hold time")("Slider"));seedRandom(seed, true);y = random(minY, maxY);`

This will move the heartbeat up or down every "seed" seconds. But this isn't too real..
I want to achieve an expression where the heartbeat first moves up, then down, then holds for the specified amount of time. My expression gives the "person" a heartbeat of several hundred beats per minute...

Is it possible?
I was thinking of something like this, but it did not work:
Code: Select all
`minY = thisComp.layer("HoldTime").effect("Min Y pos")("Slider");maxY = thisComp.layer("HoldTime").effect("Max Y pos")("Slider");seed = Math.floor(time/thisComp.layer("HoldTime").effect("Hold time")("Slider"));seedRandom(seed, true);y = random(minY, 0); // move first upseedRandom(seed,true);y = random(0, maxY); // then down`

Cheers!
davstein

Posts: 2
Joined: 01/7/2013, 6:14 am

### Re: Randomizing layer Y position

I solved my problem, using some old trigonometry skills. It's not completely perfect, but I got an idea to turn it into a plugin.. That's gotta have to wait, but anyways; here's the code:

Code: Select all
`controls = thisComp.layer ("HeartbeatControls");A =  controls.effect("Amplitude")("Slider");c = controls.effect("c")("Slider");phi = controls.effect("phi")("Slider");d = controls.effect("d")("Slider");timeBetweenBeats = controls.effect("Seconds between beats")("Slider");beatLength = controls.effect ("Beat Length")("Slider");amplitudeVariance = controls.effect ("Amplitude Variance (%)")("Slider");currentY = effect("Separate XYZ Position")("Y Position").value;if ( random (0, 100) < amplitudeVariance )    A = A * random (1, 1.5);t = (time - inPoint) % timeBetweenBeats;if ( t > beatLength )    A = 0;y = A * Math.sin (c * (1+time) + phi) + d;`

To test it, to see my results, use the following settings:
Amplitude: 100
Amplitude variance: 30 %
c: 30
d: 38
phi: 1.5
Seconds between each beat: 1
Beat length: 0.25

Cheers!
davstein

Posts: 2
Joined: 01/7/2013, 6:14 am

### Re: Randomizing layer Y position

Heartbeats are not random. They're overlapping sinus waves with slight temporal variations. It helps to stick to the medical facts. Using your simplistic approach is not going to work in any way. If at all, you'd use a Math.sin() based on time and modulate the amplitude and phase with some randomization, but frankly within perhaps a 10 second shot nobody would even see these variations because they're so subtle (unless you want to simulate cardiac arrest with whacked out graphs). You'd be much easier off simpoly animating this by hand and using a loopOut() expression or using a graphic...

Mylenium
[Pour Mylène, ange sur terre]
----------------------------------------
www.mylenium.de - your After Effects resource
Mylenium

Posts: 3193
Joined: 02/1/2009, 11:45 am
Location: Germany