Skip to main content
tynanhumphrey
Participant
January 18, 2018
Answered

Moving an Object Relative to Others

  • January 18, 2018
  • 2 replies
  • 3350 views

Hi!

I have a cloud of layers spread across 3D space. They are 2D. The front layer is the controller. I used this tutorial to code this with position instead of scale. What I want instead is the layers to move relative to the objects around them. Does that make sense? If an object in the top left of the cloud moves toward the center object, all objects between them shift proportionally.

This topic has been closed for replies.
Correct answer Mylenium

You have to measure the distance using the vector length() method and then pipe the result through suitable linear() or whatever interpolators.

Mylenium

2 replies

ericsten
Inspiring
January 19, 2018

So one way of doing this is to get the change over time from the surrounding layers positions and add that to your layers current position. I can't think of a way to do this with a delayed move as that would create a temporal loopback and result in an expression (divide by zero?) error.

LP=thisLayer.transform.position; // This layers position //

LPI=thisLayer.index; // This Layers Index //

try {AZ=thisComp.layer(LPI-1).position.valueAtTime(time)-thisComp.layer(LPI-1).position.valueAtTime(0);} catch (err) {AZ=0;} //Layer Above Positional change over time//

try {CZ=thisComp.layer(LPI+1).position.valueAtTime(time)-thisComp.layer(LPI+1).position.valueAtTime(0);} catch(err) {CZ=0} //Layer Below Positional change over time//

try {NZ=(AZ+CZ)/2+LP;} catch(err) {NZ=LP;} //add average of surrounding layers positional changes to this layers position //

[NZ[0], NZ[1], NZ[2]] //positional output //

- Eric

tynanhumphrey
Participant
January 19, 2018

This is great for proximity in terms of layer index but I was thinking about comp position proximity. I'll try to combined this idea with Mylenium's to get a chosen few layers that control the rest.

Thanks!

Mylenium
MyleniumCorrect answer
Legend
January 19, 2018

You have to measure the distance using the vector length() method and then pipe the result through suitable linear() or whatever interpolators.

Mylenium

tynanhumphrey
Participant
January 23, 2018

I abandoned the interpolation expressions because they required min and max outputs which limited how far the controllers could stretch. Here's the expression I ended up using:

     //Initial position variables

     initCtrl1=thisComp.layer("Ctrl1").transform.position.valueAtTime(-1);

     initCtrl2=thisComp.layer("Ctrl2").transform.position.valueAtTime(-1);

     objPos=transform.position;

     //Current positions variables

     newCtrl1=thisComp.layer("Ctrl1").transform.position;

     newCtrl2=thisComp.layer("Ctrl2").transform.position;

    

     //Measurements

     oldDistance = initCtrl1-initCtrl2;

     newDistance = newCtrl1-newCtrl2;

     xRatio = newDistance[0]/oldDistance[0];

     yRatio = newDistance[1]/oldDistance[1];

    

     //Adds relative movement to the 0 value controller

     newPos=[newCtrl2[0]+objPos[0]*xRatio, newCtrl1[1]+objPos[1]*yRatio];

It scales well and seems fairly lightweight. Let me know if you see any optimizations.