There’s multiple ways to tackle this.
But because there was the boundary issue that it should never go over the comp’s edge, I thought it might be easier to define a random Walk in orthogonal steps, and then randomizing for each step if the square would turn clockwise or anti-clockwise. So instead of starting from the randomization of rotations, I start from the positions on the grid.
The anchorpoint would put itself in the correct corner to anticipate clockwise/counterclockwise, and the position that’s doing the rqndom jumps also compensates for the anchorpoint.
Because mulitple properties need to look at the same random walk values, I start with putting those in exprssion controls:
– create a 90×90 solid layer.
– add a point Control to it named “Random Walk” with this expression:
pos = [960,540];
seedRandom(100,true);
directions = [[1,0],[0,1],[-1,0],[0,-1]];
for (t=0.5; t<=time; t+=0.5) {
// move to a random neighbouring square.
i = Math.floor(random(4));
change = directions[i]*90;
pos+=change;
// if we go out of bounds, go the other way;
if (pos[0]<45 || pos[0]>1875 || pos[1]<54 || pos[1]>1055) {
pos-=change*2;
}
}
pos;
if Position gets this expression:
effect("randomWalk")("Point").value;
It already jumps around without leaving the comp.
I also added an angle control named “randomTurn” with this expression:
posterizeTime(2);
random(1)<0.5 ? -90 : 90;
Rotation gets this expression so it uses that random Turn:
ease(time%0.5, 0.0, 0.5, 0, effect("randomTurn")("Angle").value);
But it’s turning in place, then jumping, so we need to have the anchorPoint look at where it’s going. I did it like this, but there’s probably a more clever Maths-y way to do it.
Expression fro Anchorpoint:
thisPos = effect("randomWalk")("Point").valueAtTime(Math.floor(time*2)/2);
nextPos = effect("randomWalk")("Point").valueAtTime(0.5+Math.floor(time*2)/2);
turn = effect("randomTurn")("Angle").value;
direction = ((nextPos - thisPos)/90).toString();
switch (direction) {
case "0,1":
a = turn == 90 ? [0,90] : [90,90];
break;
case "0,-1":
a = turn == 90 ? [90,0] : [0,0];
break;
case "1,0":
a = turn == 90 ? [90,90] : [90,0];
break;
case "-1,0":
a = turn == 90 ? [0,0] : [0,90];
break;
}
a;
And finally, Position needs to compensate for the moving anchorPoint
effect("randomWalk")("Point").value + transform.anchorPoint - [45,45];