FORUMS: list search recent posts

# Moving an object to a random but preset position

FAQ   •   VIEW ALL
 Moving an object to a random but preset position on Apr 26, 2016 at 9:12:09 am

Hello,
Here is the situation:
I have 1 dot, and about 7 preset positions in a sort of grid.
The dot is placed randomly on one of them, and should move every second to any other or remain on the same (as it should be random).

Do you know any script that would allow that?

Thanks for your help

 Re: Moving an object to a random but preset positionon Apr 26, 2016 at 11:24:08 am

One of the closest solution i've found is from Dan Ebberts' website
http://www.motionscript.com/mastering-expressions/random-1.html
Based on that example, I would like to have a bunch of dots shuffling their position every second, with a smooth motion, not in a jump (like in Dan's example called "random motion - synchronized").

 Re: Moving an object to a random but preset positionon Apr 26, 2016 at 5:19:55 pm

The formalize it more simply, the idea is to randomly select a preset position, then I think, the script I found on Dan's website can do the rest (the smooth motion toward the position).

Any idea about the above?

 Re: Moving an object to a random but preset positionon Apr 26, 2016 at 5:39:30 pm

Here is my latest attempt, which doesn't work at all… but still is hopefully something to discuss:
segDur = .5;// duration of each "segment" of random motion
myPos1 = 960, 300
myPos2 = 862, 460
myPos3 = 666, 300
myPos4 = 960, 540
myPos5 = 1058, 460
myPos6 = 1254, 300
myPos7 = 1254, 780
myPos8 = 666, 780

seed = Math.floor(time/segDur);
segStart = seed*segDur;
seedRandom(seed,true);
startVal = thisComp.layer("dot 1").transform.position;
seedRandom(seed+1,true);
n = Math.floor(random(7)) + 1;
endVal = "myPos." + n;
ease(time,segStart,segStart + segDur, startVal, endVal);

 Re: Moving an object to a random but preset positionon Apr 26, 2016 at 6:47:54 pm

I'm not sure exactly what you're doing, but this might get you closer:

``` segDur = .5;// duration of each "segment" of random motion pArray = [[960, 300], [862, 460], [666, 300], [960, 540], [1058, 460], [1254, 300], [1254, 780], [666, 780]]; seed = Math.floor(time/segDur); segStart = seed*segDur; seedRandom(seed,true); startVal = pArray[Math.floor(random(pArray.length))]; seedRandom(seed+1,true); endVal = pArray[Math.floor(random(pArray.length))]; ease(time,segStart,segStart + segDur, startVal, endVal); ```

Dan

 Re: Moving an object to a random but preset positionon Apr 26, 2016 at 11:03:27 pm

Thanks Dan, that works very well indeed!

I would like to add one level of complexity though, and in that case, depending on where the dot is, it could only get to certain positions nearby its current.

For instance, within this array of positions, if the dot is currently placed on [960, 540], it could only move to [1058, 460], [862, 460], and [960, 300]. Do you see what I mean?

Then, once it gets to a new position, a few others become available, and a few others not, etc. By moving from a position to another, the restiction list changes.

 Re: Moving an object to a random but preset positionon Apr 27, 2016 at 10:12:28 am

So, here is the thing. Aiming for a slightly more complex situation (hereinabove mentioned), I've tried the following script. It is wrong and doesn't work at all, but at least I think it shows what I'm trying to achieve:
The end value (the final position of the dot after each travel), depends on where it starts, and therefore, depending on the value of "startVal", "endVal" goes to randomly pick in preset arrays of positions that restrict its possibilities for each step it takes.

Thanks for any help, I know it looks quite clumsy…

```segDur = .5;// duration of each "segment" of random motion //for StartVal pArray = [[960, 300], [666, 300], [1254, 300], [862, 460], [1058, 460], [666, 780], [1254, 460]]; //for [960, 300] pArray1 = [[862, 460],           [1058, 460]]; //for [862, 460] pArray2 = [[960, 300], [666, 300], [1254, 780], [666, 780]]; //for [1058, 460] pArray3 = [[960, 300], [1254, 300], [1254, 780], [666, 780]]; //for [666, 300] pArray4 = [[862, 460], [666, 780]]; //for [1254, 300]){ pArray5 = [[1254, 780], [1058, 460]]; //for [666, 780] pArray6 = [[862, 460], [666, 300]]; //for [1254, 780]) pArray7 = [[1254, 300], [1058, 460]]; seed = Math.floor(time/segDur); segStart = seed*segDur; seedRandom(seed,true); startVal = pArray[Math.floor(random(pArray.length))]; seedRandom(seed+1,true); endVal = (if(startVal==[960, 300]){ pArray[Math.floor(random(pArray1.length))]; } if(startVal==[862, 460]){ pArray[Math.floor(random(pArray2.length))]; } if(startVal==[1058, 460]){ pArray[Math.floor(random(pArray3.length))]; } if(startVal==[666, 300]){ pArray[Math.floor(random(pArray4.length))]; } if(startVal==[1254, 300]){ pArray[Math.floor(random(pArray5.length))]; } if(startVal==[666, 780]){ pArray[Math.floor(random(pArray6.length))]; } if(startVal==[1254, 780]){ pArray[Math.floor(random(pArray7.length))]; }); ease(time,segStart,segStart + segDur, startVal, endVal);```

 Re: Moving an object to a random but preset positionon Apr 27, 2016 at 4:04:40 pm

New attempt, still not working…

```segDur = .5;// duration of each "segment" of random motion //for StartVal pArray = [[960, 300], [666, 300], [1254, 300], [862, 460], [1058, 460], [666, 780], [1254, 460]]; endVal = pArray[Math.floor(random(pArray.length))]; //for [960, 300] pArray1 = [[862, 460],           [1058, 460]]; endVal1 = pArray1[Math.floor(random(pArray1.length))]; //for [862, 460] pArray2 = [[960, 300], [666, 300], [1254, 780], [666, 780]]; endVal2 = pArray2[Math.floor(random(pArray2.length))]; //for [1058, 460] pArray3 = [[960, 300], [1254, 300], [1254, 780], [666, 780]]; endVal3 = pArray3[Math.floor(random(pArray3.length))]; //for [666, 300] pArray4 = [[862, 460], [666, 780]]; endVal4 = pArray4[Math.floor(random(pArray4.length))]; //for [1254, 300]){ pArray5 = [[1254, 780], [1058, 460]]; endVal5 = pArray5[Math.floor(random(pArray5.length))]; //for [666, 780] pArray6 = [[862, 460], [666, 300]]; endVal6 = pArray6[Math.floor(random(pArray6.length))]; //for [1254, 780]) pArray7 = [[1254, 300], [1058, 460]]; endVal7 = pArray7[Math.floor(random(pArray7.length))]; seed = Math.floor(time/segDur); segStart = seed*segDur; seedRandom(seed,true); startVal = pArray[Math.floor(random(pArray.length))]; seedRandom(seed+1,true); if(startVal==[960, 300]){ ease(time,segStart,segStart + segDur, startVal, endVal1); } if(startVal==[862, 460]){ ease(time,segStart,segStart + segDur, startVal, endVal2); } if(startVal==[1058, 460]){ ease(time,segStart,segStart + segDur, startVal, endVal3); } if(startVal==[666, 300]){ ease(time,segStart,segStart + segDur, startVal, endVal4); } if(startVal==[1254, 300]){ ease(time,segStart,segStart + segDur, startVal, endVal5); } if(startVal==[666, 780]){ ease(time,segStart,segStart + segDur, startVal, endVal6); } if(startVal==[1254, 780]){ ease(time,segStart,segStart + segDur, startVal, endVal7); }```

 Re: Moving an object to a random but preset positionon Apr 27, 2016 at 4:41:12 pm

With your new requirement, the expression now needs to know the entire history of what has happened in the past. So I think it is going to have to loop through all the previous segments to figure out what the starting position for the current segment is. I would set up a destination "array of arrays" where, the element for each position is an array of positions that it can move to from there, like this.

``` destArray = [[[862, 460],[1058, 460]], [[960, 300],[666, 300],[1254, 780],[666, 780]], . . (etc.) . [[1254, 300],[1058, 460]]]; ```

Dan

 Re: Moving an object to a random but preset positionon Apr 27, 2016 at 6:00:02 pm

Hi Dan, and thanks a lot again, but I'm not sure to understand how I make use of the destArray further in the script… How shall I refer to it?

 Re: Moving an object to a random but preset positionon May 9, 2016 at 5:14:39 pm

hey check out what I just posted here, it might help ya. as I think the code does what you want ( https://forums.creativecow.net/thread/227/32713 )

Dan C

 Re: Moving an object to a random but preset positionon Apr 27, 2016 at 6:13:18 pm

The starting position index for the current segment would be an integer between zero and six. You would use that as the index into destArray to get the sub-array of legal destinations for that starting position:

myDestArray = destArray[myPosIdx];

Then you would randomly select one of those destinations:

myDest = myDestArray[Math.floor(random(myDestArray.length))];

Something like that.

Dan