Getting the world position of a QR code

Hi, would love for your help in that matter. I try to get the position in space of two QR codes to make an alignment to their positions in space. The detection shows that the QR codes position is always 0,0,0 and I don't understand why. Here's my code:

import SwiftUI import RealityKit import RealityKitContent

struct AnchorView: View { @ObservedObject var qrCoordinator: QRCoordinator @ObservedObject var coordinator: ImmersiveCoordinator let qrName: String @Binding var startQRDetection: Bool

@State private var anchor: AnchorEntity? = nil
@State private var detectionTask: Task<Void, Never>? = nil

var body: some View {
    RealityView { content in
        // Add the QR anchor once (must exist before detection starts)
        if anchor == nil {
            let imageAnchor = AnchorEntity(.image(group: "QRs", name: qrName))
            content.add(imageAnchor)
            anchor = imageAnchor
            print("📌 Created anchor for \(qrName)")
        }
    }
    .onChange(of: startQRDetection) { enabled in
        if enabled {
            startDetection()
        } else {
            stopDetection()
        }
    }
    .onDisappear {
        stopDetection()
    }
}

private func startDetection() {
    guard detectionTask == nil, let anchor = anchor else { return }

    detectionTask = Task {
        var detected = false
        while !Task.isCancelled && !detected {
            print("🔎 Checking \(qrName)... isAnchored=\(anchor.isAnchored)")

            if anchor.isAnchored {
                // wait a short moment to let transform update
                try? await Task.sleep(nanoseconds: 100_000_000)

                let worldPos = anchor.position(relativeTo: nil)

                if worldPos != .zero {
                    // relative to modelRootEntity if available
                    var posToSave = worldPos
                    if let modelEntity = coordinator.modelRootEntity {
                        posToSave = anchor.position(relativeTo: modelEntity)
                        print("converted to model position")
                    } else {
                        print("⚠️ modelRootEntity not available, using world position")
                    }

                    print("✅ \(qrName) detected at position: world=\(worldPos) saved=\(posToSave)")

                    if qrName == "reanchor1" {
                        qrCoordinator.qr1Position = posToSave
                        let marker = createMarker(color: [0,1,0])
                        marker.position = .zero // sits directly on QR
                        marker.position = SIMD3<Float>(0, 0.02, 0)
                        anchor.addChild(marker)
                        print("marker1 added")
                    } else if qrName == "reanchor2" {
                        qrCoordinator.qr2Position = posToSave
                        let marker = createMarker(color: [0,0,1])
                        marker.position = posToSave // sits directly on QR
                        marker.position = SIMD3<Float>(0, 0.02, 0)
                        anchor.addChild(marker)
                        print("marker2 added")
                    }

                    detected = true
                } else {
                    print("⚠️ \(qrName) anchored but still at origin, retrying...")
                }
            }

            try? await Task.sleep(nanoseconds: 500_000_000) // throttle loop
        }

        print("🛑 QR detection loop ended for \(qrName)")
        detectionTask = nil
    }
}

private func stopDetection() {
    detectionTask?.cancel()
    detectionTask = nil
}

private func createMarker(color: SIMD3<Float>) -> ModelEntity {
    let sphere = MeshResource.generateSphere(radius: 0.05)
    let material = SimpleMaterial(color: UIColor(
        red: CGFloat(color.x),
        green: CGFloat(color.y),
        blue: CGFloat(color.z),
        alpha: 1.0
    ), isMetallic: false)
    let marker = ModelEntity(mesh: sphere, materials: [material])
    marker.name = "marker"
    return marker
}

}

Hi @ArcSurgeryLab,

Is your RealityView in an immersive space?

Do your debugging spheres show up on the QR codes?

If your debugging spheres show up on the various QR codes the entities are clearly in different locations, so the nil in position(relativeTo:) must be using a different 'root' than expected.

Getting the world position of a QR code
 
 
Q