Copy link to clipboard
Copied
Hey All!
I'm trying to create an expression that will randomly move a square one of four ways, and only do so occasionally. I've got it close, and I think it's working except for - the square returns to its home anchor point every time. The idea is that it would move North, East, South, or West about 30% of the time, and the rest of the time it would just stay in its last position. I am sooooo close with this script:
segDur = .5;// duration of each "segment" of random motion
seed = Math.floor(time/segDur);
segStart = seed*segDur;
amt=800;
south=[0,-1*amt];
north=[0,amt];
east=[amt,0];
west=[-amt,0];
seedRandom(seed,true);
m=Math.round(random(1,15));
if (m==1){startVal=north}
else if (m==2){startVal=south}
else if (m==3){startVal=east}
else if (m==4){startVal=west}
else startVal=[0,0];
seedRandom(seed+1,true);
m=Math.round(random(1,15));
if (m==1){endVal=startVal+north}
else if (m==2){endVal=startVal+south}
else if (m==3){endVal=startVal+east}
else if (m==4){endVal=startVal+west}
else endVal=[0,0];
ease(time,segStart,segStart + segDur, startVal, endVal)
Obviously stole a lot of this from @Dan Ebberts and have been modifying on my own for two days. I just KNOW that as soon as someone points out the solution I will be incredibly frustated with the simplicity of the code. But i've spent too much time on it and client needs it done. Can anyone point out how stupid I have been?
Copy link to clipboard
Copied
I'm thinking something like this:
segDur = .5;
amt = 800;
t = 0;
accum = prevAccum = [0,0];
seedRandom(index,true);
while (t <= time){
n = Math.round(random(1,15));
prevAccum = accum;
switch(n){
case 1:
accum += [0,-amt];
break;
case 2:
accum += [0,amt];
break;
case 3:
accum += [amt,0];
break;
case 4:
accum += [-amt,0];
break;
default:
break;
}
t += segDur;
}
value + ease(time,t-segDur,t,prevAccum,accum)
Copy link to clipboard
Copied
oh okay - so basically create a looping accum value that gets added to each time the 'while' segment runs. Ha. I was JUST getting to this section in your blog before I quit for the day! This works perfectly!
In order to adjust frequency, in theory, I can just adjust the max value of n, correct? Mathematically, it makes sense to me, but I am thinking about adding a slider to that max value that allows me to adjust how often the square moves, but the moves will still be random?
and it looks like the switch/case is a much more elegant way of solving the changing inputs. Of course.
for those following along, the square on the left is my original code, and the one on the right with the blue outline is @Dan s code.
Thanks so much! I have no idea I have learned about expressions by reading your content, but it's basically everything I have learned about expressions!
Copy link to clipboard
Copied
>In order to adjust frequency, in theory, I can just adjust the max value of n, correct?
Yes.
> I am thinking about adding a slider to that max value that allows me to adjust how often the square moves, but the moves will still be random?
Yes, that would be like this:
segDur = .5;
amt = 800;
maxN = effect("Slider Control")("Slider");
t = 0;
accum = prevAccum = [0,0];
seedRandom(index,true);
while (t <= time){
n = Math.round(random(1,maxN));
prevAccum = accum;
switch(n){
case 1:
accum += [0,-amt];
break;
case 2:
accum += [0,amt];
break;
case 3:
accum += [amt,0];
break;
case 4:
accum += [-amt,0];
break;
default:
break;
}
t += segDur;
}
value + ease(time,t-segDur,t,prevAccum,accum)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now