【问题标题】:Cant drag ARKit SCNNode?不能拖动 ARKit SCNNode?
【发布时间】:2018-09-01 15:13:49
【问题描述】:

好的,和其他人一样,我在 ARKit/世界空间中拖动/翻译 SCNNode 时遇到了麻烦。我查看了Dragging SCNNode in ARKit Using SceneKit 和所有热门问题,以及来自 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
}

我试过了:

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

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

【问题讨论】:

  • 看看苹果的newer version of that sample code。现在似乎有了更简单、更清晰的拖动代码(带有手势识别器)。
  • 对 - 我的问题是我不能使用手势识别器,因为我在屏幕上有其他 UI 并且它搞砸了。您是否看到无需平移手势即可拉出/应用的任何内容?
  • SCNAction 有什么问题?我使用了平移手势,但使用SCNAction 进行了平滑过渡。

标签: swift scenekit augmented-reality arkit


【解决方案1】:

我同意@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

【讨论】:

  • 谢谢!必须从 simdposition 更改它,因为它没有从节点中心拖动,但谢谢!
  • 非常欢迎并且总是很有趣地接受我个人多次失败的挑战:)
  • 出于好奇,您为 simdPosition 放了什么?
  • 如果物体不在飞机上而是漂浮在半空中怎么办?
猜你喜欢
  • 2017-11-27
  • 2019-01-30
  • 2018-01-12
  • 2018-08-03
  • 2018-08-04
  • 1970-01-01
  • 2018-01-02
  • 2019-09-22
  • 2017-12-14
相关资源
最近更新 更多