I'm working on a project where I need to determine the distance along the camera's local Z axis until it hits a plane in which a given object rests.
My thought process is to calculate a vecor from the camera to the focus object and then project that vector onto a vector representing the camera's local Z axis and then taking the magnitude of the projection. (If there is a better way to solve the problem, definitely let me know :)
I can easily calculate the first vector like so (in this case, the cube is the focus object):
cameraToFocus = bpy.data.objects['Camera'].location - bpy.data.objects['Cube'].location
I've been playing around with information from this answer to create a function that creates a unit vector reflecting the camera's rotation. This is what I've come up with so far and I'm not quite there:
#alpha, the angle measured from the X axis towards the y axis
#beta is the angle measured from Y axis towards Z axis
alpha = -rotation.z
beta = pi/2-rotation.x
x = cos(beta) * sin(alpha)
y = cos(beta) * cos(alpha)
z = sin(beta)
unitVector = mathutils.Vector((x, y, z))
I could probably keep adjusting the code until I get what I'm looking for, but is there a more straightforward way to create a vector representing the local Z axis of the camera, or a normal vector to the camera's field of view?
Thanks so much!