From c904dc6cb5c5486a79b0cc747fbc30f8dcfcc8ec Mon Sep 17 00:00:00 2001 From: Untold Engine Date: Thu, 3 Dec 2026 13:33:18 -0700 Subject: [PATCH] [Patch] Fix getCameraEye returning a stale position after WASD movement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getCameraEye read CameraComponent.eye, a field only ever written by cameraLookAt (orbit/pan/zoom/look). moveCameraWithInput/moveCameraBy/ moveCameraTo (the WASD fly path, built on translateBy/translateTo) correctly update the camera's real position but never touched .eye, so getCameraEye kept returning the pre-move position indefinitely — wrong for any caller using it as a picking-ray origin, even though the camera had visually moved. getCameraEye now reads localPosition, the same ground truth getCameraPosition already uses, so it never diverges from the camera's actual position regardless of which movement path moved it. Fixes #1437 --- Sources/UntoldEngine/Systems/CameraSystem.swift | 8 +++++++- Tests/UntoldEngineTests/CameraTest.swift | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/UntoldEngine/Systems/CameraSystem.swift b/Sources/UntoldEngine/Systems/CameraSystem.swift index ffa82f61f..63acbb09c 100644 --- a/Sources/UntoldEngine/Systems/CameraSystem.swift +++ b/Sources/UntoldEngine/Systems/CameraSystem.swift @@ -222,13 +222,19 @@ public func cameraLookAt(entityId: EntityID, eye: simd_float3, target: simd_floa applyCameraLocalTransform(entityId: entityId, position: eye, rotation: simd_normalize(q1)) } +/// The camera's current world position. Reads `localPosition` (the same ground truth +/// `getCameraPosition` uses), not the `eye` bookkeeping field `cameraLookAt` writes — +/// `eye` is only ever updated by `cameraLookAt`-family calls (orbit/pan/zoom/look), so a +/// camera moved by `moveCameraWithInput`/`moveCameraBy`/`moveCameraTo` (the WASD fly path, +/// built on `translateBy`/`translateTo`) would otherwise leave `eye` stale indefinitely +/// while the camera's actual rendered position keeps moving. public func getCameraEye(entityId: EntityID) -> simd_float3 { guard let cameraComponent = scene.get(component: CameraComponent.self, for: entityId) else { handleError(.noActiveCamera) return .zero } - return cameraComponent.eye + return cameraComponent.localPosition } public func getCameraUp(entityId: EntityID) -> simd_float3 { diff --git a/Tests/UntoldEngineTests/CameraTest.swift b/Tests/UntoldEngineTests/CameraTest.swift index 465daa050..1d2eebcfc 100644 --- a/Tests/UntoldEngineTests/CameraTest.swift +++ b/Tests/UntoldEngineTests/CameraTest.swift @@ -168,6 +168,22 @@ final class CameraTests: XCTestCase { XCTAssertEqual(cameraComponent.localPosition.z, -1.0, accuracy: 0.1) } + /// Regression test: `getCameraEye` used to return `CameraComponent.eye`, a field only + /// ever written by `cameraLookAt`. The WASD fly path (`moveCameraWithInput` -> + /// `moveCameraBy`/`moveCameraTo` -> `translateBy`/`translateTo`) updates the camera's + /// real position but never touched `.eye`, so `getCameraEye` kept returning the + /// pre-move position forever — wrong for any caller using it as a picking-ray origin, + /// even though the camera had visually moved (rendering reads `.localPosition`). + func testGetCameraEyeTracksPositionAfterWASDMovement() { + cameraLookAt(entityId: camera, eye: simd_float3(0, 0, 10), target: .zero, up: simd_float3(0, 1, 0)) + XCTAssertEqual(getCameraEye(entityId: camera), simd_float3(0, 0, 10)) + + moveCameraWithInput(entityId: camera, input: (w: true, a: false, s: false, d: false, q: false, e: false), speed: 1, deltaTime: 1) + + XCTAssertEqual(getCameraEye(entityId: camera), getCameraPosition(entityId: camera)) + XCTAssertNotEqual(getCameraEye(entityId: camera), simd_float3(0, 0, 10)) + } + func testGetMainCamera() { XCTAssertEqual(findGameCamera(), camera, "Could not find Main camera") }