it's been a couple of days since I started my research on how to calculate the angle between a camera's Z axis and a layer's Z axis;
It would seem pretty simple but there are a couple of aspects I really can't seem to figure out.
Basically in my comp I have a camera and a layer; nothing fancy.
I want to know the angle beteen the two, which NOT ONLY changes with the position of the camera when you orbit around the layer, but also when you move from side to side while looking at the layer and, most importantly, if the layer is rotated on it's own, while the camera is still or moves along another path.
I attached 3 examples that should help explain the task I want to accomplish; all drawings are referred to a TOP VIEW.
My first attemp was to use lookAt(thisComp.activeCamera.position,position); to know the orientation of the layer;
although it seemed to be working, once I started turning the layer on its own, the expression broke, basically not changing the value since the camera and the layer did not physically move.
Besides, it does not return a value that can actually tell my if the camera is looking at the layer from the left or the right, kind of as if the value was absolute.
My second, and last, attempt was to use one of Dan Ebberts expressions which I found in this forum:
And while it did a better job, by dealing with rotation of the layer as well, it also couldn't tell me if the layer was rotated to the left or to the right of the camera.
My question then is:
Is there a way to know whether the target layer is rotated to the right or to the left?
Maybe using other parameters of the camera or layer....
If there was a way to know, for example, which side of the target layer is closer to the camera, it would be perfect!
This one should give you the angle between the z axis of the camera and the z axis of the target layer. The result should be positive if the camera is to the right of the layer. This assumes that you're moving things around in the x/z plane, not y (otherwise left and right really makes no sense).
L = thisComp.layer("target layer");
C = thisComp.activeCamera;
v1 = C.toWorldVec([0,0,1]);
v2 = L.toWorldVec([0,0,1]);
angle = radiansToDegrees(Math.acos(dot(v1,v2)));
if(cross(v1,v2) < 0 ) -angle else angle
Thanks Dan, I really was hoping for your response! ;D
Your expression solves almost all problems, but as you said, y rotation is not taken into account; unfortunately, I might have a slight rotation along the Y axis as well, since I'll eventually use a real camera and composite it into that comp, I could have the need during the shoot to take the camera slightly higher than normal and point downwards or vice-versa.
I noticed that your script would transition between two adjacent frames very abruptly from positive to negative without going through zero, is there a way to fix it? even some sort of ease would get the job done, I think.
I attach two screenshots of adjacent frames when the camera is slightly tilted along the Y axis.
unfortunately it does the same as the previous code you sent me; in fact, it spits out the exact same values (although switched in sign).
I think the real problem lies in the if(...) -angle; else angle; statement; that abrupt transition from positive to negative should be solved otherwise.
Maybe the ae project I attached can help you try different approaches; I'll link it again here: https://f1.creativecow.net/11724/11724