Skip to main content
Participant
March 2, 2023
Answered

Sporadic rotation expression?

  • March 2, 2023
  • 2 replies
  • 583 views

So I'm trying to write an expression that randomly changes between one of three values and and smoothly transitions from the current value to a new one, and then holds for a while.  So for example, an analog clock face where the hour hand rotates from 12:00 to 4:00 to 8:00, but in no particular order and with a long hold at each position.

I was starting with the following Dan Ebberts expression:

 

segMin = .3; //minimum segment duration
segMax = .7; //maximum segment duration
minVal = [0.1*thisComp.width, 0.1*thisComp.height];
maxVal = [0.9*thisComp.width, 0.9*thisComp.height];

end = 0;
j = 0;
while ( time >= end){
  j += 1;
  seedRandom(j,true);
  start = end;
  end += random(segMin,segMax);
}
endVal =  random(minVal,maxVal);
seedRandom(j-1,true);
dummy=random(); //this is a throw-away value
startVal =  random(minVal,maxVal);
ease(time,start,end,startVal,endVal)

 

 But the two problems I haven't been able to get around are a) I don't actually want to move to a random new value, I want to move... well, in this case, I want to move either up or down by 120º.  And b) I can't figure out how to move and then hold.  I keep feeling like it should be much simpler than I'm making it in my head.

Any ideas?

This topic has been closed for replies.
Correct answer Dan Ebberts

Maybe something like this:

minDur = .3;
maxDur = .7;

moveDur = .15;

t = inPoint;
i = 1303;
seedRandom(index,true);
curVal = Math.floor(random(3));
while (t <= time){
  prevVal = curVal;
  tPrev = t;
  seedRandom(i,true);
  curVal = (curVal + Math.floor(random(1,3)))%3;
  t += random(minDur,maxDur);
  i ++;
}

v1 = prevVal*120;
v2 = curVal*120;

if (v1 == 0 && v2 == 240) v1 = 360;
if (v2 == 0 && v1 == 240) v2 = 360;

ease(time,tPrev,tPrev+moveDur,v1,v2)

2 replies

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
March 2, 2023

Maybe something like this:

minDur = .3;
maxDur = .7;

moveDur = .15;

t = inPoint;
i = 1303;
seedRandom(index,true);
curVal = Math.floor(random(3));
while (t <= time){
  prevVal = curVal;
  tPrev = t;
  seedRandom(i,true);
  curVal = (curVal + Math.floor(random(1,3)))%3;
  t += random(minDur,maxDur);
  i ++;
}

v1 = prevVal*120;
v2 = curVal*120;

if (v1 == 0 && v2 == 240) v1 = 360;
if (v2 == 0 && v1 == 240) v2 = 360;

ease(time,tPrev,tPrev+moveDur,v1,v2)
Participant
March 2, 2023

wow.
Yes, that works.  And it makes sense why it works.  Thank you so much.

Mylenium
Legend
March 2, 2023

Put the values in an array like [0,-120,120], randomize the array index with random([0,1,2]). If it needs to "remember" the previous rotation, you will have to integrate the values for the reasons explained here:

 

http://www.motionscript.com/articles/speed-control.html

 

Mylenium