How to move camera to contain plane?


In the image above, I have a plane and a perspective camera.
I want to move the camera along Y axis until it can exact see the full plane.
how can I get the exact position Y value like this?
camera.position.y = magicFunction(plane.scale, camera.fov)

FYI. the plane and camera are both centered at origin and camera face to the plane.

can you explain more about your requirements

You also need the aspect of the camera.

I assume the camera is rotated 0.5*Math.PI around the X axis.

Try:

function magicFunction(planeScale, camera) {
    const vfov = camera.fov * Math.PI/180;
    const aspect = camera.aspect;
    const hfov = aspect*vfov; // Hm, not perfectly certain about this one right now.

    //Horizontal:
    //Solve 2*tan(0.5*hfov)*distance = planeScale.x for distance
    const hDist = planeScale.x/(2*Math.tan(0.5*hfov));

    //Vertical:
    const vDist = planeScale.z/(2*Math.tan(0.5*vfov));

    return Math.max(hDist, vDist);
}
1 Like

There are lots of lines in the screen, and I need to focus the line which user clicked.
The focus effect I need is move the camera to the center of the line, and look down the whole line.
The problem is I don’t know the position y of camera that makes the whole line in sight.
Sorry for my poor English

Yeah, as I sad, the camera is facing the plane which means your assumption was right.
I’ll try that later, thanks

If you know the FOV and the plane size. Then this is a trigonometry problem. Couldn’t you use the tan rule?
Y distance = half plane width/tan(FOV*0.5)

That is the principle behind my suggested solution. But it checks horizontally and vertically and returns the max.

1 Like

I’ve tried and this is the final solution

function magicFunction(planeScale, camera) {
    const fovTan = Math.tan(camera.fov * Math.PI / 180 / 2);

    //Horizontal:
    const hDist = planeScale.x / 2 / (fovTan * camera.aspect);

    //Vertical:
    const vDist = planeScale.z / 2 / fovTan;

    return Math.max(hDist, vDist);
}

FYI, camera.aspect = Math.tan(0.5 * hfov) / Math.tan(0.5 * vfov)
Thanks anyway

1 Like

Nice solution!

Ah, sounds much more right. Thanks for that information! :smiley:

For completeness, reordering gives:

hfov = 2*Math.atan(camera.aspect*Math.tan(0.5 * vfov))

yeah, but

const hfov = 2 * Math.atan(foo)
const bar = Math.tan(0.5 * hfov)
// foo === bar

looks kinda stupid, so I simplified it