Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Make a layer always point away from another layer

  • Make a layer always point away from another layer

    Posted by Pravin Chottera on November 13, 2015 at 5:15 pm

    I wrote an expression to have the square always point away from the circle, and the circle to always point to the layer.

    However, I think I took the brute force route and feel like there is a smarter way to accomplish this effect.

    My expression is a series of if/else statements that first figures out which quadrant of the comp the circle is in (relative to the square), and uses that information to calculate the angles.

    Is there a way to not calculate what quadrant you’re in, and instead just look at the positions of the objects relative to the comp and calculate the angles?

    I’d like to eventually set this up in Xpresso in C4D, which is why I was hoping to avoid if/else statements.

    Thanks!

    Here is my project file

    square:

    if(thisComp.layer("VARIABLES").effect("xdiff")("Slider")>0) {
    if(thisComp.layer("VARIABLES").effect("ydiff")("Slider")>0) {
    180-(90-thisComp.layer("VARIABLES").effect("angle")("Slider"))
    }
    else {
    180-(thisComp.layer("VARIABLES").effect("angle")("Slider")+90)
    }}
    else {
    if(thisComp.layer("VARIABLES").effect("ydiff")("Slider")>0) {
    -(180-(90-thisComp.layer("VARIABLES").effect("angle")("Slider")))
    }
    else {
    -(180-(90+thisComp.layer("VARIABLES").effect("angle")("Slider")))
    }}

    ----------

    circle:

    if(thisComp.layer("VARIABLES").effect("xdiff")("Slider")>0) {
    if(thisComp.layer("VARIABLES").effect("ydiff")("Slider")>0) {
    180-(90-thisComp.layer("VARIABLES").effect("angle")("Slider"));
    }
    else {
    90-thisComp.layer("VARIABLES").effect("angle")("Slider");
    }}
    else {
    if(thisComp.layer("VARIABLES").effect("ydiff")("Slider")>0) {
    -(180-(90-thisComp.layer("VARIABLES").effect("angle")("Slider")))
    }
    else
    {
    -(90-thisComp.layer("VARIABLES").effect("angle")("Slider"))
    }}

    Pravin Chottera replied 10 years, 5 months ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    November 13, 2015 at 5:54 pm

    I think I’d try it this way:

    // square

    p = thisComp.layer(“circle”).transform.position;
    v = p – transform.position;
    radiansToDegrees(Math.atan2(v[1],v[0]))-90

    // circle

    p = thisComp.layer(“square”).transform.position;
    v = p – transform.position;
    radiansToDegrees(Math.atan2(v[1],v[0]))+90

    Dan

  • Pravin Chottera

    November 13, 2015 at 6:08 pm

    Perfect! Thanks!

    So it seems like atan2 compensates for negative values in a way that atan doesn’t? Am I understanding that correctly?

  • Pravin Chottera

    November 13, 2015 at 6:24 pm

    Ah. Looks like atan2 gives you a a value in radians. Sweet. Thanks again.

    If anyone is interested, there is more info on the topic in this thread: https://forums.creativecow.net/thread/227/7326

    Now off to learn trig in C4D!

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