简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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