简体   繁体   English

两个节点之间 Y 的 ARKit 距离

[英]ARKit Distance of Y between two nodes

So I have successfully detected the planes.所以我已经成功检测到飞机。 My goal is to measure distance of height between camera node and detected plane when phone is flat rotated (rotated like plane (horizontally)) but it is not accurate result because of phone angle ie some time it is not flat like slightly up or down.我的目标是在手机平旋转(像平面一样旋转(水平))时测量相机节点和检测平面之间的高度距离,但由于手机角度,它不是准确的结果,即有时它不像稍微向上或向下那样平坦。

Here is what I tried.这是我尝试过的。

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)

Any one can help me to improve it.任何人都可以帮助我改进它。 Or how can I take camera rotation in account.或者我如何考虑相机旋转。 as Plane is rotated 90 degree in X. So I need to match that rotation to of camera to get exact value因为平面在 X 方向上旋转了 90 度。所以我需要将该旋转与相机的旋转相匹配以获得准确的值

Hey im not sure if you want to detect shortest distance to plane or to center of plane?嘿,我不确定您是要检测到平面还是到平面中心的最短距离? This will hopefully help.这将有望有所帮助。

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM