SCNNode won't stay in place

Hey,


So i'm using ARKit and detecting planes for the user to place an object. Once the plane is detected I display a button which when pressed run's the addBowlingLane() function to place it. The problem i'm having is when I walk away or around the object follows me which defeats the purpose of how i'm designing the app. So my question, is there any way for me to place an object and have it stay in that particular spot?


Thanks!


here's the relevant code


  1. func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
  2. /
  3. guard let planeAnchor = anchor as? ARPlaneAnchor,
  4. let planeNode = node.childNodes.first,
  5. let plane = planeNode.geometry as? SCNPlane
  6. else { return }
  7. /
  8. planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
  9. /
  10. plane.width = CGFloat(planeAnchor.extent.x)
  11. plane.height = CGFloat(planeAnchor.extent.z)
  12. }
  13. func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
  14. /
  15. guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
  16. if(!hasLaneBeenAnchored){
  17. let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))
  18. planeNode = SCNNode(geometry: plane)
  19. planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
  20. planeNode.eulerAngles.x = -.pi / 2
  21. planeNode.opacity = 0.25
  22. planeNode.geometry?.materials.first?.diffuse.contents = UIColor.init(red: 255.0/255.0, green: 146.0/255.0, blue: 237.0/255.0, alpha: 1.0)
  23. node.addChildNode(planeNode)
  24. }
  25. }
  26. @objc func addBowlingLane(){
  27. let bScene = bowlingScene?.rootNode
  28. bScene?.position = planeNode.position
  29. sceneView.scene.rootNode.addChildNode(bScene!)
  30. hasLaneBeenAnchored = true
  31. instructionsLabel.text = "Back away from the\nbowling pins until you are at the edge of the bowling\n lane swipe up to bowl! "
  32. placeLaneBtn.isHidden = true
  33. }

Replies

I'm not sure why it won't stay in place but plenty of times a lot of my problems have been solved by attaching things to node instead of equating their positions.

So instead of,

bScene?.position = planeNode.position
sceneView.scene.rootNode.addChildNode(bScene!)


Maybe try

planeNode.addChildNode(bScene)