2017-10-19 1 views
1

축 y에서 화면에 내 손가락이있는 노드를 정확하게 드래그하려고합니다. 그러나 나는 그것을 할 어떤 방법도 찾지 못했고, 여기에 나의 현재 코드가있다. 너는 어떤 생각을 가지고 있니?ARKit - 평면이 아닌 특정 축을 따라 노드 드래그

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 
     } 

} 

답변

3

해결책을 찾았습니다! 코멘트에 몇 가지 설명을 추가했습니다. 도움이

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

관련 문제