swiftscenekitarkitscnscenearscnview

Get the position of a sphere created in scn file


I created an scn file using Xcode (as you can see below), where there are two nodes (a sphere and a platform). I set up the position of the sphere:

SCN editor window showing a sphere Readout of the sphere position 0.074 0.391 0.392

I used a class which creates a node platform, which you can resize and move:

Screenshot

After pressing a button it places my scn file instead of that platform (with the same scale, position, and orientation) The problem is I don't know how to get sphere's position from the scene view.

  let sphere = SCNNode()
  self.scene.parent!.enumerateChildNodes { (node, _) in
        if node.name == "sphere" {
            sphere = node
            sphere.position = node.position
            print(sphere.position)
        }
    }

Using that code it always prints the position that I set up in the scn file (as you can see above), but I want the real position of it in the view.

ScreenShot


Solution

  • With sphere.position you will get the position of sphere relative to its parent node. If you want the position in the scene you have to use sphere.worldPosition.