Post

Replies

Boosts

Views

Activity

Reply to WorldTrackingProvider's queryDeviceAnchor is not giving correct deviceAnchor
Hello gchiste, Thanks for the reply! Please create a new visionOS app from xcode, replace the code in ImmersiveView with the following code, and change the preferred Default Scene Session Role to Immersive Space Application Session Role. // // ImmersiveView.swift // deviceAnchorSample // import SwiftUI import RealityKit import ARKit struct ImmersiveView: View { var body: some View { RealityView { content in Task { do { try await arkitSession.run([worldTrackingProvider]) var deviceAnchor : DeviceAnchor? while (deviceAnchor == nil || !checkDeviceAnchorValid(Transform(matrix: deviceAnchor!.originFromAnchorTransform).translation)) { deviceAnchor = worldTrackingProvider.queryDeviceAnchor(atTimestamp: CACurrentMediaTime()) } let cameraTransform = Transform(matrix: deviceAnchor!.originFromAnchorTransform) // attachmentEntity.transform.translation = cameraTransform.translation + [0, 0.05, -1] } catch { print("Error: \(error)") } } } } private func checkDeviceAnchorValid(_ translation: SIMD3<Float>) -> Bool { // device anchor's x and z should be almost zero, with y in the valid range of a human being's height print("deviceAnchor's translation:\(translation)") return abs(translation.x) < 0.1 && abs(translation.z) < 0.1 && translation.y > 0.2 && translation.y < 3 } // MARK: Private private let arkitSession = ARKitSession() private let worldTrackingProvider = WorldTrackingProvider() } #Preview { ImmersiveView() .previewLayout(.sizeThatFits) }// When running, look for logs that start with deviceAnchor's translation:.The dead loop problem cannot be reproduced stably, But it did happen more than once. Notice the jump change of the translation value, I think the main issue is that there are no callbacks or flags indicating the worldTrackingProvider is initiated. I'm on Xcode 15.2(15C500b), and visionOS 1.0 (21N305) simulator
Jan ’24