Search code examples
swiftaugmented-realityscenekitarkitrealitykit

Change Y-position of the Anchor to keep it grounded


I want to put an anchor with mesh on the floor. Sometimes it does not pricesly set on the ground. Few inches of space is there between object and ground. How to change the Y of the object so it will be grounded on the floor.

I am thinking of detecting a horizontal plane below the object. But how to convert the position of an object? I do not want to put the object on the plane (It may move if plane moves)

enter image description here


Solution

  • Whether you're using RealityKit or ARKit, you definitely need to use plane detection feature. If your app detects a plane in RealityKit, it will automatically track a plane target. And I should say that a detected plane doesn't move (you're using World Tracking, aren't you), but it may be extended.

    AnchorEntity(.plane([.any], classification: [.any], minimumBounds: [0.5, 0.5]))
    

    In case you're using a plane detection feature in ARKit/SceneKit, you must additionally implement session(_:didAdd:) or renderer(_:didAdd:for:) delegate method (because ARKit can't do a job automatically):

    let config = ARWorldTrackingConfiguration()
    config.planeDetection = [.horizontal, .vertical]
    sceneView.session.run(config)
    
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        guard let planeAnchor = anchors.first as? ARPlaneAnchor else { return }
    }
    

    For manual object placement use raycasting.

    Also, you have to correctly position model's pivot point in 3D authoring app.