ios – Unable to match EntityAnchor to ARImageAnchor’s Coords


The purpose is to have the user be able to place an image (on a plane entity) onto a tracker image, then tap a button to have it lock in position where the tracker image was in 3D space, so that the image stays in place in AR. In theory it is designed to allow the user to then move around a space; to be able to come back to the same spot where the image is placed and see the image still there, whether the tracker image remains there or not.

This code below seemingly perfectly captures the imageAnchor coords, however, only from that initial perspective from the camera when lockedPostion was initiated. You move the camera and its clear the newly transformed arViewAnchor is ~0.3m behind where the imageAnchor was visualy and also scaled-up exactly enough to for the plane entity with the image on to match the perspective from the camera perfectly.

The only thing I can think is that the ARImageAnchor uses coords relative to the camera for its postioning, but these aren’t captured in the transform matrix as I understand it…

what do I need to understand to perfectly match the position of the imageAnchor?


func updateUIView(_ uiView: ARView, context: Context) {
        // If the locked position state has changed, execute the locking/unlocking logic
        if lockedPosition != context.coordinator.stateHolder?.previousLockedPosition {
            if lockedPosition {
                // Lock position: Remove entity from anchor and add it to the scene
                if let entity = selectedEntity, let anchor = arViewAnchor, let imageAnchor = arViewImageAnchor {
                    print("LOCKED STATE ENGAGED")
                  
                    entity.removeFromParent()
                    anchor.addChild(entity)
                    let pos = imageAnchor.transformMatrix(relativeTo: nil)
                    anchor.setTransformMatrix(pos, relativeTo: nil)
                    uiView.scene.addAnchor(anchor)

//…

This is my test config I’m working with for context:

        guard let trackerImage = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
            fatalError("Missing expected asset catalog resources.")
        }

        let configuration = ARWorldTrackingConfiguration()
        configuration.detectionImages = trackerImage
        configuration.maximumNumberOfTrackedImages = 1
        arView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

I’m very new to SwiftUI. Your help is really appreciated.

I have tried every form of capturing the imageAnchor transform matrix to apply to the arViewAnchor, but nothing seems to produce a better result than the code supplied.

I am expecting the arViewAnchor to completely match the tracking image’s location in 3D space; so that the plane entity with the image on is as it was when the it was added as a child to the imageAnchor.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img