1
votes

I am trying to attach a sphere at the center of the device screen and as I move the device around the sphere should stay in the centre of the screen (like a crosshair)

I have attached a sphere entity and added it to sphere_anchor like this in makeUIView function sphere_anchor.addChild(modelEntity)

But as i move my device the sphere just moves in the initial frame the entity was attached to as I move the device.Hoping someone could point me to the correct way of doing this

//Implement ARSession didUpdate session delegate method
public func session(_ session: ARSession, didUpdate frame: ARFrame) {
    
    let trasnform =  frame.camera.transform

    if ((self.scene.findEntity(named: "sphere")) != nil) {
        let position = simd_make_float3(trasnform.columns.3)
             //print(position)
         sphere_anchor.position = position
         sphere_anchor.orientation = Transform(matrix: trasnform).rotation
    }
}
1

1 Answers

2
votes

Try AnchorEntity(.camera). If you implement it there's no need for session(_:didUpdate:) instance method because RealityKit's anchor automatically tracks ARCamera position.

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    let mesh = MeshResource.generateSphere(radius: 0.1)
    let sphere = ModelEntity(mesh: mesh)

    let anchor = AnchorEntity(.camera)

    sphere.setParent(anchor)
    arView.scene.addAnchor(anchor)
    
    sphere.transform.translation.z = -0.75
}

AnchorEntity(.camera) works only when real iOS device in Active Scheme is chosen.

enter image description here