不能拖动 ARKit SCNNode?

Cant drag ARKit SCNNode?

好的,和其他人一样,我在 ARKit/world space 中遇到 dragging/translating SCNNode 的问题。我看过 Dragging SCNNode in ARKit Using SceneKit and all the popular questions, as well as the code from Apple https://github.com/gao0122/ARKit-Example-by-Apple/blob/master/ARKitExample/VirtualObject.swift

我尝试尽可能地简化,只是做了我在普通场景套件游戏中会做的事情 - http://dayoftheindie.com/tutorials/3d-games-graphics/t-scenekit-3d-picking-dragging/

我可以获取点击的对象并存储当前手指位置没问题:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])

        //TAP Test
        let hits = gameView.hitTest(touch.location(in: gameView), options: nil)
        currentTapPos = getARPos(hitFeature: hitFeature) //TAP POSITION

        if let tappedNode = hits.first?.node {

但是,我的问题是,在更新中执行此操作 - 没有动画。无论我点击哪里,该对象都会出现,并且总体上不起作用 -

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last else { return }

        testNode.position = currentTapPos

我用这个函数转换为 SCNVector3:

func getARPos(hitFeature: ARHitTestResult)->SCNVector3
{

    let hitTransform = SCNMatrix4.init(hitFeature.worldTransform)
    let hitPosition = SCNVector3Make(hitTransform.m41,
                                     hitTransform.m42,
                                     hitTransform.m43)
    return hitPosition
}

我试过:

-按矢量翻译 -平移手势(这搞砸了其他功能) -SCNAction.moveTo

我可以在这里做什么?怎么了?

我同意@Rickster 的观点,Apple Code 提供了一个更强大的例子,但是,我有这个并且它似乎工作顺利(当你使用 PlaneDetection 时更是如此(因为特征点很多更零星):

我没有使用 touchesBegan,而是简单地在 touchesMoved 中完成工作:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    //1. Get The Current Touch Point
    guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
        //2. Get The Next Feature Point Etc
        let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

    //3. Convert To World Coordinates
    let worldTransform = hitTest.worldTransform

    //4. Set The New Position
    let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

    //5. Apply To The Node
    nodeToDrag.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)

}

我可能会以完全错误的方式解决这个问题(如果我是,我很乐意得到反馈)。

无论如何,我希望它能有所帮助...您可以在此处观看它的快速视频:Dragging SCNNode