Search code examples
iosswiftaugmented-realityarkit

Define position for SCNNode


I create the SCNBox based on the SCNVector3. But I don't understand the concept of the creation.

For 2D rectangle, the x and y represent the rectangle top right corner. For ARKit 3D SCNBox, what is the x, y and z represent for? Is that represent the center of the SCNBox?


Solution

  • When you're setting position of your SCNNode, you're initializating SCNVector3 which describing node's position.

    You have to define position of node's center point in relation to the position of the parent (center point is not necessarily in the center of your custom node, but if you have basic nodes with geometry like SCNBox or SCNSphere, center point is in the center)

    A simple example can be adding node with geometry SCNBox to rootNode of sceneView's scene which you get when you in Xcode create new Augmented Reality App.

    let cube = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.red
    cube.materials = [material]
    let node = SCNNode()
    node.position = SCNVector3(x: 0, y: 0, z: -0.5)
    node.geometry = cube
    sceneView.scene.rootNode.addChildNode(node)
    

    enter image description here

    As you can see, to initialize SCNVector3 you have to define x, y and z positions of node's center point. Note that units are in meters.

    I created (very nice) picture with x,y and z axises which should help you to understand how to position your SCNNode:

    ...origin is origin of the node's parent's coordinate system when node is added. Image origin of AR world simplified as position of the center of ARSCNView

    enter image description here