簡體   English   中英

兩個節點之間 Y 的 ARKit 距離

[英]ARKit Distance of Y between two nodes

所以我已經成功檢測到飛機。 我的目標是在手機平旋轉(像平面一樣旋轉(水平))時測量相機節點和檢測平面之間的高度距離,但由於手機角度,它不是准確的結果,即有時它不像稍微向上或向下那樣平坦。

這是我嘗試過的。

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 度。所以我需要將該旋轉與相機的旋轉相匹配以獲得准確的值

嘿,我不確定您是要檢測到平面還是到平面中心的最短距離? 這將有望有所幫助。

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