ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Rotation of rectangle based on and locked between two null objects

FAQ   •   VIEW ALL
 Rotation of rectangle based on and locked between two null objects on Sep 18, 2019 at 4:49:22 pmLast Edited By Steve Bone on Sep 18, 2019 at 7:05:53 pm

I know that there are tons of threads that explain how to rotate a rectangle shape layer based on the position of two null objects using atan2 and radiansToDegrees. That gets me most of the way there, but I am running into a unique problem and need help.

I need for the left edge of the rectangle to meet up exactly with null_A and the right edge of the rectangle to meet up exactly with null_B. Much like pulling a bow across a violin. I've placed my anchor point in the bottom left corner of the rectangle, so the left edge meets up exactly with null_A as intended. The problem is that the right edge floats over the top of null_B when the rectangle is dragged via null_A.

```//(On Position property for rectangle) x = thisComp.layer("null_A").transform.position[0]; y = thisComp.layer("null_A").transform.position[1]; [x,y] //(On Rotation property for rectangle : 480 is the rectangle width) a = thisComp.layer("null_B").transform.position[0] - thisComp.layer("null_A").transform.position[0] + 480; b = thisComp.layer("null_B").transform.position[1] - thisComp.layer("null_A").transform.position[1]; radiansToDegrees(Math.atan2(b,a))```

```// SET ROTATION nullA_x = thisComp.layer("2_Top").transform.position[0]; nullA_y = thisComp.layer("2_Top").transform.position[1]; nullB_x = thisComp.layer("1_Bottom").transform.position[0]; nullB_y = thisComp.layer("1_Bottom").transform.position[1]; rectWidth = 484; //the problem is here with accounting for nullA to align with the right side of the rectangle. The default would've been left since the anchor point for the rectangle is placed on the bottom left. diff = length(nullA_x, nullB_x) * (rectWidth / length(nullA_x, nullB_x)); a = (nullA_x - diff) - nullB_x; b = nullA_y - nullB_y; radiansToDegrees(Math.atan2(b,a))```