【问题标题】:How to limit SCNNode from moving when camera moves along the x-axis?相机沿x轴移动时如何限制SCNNode移动?
【发布时间】:2021-07-12 09:43:13
【问题描述】:

我正在使用 ARKit 开发一个简单的应用程序,用户可以在其中点击屏幕并将节点 (SCNNode) 放置在给定位置。我希望用户能够放置无论相机在哪里都保持原位的节点,这样当他们平移回他们放置节点的位置时,它仍然在那里。

我已经让点击功能正常工作,但我注意到当我沿 x 轴物理移动设备时,放置的节点也会随之移动。我试图将节点锚定到根节点以外的地方,但它没有按预期工作。我试图查找有关如何放置根节点以及是否基于相机计算的文档,这将解释为什么节点会随着相机移动,但那里也没有运气。

这是放置节点的代码。节点位置使用scenePoint 放置,这是从触摸位置到使用SceneKit: unprojectPoint returns same/similar point no matter where you touch screen 完成的场景的投影。

let nodeImg = SCNNode(geometry: SCNSphere(radius: 0.05))
nodeImg.physicsBody? = .static()
nodeImg.geometry?.materials.first?.diffuse.contents = hexColor
nodeImg.geometry?.materials.first?.specular.contents = UIColor.white
nodeImg.position = SCNVector3(scenePoint.x, scenePoint.y, scenePoint.z)
print(nodeImg.position)
            
sceneView.scene.rootNode.addChildNode(nodeImg)

我认为这与我将 nodeImg 节点作为子节点添加到 rootNode 的事实有关,但我不确定将它锚定到什么。

【问题讨论】:

    标签: ios swift arkit scnnode


    【解决方案1】:

    点击时您需要设置节点的“worldPosition”,而不仅仅是“位置”

    查看此链接:ARKit: position vs worldposition vs simdposition

    假设你已经设置了 sceneView.allowsCameraControl = false

    @objc func handleTapGesture(withGestureRecognizer recognizer: UITapGestureRecognizer) {
        let location: CGPoint = recognizer.location(in: self.sceneView)
        let hits = self.sceneView.hitTest(location, options: nil)
        if let tappednode = hits.first?.node {
            nodeImg.worldPosition = tappednode.worldPosition
            self.sceneView.scene.rootNode.addChildNode(nodeImg)
       }
    }
    

    【讨论】:

    • 不适合我。我尝试设置位置和世界位置。我每帧打印位置/世界位置,并且坐标在日志中没有改变。但我投射的线仍然随着相机移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2013-03-17
    • 2021-03-10
    • 2022-10-06
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多