2

I am trying to get the position of the camera in the 3D space in the global coordinates.

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        sceneView.delegate = self
        sceneView.showsStatistics = true
        sceneView.session.delegate = self
    }

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Create a session configuration with plane detection
    let configuration = ARWorldTrackingConfiguration()
    configuration.worldAlignment = .gravityAndHeading
    // Run the view's session
    sceneView.session.run(configuration)
}

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

I run the above code and below is an example output I get

Optional(simd_float4x4([[-0.739868, -0.052769, -0.670678, 0.0)], [-0.569322, 0.580241, 0.582402, 0.0)], [0.358422, 0.812733, -0.459345, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.790284, -0.0315036, -0.611929, 0.0)], [-0.510883, 0.585261, 0.629656, 0.0)], [0.338302, 0.810233, -0.478617, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.828626, -0.0174089, -0.559531, 0.0)], [-0.458209, 0.595292, 0.660054, 0.0)], [0.321594, 0.803321, -0.501252, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.862658, -0.00656158, -0.505744, 0.0)], [-0.404869, 0.608273, 0.682703, 0.0)], [0.303151, 0.793701, -0.527388, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.898201, 0.0059859, -0.43954, 0.0)], [-0.337589, 0.631021, 0.698458, 0.0)], [0.28154, 0.775742, -0.564764, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.920434, 0.0197568, -0.390395, 0.0)], [-0.284616, 0.650707, 0.703969, 0.0)], [0.267941, 0.759072, -0.593311, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.940432, 0.041811, -0.337396, 0.0)], [-0.224465, 0.668996, 0.708559, 0.0)], [0.255342, 0.742089, -0.619762, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.960581, 0.0803864, -0.266119, 0.0)], [-0.141218, 0.683463, 0.716193, 0.0)], [0.239455, 0.725546, -0.64517, 0.0)], [0.0, 0.0, 0.0, 1.0)]])) Optional(simd_float4x4([[-0.969532, 0.108638, -0.219551, 0.0)], [-0.0873999, 0.683869, 0.724349, 0.0)], [0.228836, 0.721471, -0.653539, 0.0)], [0.0, 0.0, 0.0, 1.0)]]))

As you can see, The 4th column is always [0.0, 0.0, 0.0, 1.0] even when I am moving my phone around. Can someone help me with understanding this 4x4 matrix (I think it is the camera rotation concatenated with the position in global coordinate i.e. camera extrinsic parameters).

unknown_jy
  • 757
  • 9
  • 19
  • I'm failing to reproduce this on my devices. You might want to try [filing a bug](http://bugreport.apple.com) with a complete project attached. – rickster Oct 03 '17 at 19:58

2 Answers2

3

It can take a while (in my experience, 10–15 seconds on an iPhone 7) for ARKit to get enough camera / motion data to provide positional tracking; until then, it can only give you orientation, and the position will be—as you’re observing—(0,0,0). To handle this, you can implement the session:cameraDidChangeTrackingState: method and check the camera’s trackingState—when it switches from notAvailable or limited to normal, then you can start using the position data.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • I have let my app run for more than a minute and I still don't get anything in the fourth column... and for some reason, I am getting an error "Value of type 'ARCamera' has no member 'trackingState'". – unknown_jy Oct 03 '17 at 17:49
  • Thank you for your help it turned out that I had an outdated SDK. After updating, it all worked! – unknown_jy Oct 04 '17 at 00:42
0

I think the [0,0,0,1] is the 4th row, which stays constant all the time. It is just a padding row to make matrix multiplication work nicely.

jiawen
  • 1,198
  • 2
  • 17
  • 29
  • Answers to this question seems to indicate that you need to look at the third column for position: https://stackoverflow.com/questions/45212598/convert-matrix-float4x4-to-x-y-z-space – Joris van Liempd iDeveloper May 10 '19 at 05:49