Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/UntoldEngine/Systems/CameraSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions Tests/UntoldEngineTests/CameraTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Loading