5

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?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Alex Wulff
  • 2,039
  • 3
  • 18
  • 29

3 Answers3

6

The last column of a 4x4 transform matrix -- matrix.columns.3 in this case -- is the translation vector. See this old answer for some background on 4x4 transform matrices and how they work.

rickster
  • 124,678
  • 26
  • 272
  • 326
1

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)
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
1
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