【问题标题】:ARKit – Drag a node along a specific axis (not on a plane)ARKit – 沿特定轴(不在平面上)拖动节点
【发布时间】:2018-03-31 05:03:42
【问题描述】:

我正在尝试将一个节点拖动到我手指在屏幕上Y 轴上的确切位置。但是我找不到任何方法,这是我当前的代码。你有什么想法吗?

var movedObject:SCNNode?

@objc func handlePan(_ recognizer: UIPanGestureRecognizer) {

    if recognizer.state == .began {

        let tapPoint: CGPoint = recognizer.location(in: sceneView)
        let result = sceneView.hitTest(tapPoint, options: nil)
        if result.count == 0 {
            return
        }
        let hitResult: SCNHitTestResult? = result.first
        movedObject = hitResult?.node //.parent?.parent
        } else if recognizer.state == .changed {

        if (movedObject != nil) {
            let tapPoint: CGPoint = recognizer.location(in: sceneView)
            let hitResults = sceneView.hitTest(tapPoint, types: .featurePoint)
            let result: ARHitTestResult? = hitResults.last

            if result?.worldTransform != nil{                      
                let matrix: SCNMatrix4 = SCNMatrix4.init((result?.worldTransform)!)
                let vector: SCNVector3 = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43)
                movedObject?.position = vector    
            }                   
        }
    } else if recognizer.state == .ended {
        movedObject = nil
    }  
}

【问题讨论】:

    标签: ios swift scenekit augmented-reality arkit


    【解决方案1】:

    找到解决方案!我在cmets中添加了一些解释。

    if (movedObject != nil) {
    
        //Normalized-depth coordinate matching the plane I want
        let projectedOrigin = sceneView.projectPoint((movedObject?.position)!)
    
        //Location of the finger in the view on a 2D plane
        let location2D = recognizer.location(in: sceneView)
    
        //Location of the finger in a 3D vector
        let location3D = SCNVector3Make(Float(location2D.x), Float(location2D.y), projectedOrigin.z)
    
        //Unprojects a point from the 2D pixel coordinate system of the renderer to the 3D world coordinate system of the scene
        let realLocation3D = sceneView.unprojectPoint(location3D)
    
        if movedObject?.position != nil {
    
            //Only updating Y axis position
            movedObject?.position = SCNVector3Make((movedObject?.position.x)!, realLocation3D.y, (movedObject?.position.z)!)
        }
    
    }
    

    有助于理解的帖子:

    unProjectPoint & projectedOrigin

    translation & unProjectPoint

    【讨论】:

    • 我们如何沿任意轴移动它?我不想将运动限制在特定的轴上。也可以用手势旋转选定的子节点吗?你能帮帮我吗?
    猜你喜欢
    • 2017-12-25
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    相关资源
    最近更新 更多