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") }