Sunday, November 8, 2026
HomeiOS Developmentios - Methods to briefly freeze a node in entrance of the...

ios – Methods to briefly freeze a node in entrance of the digicam utilizing ARKit, SceneKit in Swift

[ad_1]

I constructed a whole construction as a node (with its youngster nodes) and the person will stroll by way of it utilizing ARKit.
Sooner or later, if the person can’t proceed due to some actual impediment in the actual world, I added a “pause” button which ought to freeze regardless of the person presently sees in entrance of the digicam, the person may then transfer freely to another open area and when the person will launch the pause button he/she’s going to be capable of resume the place they left off (solely someplace else in the actual world).

Some time in the past I requested about it within the Apple Developer discussion board and an Apple Frameworks Engineer gave the next reply:

For “freezing” the scene, you could possibly rework the anchor’s place (in world coordinates) to digicam coordinates, after which anchor your content material to the digicam. This will provide you with the impact that the scene is “frozen”, i.e., doesn’t transfer relative to the digicam.

I am presently not utilizing an anchor as a result of I do not essentially have to discover a flat floor. Relatively, my node is positioned at a sure place relative to the place we begin at (0,0,0).

My query is how do I precisely do what the Apple engineer advised me to do?
I’ve the next code which I am nonetheless caught with. After I add the node to the digicam (pointOfView, final line of the code beneath), it does freeze in place, however I can not get it to freeze in the identical place and orientation because it was earlier than it was frozen.

    @IBAction func pauseButtonClicked(_ sender: UIButton) {

    let currentPosition = sceneView.pointOfView?.place
    let currentEulerAngles = sceneView.pointOfView?.eulerAngles
    
    var internalNodeTraversal = lastNodeRootPosition - currentPosition!     // for now, lastNodeRootPosition is (0,0,0)         
    internalNodeTraversal.y = lastNodeRootPosition.y + 20       // simply so it’s positioned a bit increased in entrance of the digicam

    myNode?.removeFromParentNode()      // take away the node from the Actual World view.  Appears like this line has no impact and simply including the node as a baby to the digicam (pointOfView) is sufficient, but it surely feels extra proper to do that anyway.
    
    myNode?.place = internalNodeTraversal        // the entire node is moved respectively in the other way from the foundation to the place I’m standing to reposition the digicam in my present place contained in the node
    
 //       myNode?.eulerAngles = (currentEulerAngles! * -1)      — this code put the entire node in bizarre positions so I eliminated it
        myNode?.eulerAngles.y = currentEulerAngles!.y * -1  // reverse orientation of the node so the digicam might be oriented in the identical path
        myNode?.eulerAngles.x = 0.3     // simply tilting it up a bit bit to have a greater view, extra just like the view as earlier than it was locked to the digicam
// I don’t assume I would like to alter the eulerAngles.z
        

    myNode!.convertPosition(internalNodeTraversal, to: sceneView.pointOfView)       // I’m unsure I wrote this appropriately.  Additionally, this line doesn’t appear tp change something
    
    sceneView.pointOfView?.addChildNode(myNode!)        // attaching the node to the digicam so it can stay caught whereas the person strikes round till the button is launched
}

So I first calculate the place within the node I am presently standing after which I alter the place of the node in the other way in order that the digicam will now be in that place. That appears to be right.
Now I would like to alter the orientation of the node so that it’s going to level in the fitting path and right here issues get funky. I have been attempting so many issues for days now.
I take advantage of the eulerAngles for the orientation. If I set the entire vector multiplied by -1, it could present bizarre orientations. I ended up solely utilizing the eulerAngles.y which is the left/proper orientation and I hardcoded the x orientation (up/down).

Finally what I’ve within the code above is the closest that I used to be in a position to get. If I am pointing straight, the freeze might be right. If I flip just a bit bit, the freeze might be fairly shut as effectively. Virtually the identical as what the person noticed earlier than the freeze. However the extra I flip, the extra the frozen picture is off and extra slanted. Sooner or later (say I flip 50 or 60 levels to the facet) the entire node is off the digicam and can’t be seen.

By some means I’ve a sense that there should be a neater and extra right approach to obtain the above.
The Apple engineer wrote to “rework the anchor’s place (in world coordinates) to digicam coordinates”. For that purpose I added the “convertPosition” perform in my code, however a) I am unsure I used it appropriately and b) it would not appear to alter something in my code if I’ve that line or not.

What am I doing fallacious?
Any assist can be very a lot appreciated.
Thanks!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments