ARKit两个节点之间的Y距离

时间:2018-10-25 10:43:26

标签: ios swift4 arkit

因此我已成功检测到飞机。我的目标是在手机水平旋转(像平面旋转(水平)旋转)时测量相机节点与检测到的平面之间的高度距离,但是由于手机角度的缘故,它不是准确的结果,即有些时候它不平坦,例如稍微向上或向下。

这是我尝试过的。

guard let camera = self.sceneView.session.currentFrame?.camera, let plane = self.sceneView.hitTest(self.sceneView.center, types: [.existingPlaneUsingGeometry]).first else {
            return
        }


        var anchorPosition = plane.anchor?.transform.columns.3
        var cameraPosition = camera.transform.columns.3

        anchorPosition?.x = cameraPosition.x
        anchorPosition?.z = cameraPosition.z

        let cameraToAnchor = cameraPosition - (anchorPosition ?? float4())
        // and here’s just the scalar distance
        let distance = length(cameraToAnchor)

任何人都可以帮助我改善它。或者如何考虑相机旋转。因为飞机在X中旋转了90度。所以我需要将该旋转与摄影机的旋转角度相匹配才能获得准确的值

1 个答案:

答案 0 :(得分:0)

嘿,我不确定是否要检测距飞机或飞机中心的最短距离?希望会有所帮助。

extension ARSCNView {
       func distance(node1: SCNNode, node2: SCNNode) -> Double {
        let x1 = node1.worldTransform.m41
        let y1 = node1.worldTransform.m42
        let z1 = node1.worldTransform.m43

        let x2 = node2.worldTransform.m41
        let y2 = node2.worldTransform.m42
        let z2 = node2.worldTransform.m43

        return Double(pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2))
    }

    func distanceToCamera(node: SCNNode) -> Double {
        //Point of view should be where camera is
        if let pointOfView = self.pointOfView {
             return distance(node1: node, node2: pointOfView)
        }
        print("Cant get pointOfView of node")
        return 0
    }
}