title: ARKit Calculate distance from a wall to the camera
body: <p>I’m developing a project with ARKit. I want to calculate the measure from a wall to the camera and it updates when I move away or I move closer.</p>
  
  <p>Now, i have activated that it detects horizontal and vertical surfaces. When I get a surface, I calculate the distance from the camera position and the center of the surface. After I apply the calculus that it gets the distance between 2 points in a 3D space (Euclidean).
   <a href="https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space">https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space</a></p>
  
  <p>Is it correct? Can you help me?</p>
  
  <pre><code>class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate {
  
      let configuration = ARWorldTrackingConfiguration()
  
      override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        configuration.planeDetection = [.horizontal, .vertical]
        sceneView.session.run(configuration)
        ......
      }
  
      func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: 
        CGFloat(planeAnchor.extent.z))
        let planeNode = SCNNode(geometry: plane)
        planeNode.simdPosition = float3(planeAnchor.center.x, 0, 
        planeAnchor.center.z)
        planeNode.eulerAngles.x = -.pi / 2   
        node.addChildNode(planeNode)
        let distance = distanceFromCamera(x: planeAnchor.center.x, y: 0, z: planeAnchor.center.z)
        let formatted = String(format: "Distance: %.2f", distance)
        print(formatted) q 
      }
  
      private func distanceFromCamera(x: Float, y:Float, z:Float) -&gt; Float {
              let cameraPosition =  self.sceneView.session.currentFrame!.camera.transform.columns.3
              print("Camera: \(cameraPosition)")
              let vector = SCNVector3Make(cameraPosition.x - x, cameraPosition.y - y, cameraPosition.z - z)
  
              // Scene units map to meters in ARKit.
              return sqrtf(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)
          }
  }
  </code></pre>