Get Camera Translation from ARCamera

Viewed 1629

I am experimenting with Apple's ARKit and have a question regarding camera transformations. Which of the values in the transform matrix represent how far the user has travelled from the point of origin? Calling

self.sceneView.session.currentFrame!.camera.transform.columns.0.x 

does not seem to yield the correct X translation.

Additionally, what would be the correct location for Y and Z?

3 Answers

The simplest way to get ARCamera's translation is the following:

func getCameraTransform(for sceneView: ARSCNView) -> MDLTransform {
    guard let transform = sceneView.session.currentFrame?.camera.transform else { return }
    return MDLTransform(matrix: transform)
}

let position = SCNVector3(cameraTransform.translation.x, 
                          cameraTransform.translation.y, 
                          cameraTransform.translation.z)
func session(_ session: ARSession, didUpdate frame: ARFrame)
 {
     // Do something with the new transform
     let currentTransform = frame.camera.transform
     let x = currentTransform.columns.3.x
     let y = currentTransform.columns.3.y
     let z= currentTransform.columns.3.z
 }

The last column of vector is translation values. you can get translated values from column 3

Related