Group Group Group Group Group Group Group Group Group

Chapter 8 Anchors placement

There is a question i have regarding this piece of code. [Question Below the code]

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
	DispatchQueue.main.async {
		if let planeAnchor = anchor as? ARPlaneAnchor, !self.isPortalPlaced {
			#if DEBUG
			let debugPlaneNode = createPlaneNode(center: planeAnchor.center, 
                                                                                                        extent: planeAnchor.extent)
			print("Inside PlaneAnchor")
			node.addChildNode(debugPlaneNode)
			self.debugPlanes.append(debugPlaneNode)
			#endif
			self.messageLabel?.alpha = 1.0
			self.messageLabel?.text = """
			Tap on the detected \
			horizontal plane to place the portal
			"""
		} else if !self.isPortalPlaced {
			self.portalNode = self.makePortal()
			print("Inside Portal")
			if let portal = self.portalNode {
				node.addChildNode(portal)
				self.isPortalPlaced = true
				
				self.removeDebugPlanes()
				self.sceneView?.debugOptions = []
				DispatchQueue.main.async {
					self.messageLabel?.alpha = 0
					self.messageLabel?.text = ""
				}
			}
		}
	}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let hit = sceneView?.hitTest(viewCenter, types: [.existingPlaneUsingExtent]).first {
sceneView?.session.add(anchor: ARAnchor.init(transform: hit.worldTransform)) }
}

So when we are creating a planeAnchor, right after DispatchQueue, the render tells us that we have an anchor and it sets a plane in the horizontal space, but when we tap to create a portal all of a sudden the anchor is nil. How is this possible? In the touches began method it says that Anchor.init(transform) will create a new anchor, yet when i set a breakpoint to see if there is a planeAnchor it comes as nil and then jumps straight to the else if statement. Shouldn’t we have an anchor when touches began was tapped? At what part of the lifecycle does the anchor disappear?

@namrata.bandekar Can you please help with this when you get a chance? Thank you - much appreciated! :]

Possibly the issue is that the plane that was added doesn’t intersect with the hitTest at viewCenter?