Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › Random direction change constrained to 90 degrees

  • Random direction change constrained to 90 degrees

    Posted by neil sanderson on May 26, 2021 at 5:48 am

    Hey guys, I’m looking for some expression help. I have a square shape layer setup within a grid system. Every 25 frames I would like that square to slide in to a random adjacent square in the grid, so every 25 frames the layer randomly moves either up, down, left or right in to another square in the grid.

    neil sanderson replied 5 years, 4 months ago 2 Members · 3 Replies
  • 3 Replies
  • Filip Vandueren

    May 26, 2021 at 9:07 am

    This seems to work:

    seedRandom(index,true);
    moveDistance = 100;
    moveEvery = framesToTime(25);
    startPos = [960,540];
    moves=[startPos];
    possibleMoves = [[1,0],[-1,0],[0,1],[0,-1]];
    st = thisLayer.startTime;
    // create all moves up front
    for (i=0; i < (thisComp.duration - st); i+=moveEvery) {
    move = possibleMoves[ Math.floor(random(4)) ];
    lastPos = moves[moves.length-1];
    newPos = lastPos + move*moveDistance;
    moves.push(newPos);
    }
    t=(time-st)%moveEvery;
    step = Math.floor((time-st)/moveEvery);
    easeOut( t, 0, moveEvery, moves[step], moves[step+1]);
  • Filip Vandueren

    May 26, 2021 at 9:39 am

    This version is better if you experience glitches: (sometimes doing % and / operations on time gives glitchy results, it depends on the framerate. using timeToFrames() can fix that)

    seedRandom(index,true);

    moveDistance = 100;

    moveEvery = 25;

    startPos = [960,540];

    moves=[startPos];

    possibleMoves = [[1,0],[-1,0],[0,1],[0,-1]];

    st = thisLayer.startTime;

    // create all moves up front

    for (i=0; i<timeToFrames(thisComp.duration - st); i+=moveEvery) {

    move = possibleMoves[ Math.floor(random(4)) ];

    lastPos = moves[moves.length-1];

    newPos = lastPos + move*moveDistance;

    moves.push(newPos);

    }

    f=timeToFrames(time-st);

    t=f%moveEvery;

    step = Math.floor(f/moveEvery);

    ease( t, 0, moveEvery, moves[step], moves[step+1]);

  • neil sanderson

    May 27, 2021 at 2:02 am

    This is incredible! Thank you so much Filip, both for the quick reply, and the awesome expression.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy