From a1926748fc19a489ec40fa4ceacef5d14849606f Mon Sep 17 00:00:00 2001 From: Esteban <35503563+EstebenR@users.noreply.github.com> Date: Sat, 2 Aug 2025 22:12:43 +0200 Subject: [PATCH 1/2] fix: Correct camera fit trigonometry --- src/camera/src/Client/CameraUtils.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/camera/src/Client/CameraUtils.lua b/src/camera/src/Client/CameraUtils.lua index cc63c5b323e..c432b7a48ac 100644 --- a/src/camera/src/Client/CameraUtils.lua +++ b/src/camera/src/Client/CameraUtils.lua @@ -39,7 +39,6 @@ end --[=[ Use spherical bounding box to calculate how far back to move a camera - See: https://community.khronos.org/t/zoom-to-fit-screen/59857/12 @param size Vector3 -- Size of the bounding box @param fovDeg number -- Field of view in degrees (vertical) @@ -54,6 +53,13 @@ end --[=[ Fits a sphere to the camera, computing how far back to zoom the camera from the center of the sphere. + Camera (C), object center (B) and object edge (A) form a right angle triangle + where AB is the radius and BC is the distance from the center to the camera + A + | \ + | \ + B-----C + Distance is given by equation tan(halfFov)=AB/BC => BC = AB/tan(halfFov) @param radius number -- Radius of the sphere @param fovDeg number -- Field of view in degrees (vertical) @@ -66,7 +72,7 @@ function CameraUtils.fitSphereToCamera(radius: number, fovDeg: number, aspectRat halfFov = math.atan(aspectRatio * math.tan(halfFov)) end - return radius / math.sin(halfFov) + return radius / math.tan(halfFov) end --[=[ From f56244f89e479f3555cf114d0355ae5d3036ffe4 Mon Sep 17 00:00:00 2001 From: Esteban Date: Wed, 28 Jan 2026 10:47:40 +0100 Subject: [PATCH 2/2] Corrected comments --- src/camera/src/Client/CameraUtils.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/camera/src/Client/CameraUtils.lua b/src/camera/src/Client/CameraUtils.lua index c432b7a48ac..83fd1ad3175 100644 --- a/src/camera/src/Client/CameraUtils.lua +++ b/src/camera/src/Client/CameraUtils.lua @@ -39,6 +39,7 @@ end --[=[ Use spherical bounding box to calculate how far back to move a camera + See: https://community.khronos.org/t/zoom-to-fit-screen/59857/12 @param size Vector3 -- Size of the bounding box @param fovDeg number -- Field of view in degrees (vertical) @@ -53,13 +54,15 @@ end --[=[ Fits a sphere to the camera, computing how far back to zoom the camera from the center of the sphere. - Camera (C), object center (B) and object edge (A) form a right angle triangle - where AB is the radius and BC is the distance from the center to the camera - A - | \ - | \ - B-----C - Distance is given by equation tan(halfFov)=AB/BC => BC = AB/tan(halfFov) + Camera (C), object center (B) and a point in the bounding sphere (A) form a right angle triangle + A forms a 90 degree angle as any tangent to a circle is perpendicular to its radius. (This is hard to show in ascii). + AB is the radius and BC is the distance from the center to the camera (hypothenuse) + A + / \ + / \ + / \ + B----------C + Distance is given by equation sin(angle)=opposite/hypothenuse = sin(halfFov)=AB/BC => BC = AB/tan(halfFov) @param radius number -- Radius of the sphere @param fovDeg number -- Field of view in degrees (vertical) @@ -72,7 +75,7 @@ function CameraUtils.fitSphereToCamera(radius: number, fovDeg: number, aspectRat halfFov = math.atan(aspectRatio * math.tan(halfFov)) end - return radius / math.tan(halfFov) + return radius / math.sin(halfFov) end --[=[