ARKit – Drag a node along a specific axis (not on a plane)

前端 未结 1 1200
[愿得一人]
[愿得一人] 2020-12-15 14:41

I am trying to drag a node exactly where my finger is on the screen on the Y-axis. But I don\'t find any way to do it, here is my current code. Do you have any

相关标签:
1条回答
  • 2020-12-15 15:18

    Found the solution ! I added some explanations in comments.

    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)!)
        }
    
    }
    

    Posts that help understand:

    unProjectPoint & projectedOrigin

    translation & unProjectPoint

    0 讨论(0)
提交回复
热议问题