首页 文章

在 ARKit 中查找飞机位置

提问于
浏览
0

我试图在我的 ARKit 应用中找到最接近的飞机的位置。我写了一些代码来帮助找到它,但是由于某种原因,当我运行我的应用程序时,当我尝试向飞机添加一个 AR 对象时,它总是崩溃。我的代码有问题吗?

struct myPlaneCoords {

    var x = Float()
    var y = Float()
    var z = Float()
}

func getPlaneCoordinates(sceneView: ARSCNView) -> myPlaneCoords{//coordinates where an AR node will be added

    let cameraTransform = sceneView.session.currentFrame?.camera.transform
    let cameraCoordinates = MDLTransform(matrix: cameraTransform!)

    let camX = CGFloat(cameraCoordinates.translation.x)
    let camY = CGFloat(cameraCoordinates.translation.y)
    let cameraPosition = CGPoint(x: camX, y: camY)
    let anchors = sceneView.hitTest(cameraPosition, types: ARHitTestResult.ResultType.existingPlane)
    let spefAnchor = MDLTransform(matrix: anchors[0].localTransform)//finds closest plane

    var cc = myPlaneCoords()
    cc.x = spefAnchor.translation.x
    cc.y = spefAnchor.translation.y
    cc.z = spefAnchor.translation.z

    return cc  
}

1 回答

  • 0

    难以判断 w/o 异常描述。

    我可以假设 hitTest 没有检测到任何锚点。在这种情况下,您的anchors为空。

    let spefAnchor = MDLTransform(matrix: anchors[0].localTransform)//finds closest plane
    

    在这里,您应该崩溃。

相关问题