Calculating distance of camera in 3D environment

1.7k Views Asked by At

I have a stage 840x840 px in size. My viewport is 840x840 px and so is my cube.

I want the face of my cube to fit exactly the space of the viewport and so the flash stage.

How can I calculate the camera z distance to accomplish this?

I've came up with something, but still it's not the solution of the problem. I've analized the situation and came up with this graphic: enter image description here

According to this, what I'm looking for is a catethus of a right triangle and, since I've got the other one and the opposite angle degrees, I can use trigonometry to find it. Still this doesn't work. In my case the box side is 840 and so is the viewport width, while the camera FOV is 60.

Through trial and error I found the right value for camera.z to be about -591 while using the formula in the image I get -1147.4613391789285 I really don't get what I'm missing.

Hope someone can help me.

Thank you! Andrea

1

There are 1 best solutions below

8
On

I am afraid your calculations are perfectly correct. In general, if half your side length is $c$, the distance from the origin should be:

$$c\left(1+\frac{1}{\tan (\beta)}\right)$$

In your case, assuming a horizontal field of view of $60^\circ$, (i.e. $\beta=30^\circ$), this indeed works out to be around $1147.46$.

The only way the distance could be as small as you suggested is if:

  • The field of view of your camera is much larger than you think, $\approx 135^\circ$
  • The camera is not using a rectilinear lens, and therefore the pinhole model is incorrect. You would still need to drastically increase the field of view for this to work.
  • You are mistaken in regards to the size of the cube. Specifically I suspect that you missed a unit conversion somewhere, since physical objects should be measured in physical units, not pixels.

In case you don't trust the math, here's a blender mock up with a camera distance of $1147.46$, which exactly fits the cube.

$\hskip 2 in$enter image description here