[英]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.