## Calculate angle between Object and Camera in 3D

Moderators: Disciple, zlovatt

pedestrian
Posts: 2
Joined: January 5th, 2009, 2:33 am
Hi people.
I have a comp with a camera, and a layer positioned in 3D. What I would like to do is to move the camera around, and calculate the angle of the object relative to the camera.

After some googling I found this piece of code on CreativeCow:

Code: Select all

C = thisComp.layer("Camera 1");
v1 = normalize(position - C.position)
v2 = C.toWorldVec([0,0,1]);

But I'm not sure if this works, because the value depends on the xy position of the object in zspace. So not only the angle.

Basically what I am looking for is to change the object's opacity depending on its angle and distance to the camera. Could someone help me out in the right direction?

Thank!
P.
pedestrian
Posts: 2
Joined: January 5th, 2009, 2:33 am
Well after some digging around here and there I found some pieces of code here and there. It seems to work a bit. the only thing that I would like to add is the amount of degrees it faces away from the cam. So the more it faces away, to less you see it.

Code: Select all

//check to see if it is facing the camera if so then:
if (toCompVec([0, 0, 1])[2] > 0 ) {
try{ // Check whether there's a camera.
C = thisComp.activeCamera.toWorld([0,0,0]);
}catch(err){ // No camera, so assume 50mm.
w = thisComp.width * thisComp.pixelAspect;
C = [0,0,-z];
}
P = toWorld(anchorPoint);
d = length(C,P);
//change the opacity value
}
//if the object is not facing the camera than it is invisible
else 0;
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am
You could also try the "lookAt" function, for calculating the x,y,z angles between 2 points in 3d space
and the "length" function, for calculating the distance between 2 points.

for example:

Code: Select all

C = thisComp.layer("Camera 1");
ANGL = lookAt(position, C.position);
DIST = length(position, C.position);
ANGL is a 3 array with [x,y,z] 3d orientation angles between position and C.position.
DIST is a single value with the distance between position and C.position.

Koby.
ritka
Posts: 10
Joined: March 25th, 2010, 10:30 am
Hi!
How can i use the "lookAt", and "length" function for the opacity?
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am
I didn't quite understand what you want to do with the Opacity,
but you could use the lookAt and length expressions the same as I used them in the last expression...
But you have to create a single value out of the lookAt 3d vector,
and that depends on what you want to achieve...

Koby
ritka
Posts: 10
Joined: March 25th, 2010, 10:30 am
I need an expression for change the opacity depending the layer relative angle to the camera.
(Example: when the camera and the layer is face to face, then opacity is 100%, and 0% if we see the side of the layer.)
Sorry, but i'm not a genius, i don't know how to create a single value from lookAt...
Dan Ebberts
Posts: 319
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:
If you just want the opacity to go to zero when the layer is facing away from the camera, this should work:

toCompVec([0, 0, 1])[2] > 0 ? value : 0

If you need to fade from 100 to zero as it points away from the camera, try this:

v = toCompVec([0,0,1]);
d = length(toWorld(anchorPoint),thisComp.layer("Camera 1").toWorld([0,0,0]));
c = v[2]/d;
ease(c,0,1.0,0,100)

Dan
ritka
Posts: 10
Joined: March 25th, 2010, 10:30 am
I was looking for the second version.
Thanks Dan!