ARKit: 3d Object keep moving

When I added a 3D object. for some reason it keeps moving from time to time. Can anyone tell me why would that happened?

ARSCNViewDelegate:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

DispatchQueue.main.async {
let planeNode = self.createARPlaneNode(center: planeAnchor.center, extent: planeAnchor.extent)

node.addChildNode(planeNode)

}

}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

DispatchQueue.main.async {
self.updateARPlaneNode(planeNode: node.childNodes[0], planeAnchor: planeAnchor)
}

}

method of adding the 3d object:
func loadVirtualKit(translation: simd_float4) {
guard !virtualKitInPlaced else {
return
}
guard let scene = SCNScene(named: “Models.scnassets/virtualkit.scn”)

else { return }

scene.isPaused = false

if let node = scene.rootNode.childNode(withName: “virtualkit” , recursively: false) {

self.virtualKitNode = node
self.virtualKitNode.position = SCNVector3(translation.x, translation.y, translation.z)

virtualKitView.scene = scene
virtualKitInPlaced = true

}
}

this is where it gets added:
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
if let touch = touches.first {
guard touch.view == self.virtualKitView else { return }

let tapLocation: CGPoint = touch.location(in: virtualKitView)
  guard let hit = virtualKitView.hitTest(tapLocation, types: .existingPlaneUsingExtent).first else { return }
  
  let translation = hit.worldTransform.columns.3
  
  DispatchQueue.main.async {
    self.loadVirtualKit(translation: translation)
    self.stopPlaneDetection()
  }

}
}

This topic was automatically closed after 166 days. New replies are no longer allowed.