Search code examples
swiftaugmented-realityarkitrealitykit

RealityKit : Camera Anchor Transform is not updating


I've got a realitykit app. I know that with arkit I can get the camera transform from frame.camera.transform.

I just can't understand why the transform of the camera anchor below is not updated. I would expect this transform to change as I move my device.

var cameraAnchor : AnchorEntity!
...

override func viewDidLoad()
{
    super.viewDidLoad()
    arView.session.delegate = self
...

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated)
    cameraAnchor = AnchorEntity(.camera)
    arView.scene.addAnchor(cameraAnchor)

...

func session(_ session: ARSession, didUpdate frame: ARFrame)
{
   print(cameraAnchor.transform)
...

result : Transform(scale: SIMD3(1.0, 1.0, 1.0), rotation: simd_quatf(real: 1.0, imag: SIMD3(0.0, 0.0, 0.0)), translation: SIMD3(0.0, 0.0, 0.0))


Solution

  • The cameraAnchor you added will always have the same transform. The cameraAnchor's transform relative to the world origin is what you really want.
    Try instead:

    print(cameraAnchor.position(relativeTo: nil))
    

    That will update the position and tell you where your camera is relative to the starting point - world position [0, 0, 0]. Same can be done with .transformMatrix(relativeTo: nil).

    Weirdly there's no way to directly get the whole Transform object, other than cameraAnchor.convert(transform: cameraAnchor.transform, to: nil); which seems convoluted.

    Docs: