Can't center entity on AnchorEntity(.plane)

How can entities be centered on a plane AnchorEntity?

On top of the pure existence of the box's offset from the anchor's center, the offset also varies depending on the user's location in the space when the app is being started.

This is my code:

struct ImmersiveView: View {
    var body: some View {
        RealityView { content in
			    let wall = AnchorEntity(.plane(.vertical, classification: .wall, minimumBounds: [2.0, 1.5]), trackingMode: .continuous)
			    let mesh = MeshResource.generateBox(size: 0.3)
			    let box = ModelEntity(mesh: mesh, materials: [SimpleMaterial(color: .green, isMetallic: false)])
			    box.setParent(wall)
			    content.add(wall)
        }
    }
}

With PlaneDetectionProvider being unavailable on the simulator, I currently don't see a different way to set up entities at least somewhat consistently at anchors in full space.

Answered by J0hn in 764047022

I felt like this should be obvious because it's such an important use case for dropping RealityKit scenes into the world without user interaction, but I tried a few things with translation that failed.

For some reason what worked for me was calling box.setPosition.... relative to nil. I'm not sure why it works given that the documentation says "nil" means "world space", when it appears to behave as if nil means "parent space" in this case?

class Model: ObservableObject {
    var wall: AnchorEntity?
    var child: ModelEntity?
}

struct ImmersiveView: View {
    @StateObject var model = Model()
    
    var body: some View {
        RealityView { content in
            let wall = AnchorEntity(.plane(.vertical, classification: .wall, minimumBounds: [2.0, 1.5]), trackingMode: .continuous)
            model.wall = wall
            let mesh = MeshResource.generateBox(size: 0.3)
            let box = ModelEntity(mesh: mesh, materials: [SimpleMaterial(color: .green, isMetallic: false)])
            model.child = box
            
            wall.addChild(box, preservingWorldTransform: false)
            content.add(wall)
            
            box.setPosition([0, 0, 0], relativeTo: wall)
        } update: { content in
            if let box = model.child, let wall = model.wall {
//                box.setPosition([0, 0, 0], relativeTo: wall) // <---- DOES NOT WORK
                box.setPosition([0, 0, 0], relativeTo: nil) // <---- DOES WORK even though nil means "world space"????
            }
        }
    }
}
Accepted Answer

I felt like this should be obvious because it's such an important use case for dropping RealityKit scenes into the world without user interaction, but I tried a few things with translation that failed.

For some reason what worked for me was calling box.setPosition.... relative to nil. I'm not sure why it works given that the documentation says "nil" means "world space", when it appears to behave as if nil means "parent space" in this case?

class Model: ObservableObject {
    var wall: AnchorEntity?
    var child: ModelEntity?
}

struct ImmersiveView: View {
    @StateObject var model = Model()
    
    var body: some View {
        RealityView { content in
            let wall = AnchorEntity(.plane(.vertical, classification: .wall, minimumBounds: [2.0, 1.5]), trackingMode: .continuous)
            model.wall = wall
            let mesh = MeshResource.generateBox(size: 0.3)
            let box = ModelEntity(mesh: mesh, materials: [SimpleMaterial(color: .green, isMetallic: false)])
            model.child = box
            
            wall.addChild(box, preservingWorldTransform: false)
            content.add(wall)
            
            box.setPosition([0, 0, 0], relativeTo: wall)
        } update: { content in
            if let box = model.child, let wall = model.wall {
//                box.setPosition([0, 0, 0], relativeTo: wall) // <---- DOES NOT WORK
                box.setPosition([0, 0, 0], relativeTo: nil) // <---- DOES WORK even though nil means "world space"????
            }
        }
    }
}
Can't center entity on AnchorEntity(.plane)
 
 
Q