Glitch Bug in Pan Gesture ARSceneView

I have an issues in my pan gesture in ARScnView, I have a SCNNode that is a child of some SCNNode and I want to pan/move SCNNode child in the parent of its SCNNode, the pan gesture working fine but sometimes there was a bug where my SCNNode child position move to unintended position (like move to the center), and i don't know why it move to the center, as i know i have given my SCNNode child position to the hitresult in SCNNode parent, but everytime there was a glitch it back to the center position, i don't know who position is.

this how i give the position of SCNNode child

@objc func tapGesture(_ gesture: UITapGestureRecognizer) {
    let location: CGPoint = gesture.location(in: artboardARView)
    let hitTestResults = self.artboardARView.hitTest(location, options: [:])
    if let hitTest = hitTestResults.first else {return}

    if !hitTestResults.isEmpty {
        let selectedKey = arts.lastIndex(where: { $0.status == .selected})
        if selectedKey != nil && arts[selectedKey!].artNode == nil {
            placingArt(selectedKey!, hitTest)

How i set the position to the SCNNode child

  let testLoc = hit.localCoordinates
   artPrepareNode.position = SCNVector3Make(testLoc.x, 0.00001, -testLoc.y)

this how i pan my SCNNode

@objc func panGesture(_ gesture: UIPanGestureRecognizer) {
    let location = gesture.location(in: artboardARView)
    guard let id = arts.lastIndex(where: { $0.status == .selected}) else {return}
    let hitTestResults = self.artboardARView.hitTest(location, options: [:])
    guard let hitTest = hitTestResults.first else {return}
    let invertedY = hitTest.localCoordinates.y * -1
    if arts[id].artNode != nil {
        switch gesture.state {
        case .began:
            onMove = arts[id].artNode
            onMove?.position = arts[id].artNode!.position
        case .changed:
            if let move = onMove {
                move.position = SCNVector3Make(hitTest.localCoordinates.x, 0.00001, invertedY)
        case .ended:
            onMove = nil

Read more here:

Content Attribution

This content was originally published by Danny at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: