Search code examples
iosarkitscnnode

ARKit - positioning a node in the world relative to world origin


At the moment, my code loads in a 3D model, creates a node using that model, and displays the node in the scene. Setting the scale/rotation (euler angles) of the node works fine. However, I'm trying to set the position of the node relative to the world origin, and I don't want the node to be attached to a plane.

I've tried setting node.position and node.worldPosition to no avail; although the position of the node changes, when the camera moves, the node doesn't stay static, but moves about with the camera. I'm new to using ARKit, so I'm probably doing something stupid, but I can't figure out what it is that I need to do, so any help would be much appreciated.

Edit: The weird thing is that if I set the coordinates to say SCNVector3(0, 3, 0) it's fine, but if I go over a certain number of meters away it seems to fail. Is this expected


Solution

  • Firstly, poor world tracking can be caused by these common issues.

    1. Poor ligthing. Causes low number of feature points available for tracking

    2. Lack of texture Causes low number of feature points available for tracking

    3. Fast Movement Causes blurry images which causes tracking to fail

    However, what I believe is happening in your case (which is a little more tricky to debug)... is you are most likely placing the loaded model underneath the detected horizontal plane in the scene.

    In other words, you may have positioned the SCNNode using a negative Y coordinate which places the node below the horizontal detected plane and causes the model to drift around as you change the cameraView

    try setting the Y position of the node to either 0 or a small positive value like 0.1 metres

    node.position = SCNVector3(0, 0, -1)  // SceneKit/AR coordinates are in meters
    
    sceneView.scene.rootNode.addChildNode(node)
    

    z = -1 places the SCNNode 1 metre in front of your cameraView.

    Note: I verified this issue myself using a playground I use for testing purposes.