I have an AR scene (built as a node with children nodes) that I want the user to walk inside it. Because it can be large, I added a pause button in the corner so that if the user hits an obstacle in the real world, he'll just need to press-and-hold that button to freeze the scene, move elsewhere in the real world, and when the button will be released the scene would have moved as-is to that new location, retaining the same relative position and rotation inside that scene.
What I did is very simple. When button is touched down I just did:
sceneView.session.pause()
And when the button is Touch Up Inside I ran:
sceneView.session.run(configuration)
This seemed to do the trick.
However, after I clicked and moved once or twice more and moved back to where I was before, my whole AR scene suddenly jumped back to its previous location in the real world. So it did freeze and showed up in the new location for a second or two, but then it jumped back.
So I tried to resume the session with several of the available options.
If I ran:
sceneView.session.run(configuration, options: [.resetTracking])
the whole scene would move to the new location correctly as I wanted, but it would be set to its initial position in front of the camera (as opposed to being already "inside" the node) and rotation as if I was standing at (0,0,0) when I started the app, but now moved to the new location. In other words, it lost the current location and rotation that I was at when I pressed the pause button and just reset the scene.
All the other options that I tried had the same effect as if I didn't add any options.
I should add that I'm not using any anchors (so far) nor any ray casting. I didn't try different combinations of those options (except removeExistingAnchors and resetTracking together), but I doubt that that would help. Am I wrong?
What am I doing wrong? Anyone have a solution for my problem?
Or will I need to manually record the current position and rotation when pressing the pause button and then restore those with some position change functions of the node? I was hoping I could skip having to do it this way. And if I do need to do it manually, any tips on how to achieve this?
Thanks for your help!
You are seeing the effect of ARKit attempting to relocalize when the session is resumed. For your use case, you should not pause the session at all. What you essentially want to do is move AR content from one location to another (even if it is an entire hierarchy of nodes you are moving). The key to achieve that is raycasting and anchors. Basically you want to do the following:
- Create an ARAnchor at the location where you initially position your scene's root node. As the user should be able to walk through the scene, it probably makes sense that this anchor is located on the floor. To find a suitable location for placing your content, you could turn on plane detection and then perform a raycast with an
.existingPlane
or.estimatedPlane
target, and.horizontal
alignment. - For "freezing" the scene, you could transform the anchor's position (in world coordinates) to camera coordinates, and then anchor your content to the camera. This will give you the effect that the scene is "frozen", i.e., does not move relative to the camera. Alternatively, you could continuously perform raycasts while the user keeps the button pressed, and use the raycast result for updating the position. This will still keep the content in front your camera, however with a "valid" placement on the floor.
- For releasing the scene at the new location, you can do the same as in Step 1.