-
Dynamically sized bounding box confounded by rotation
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 pointsboundingBox.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”