Tung Nguyen
Tung Nguyen

Reputation: 644

how to get real-world center coordinate of a scene in ARkit

In ARKit, if I have a ARSCNView, how do I get the real-world center coordinate of the current scene? I am using Swift as the programming language.

Upvotes: 2

Views: 3615

Answers (2)

smartdog
smartdog

Reputation: 123

Tung Nguyen,

Hope your having a nice day :-)

I going to try to help ( just not sure exactly what your looking for )...but I think I have some clues ;-)

I am going to give you hints and see if I can help:

The question: “how-to-get-real-world-center-coordinate-of-a-scene-in-arkit ?”

1) if you are asking about the center of the real world coordinate system: Here is how you see it:

https://developer.apple.com/documentation/scenekit/scndebugoptions/2882039-showworldorigin

How to use in code: Within the viewDidLoad function

sceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin]

How to get the world origin position? If I understand what you’re asking for, the real world center for ARKit is The position and orientation of the device as of when the session configuration is first run. The coordinates would be (0,0,0). [ If it does not make sense to you, let me know ( I know I am skipping a lot but on iPad typing).]

Here is how to position something in the real world center (we know the center of the world is at the coordinates (0,0,0))

How to use in code;

func addBox() {
// create a SceneKit box shape
    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

// create a SceneKit node 
    let boxNode = SCNNode()
// attach the shape to the node
    boxNode.geometry = box
// give node a position ( this will be the real world center!
    boxNode.position = SCNVector3(0, 0, 0)

// SCNVector3 you can think of for now as SCNVector3(x,y,z)
//https://developer.apple.com/documentation/scenekit/scnvector3

// create a SceneKit scene 
    let scene = SCNScene()

// add the node as a child of the rootNode
// the root node defines the origin of the world coordinate systems for SceneKit.
//Apple Documentation states:"The world coordinate system of the view's    
//SceneKit scene directly responds to the AR world coordinate system    
//established by the session configuration."
// so ARKit and SceneKit world coordinate systems match :-)

    scene.rootNode.addChildNode(boxNode)

//add the SceneKit scene to sceneView
    sceneView.scene = scene
}

So my first answer to you is to keep the positioning a variable. How to use in code:

let ARWorldOrigin = SCNVector3(0, 0, 0)

Use this to make all your measurements

1)Did you mean center of physical screen?

2) did you mean the center of camera’s viewing frustum (Try this stackflow page for hints: Get All ARAnchors of focused Camera in ARKIT

3) Follow all links from this stackoverflow question: Which measuring unit is used in SCNVector3 position for x, y and z in ARKit

I hope I was able helped in some way and understand your question:

Have nice day! And hope ARkit programming

We all learn by suring what we know.

Smartdog

Upvotes: 0

Pratap
Pratap

Reputation: 285

you can get touch point by using gesture like this

// 1.
@objc func tapped(recognizer :UIGestureRecognizer) {
    // Get exact position where touch happened on screen of iPhone (2D coordinate)
    let touchPosition = recognizer.location(in: sceneView)

    // 2.
    // Conduct a hit test based on a feature point that ARKit detected to find out what 3D point this 2D coordinate relates to
    let hitTestResult = sceneView.hitTest(touchPosition, types: .featurePoint)

    // 3.
    if !hitTestResult.isEmpty {
        guard let hitResult = hitTestResult.first else {
            return
        }
        print(hitResult.worldTransform.columns.3)
    }
}

and you can get point position

let positionOfObjectToAdd=SCNVector3(hitResult.worldTransform.columns.3.x,hitResult.wor

ldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)

Upvotes: 1

Related Questions