Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dynamically sized bounding box confounded by rotation

  • Dynamically sized bounding box confounded by rotation

    Posted by Rob Womack on November 11, 2015 at 7:20 pm

    I’m trying to create a label and a bounding box that conforms itself to the dimensions of an object as a camera rotates around it, and sticks to that object. So if the object is 100px x 100px, and the camera is rotated 90 degrees on the z axis, then the bounding box would be 141.4213562373095 x 141.4213562373095, which works the way that I have it set up. The problem comes when the camera rotates on the x, y and z axes. The bounding box is close, but not exact. Any help or insight into why this is happening is greatly appreciated.

    I have a simplified example here: 9448_boundingbox.aep.zip. So, for the sake of demonstration, the object is a 100×100 solid. In the bounding box’s shape/rectangle/size property, I’ve added an expression where I convert the position of each of the four corners of the object to camera space, then sort them greatest to least, then subtract the last element of that array from the first and it gives me the visible width of the object. I then sort it by height, subtract the last element from the first and it give me my visible height. But again, it doesn’t work 100% of the time, and I am definitely not taking something into consideration or going about it bass-ackwardsly. Here is the expression:

    obj = thisComp.layer("White Solid 1");
    cam = thisComp.activeCamera;
    var txt = "";

    var getPT = function(pt) { objP = cam.fromWorld(obj.toWorld(pt));return objP; } // converts to camera space

    var boundingBox = new Array();
    var boundingBoxLocal = new Array( [0,0], [obj.width,0], [0,obj.height], [obj.width, obj.height] );
    for (i=0; i<4; i++) { boundingBox.push( getPT(boundingBoxLocal[i]) );} // convert the four points

    boundingBox.sort( function(a,b){if (a[0] > b[0]) { return 1 }else { return -1 }}); // sort by x value

    var wd = boundingBox[0] - boundingBox[3]; // the visible width

    boundingBox.sort( function(a,b){if (a[1] > b[1]) { return 1 }else { return -1 }}); // sort by y value

    var ht = boundingBox[0] - boundingBox[3]; // the visible height

    [Math.abs(wd[0]), Math.abs(ht[1])]

    Robert Womack
    Creative Bridgekeep
    http://www.CurrentMarketing.com
    “Louisville’s Leading Interactive Marketing Agency”

    Dan Ebberts replied 10 years, 6 months ago 2 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    November 11, 2015 at 10:26 pm

    This seems to work for the outline size:

    obj = thisComp.layer(“White Solid 1”);
    ul = obj.toComp([0,0,0]);
    ur = obj.toComp([obj.width,0,0]);
    lr = obj.toComp([obj.width,obj.height,0]);
    ll = obj.toComp([0,obj.height,0]);
    minX = Math.min(ul[0],ur[0],lr[0],ll[0]);
    maxX = Math.max(ul[0],ur[0],lr[0],ll[0]);
    minY = Math.min(ul[1],ur[1],lr[1],ll[1]);
    maxY = Math.max(ul[1],ur[1],lr[1],ll[1]);
    [maxX-minX,maxY-minY]

    if you use this for the outline position:

    obj = thisComp.layer(“White Solid 1”);
    p = obj.toComp(obj.anchorPoint);
    [p[0]-thisComp.width/2, p[1]-thisComp.height/2, value[2]]

    Dan

  • Rob Womack

    November 12, 2015 at 2:18 am

    Well, that works fantastically. Thanks Dan! How come it works in Comp space but went out of whack in the cam space?

    Robert Womack
    Creative Bridgekeep
    http://www.CurrentMarketing.com
    “Louisville’s Leading Interactive Marketing Agency”

  • Dan Ebberts

    November 12, 2015 at 2:38 am

    Comp space is really the 2D camera view, so as long as what you’re dealing with is the projection onto that plane, it’s the best tool to use.

    Dan

  • Rob Womack

    November 12, 2015 at 2:57 am

    So, if I want my bounding box to follow the object behind other objects and be affected by the depth of field of the camera, I’m going to have to fake it then, right? That’s why I was translating into camera space – so I could keep it locked tight and not have to futz with camera blur effects and masking and such.

    Robert Womack
    Creative Bridgekeep
    http://www.CurrentMarketing.com
    “Louisville’s Leading Interactive Marketing Agency”

  • Dan Ebberts

    November 12, 2015 at 4:10 am

    Well I guess you could move the box along the line defined by the camera and where the anchor point is now (on the comp view), and resize it proportionally. There would probably be some trig involved in calculating the x,y, and z offsets. Do-able, I think, but a bit of work.

    Dan

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