首页 文章

创建一个相对于设备定位并朝向 real-world 标题的 SCNNode

提问于
浏览
1

我正在创建一个导航应用程序,它将使用增强现实来帮助用户导航到指定的位置。

我希望在设备前面放置一个SCNNode(箭头,在这种情况下为箭头),略低于 y-axis。箭头将朝向用户导航到的位置。当用户四处走动时,箭头将围绕 y-axis 旋转以保持指向导航位置。

为了让我的箭头朝向 real-world 位置,我必须设置ARWorldTrackingConfiguration.worldAlignment = .gravityAndHeading,但通过这样做我不能再将SCNNode相对于设备的相机放置。

有没有办法让我的 SCNNode 始终在我的相机前,但也朝向 real-world 位置?

我目前的代码:

let arrowScene = SCNScene(named: "art.scnassets/arrow.dae")!
let arrowNode = arrowScene.rootNode.childNode(withName: "arrow", recursively: true)!

let loc = locationManager.location?.coordinate
let bearing = loc?.calculateBearing(to: CLLocationCoordinate2D(latitude: 36.971542, longitude: -82.558492))

arrowNode.position = SCNVector3(0.0, -1.0, 1.5)

arrowNode.transform = SCNMatrix4Mult(arrowNode.transform, SCNMatrix4MakeRotation(Float(bearing!), 0.0, 1.0, 0.0))

sceneView.scene = arrowScene

在此输入图像描述

1 回答

  • 1

    这对我有用:

    首先,获取相机的旋转矩阵:

    let rotate = simd_float4x4(SCNMatrix4MakeRotation(sceneView.session.currentFrame!.camera.eulerAngles.y, 0, 1, 0))
    

    然后,组合矩阵:

    let rotateTransform = simd_mul(r.worldTransform, arrowRotationTransform)
    

    最后,将转换应用于您的节点,转换为 SCNMatrix4:

    node.transform = SCNMatrix4(rotateTransform)
    

    希望有所帮助!!

相关问题