7
votes

I'm trying to use ARKit and my question is very simple. How can I get the position of the camera?

I know that if I put a SCNNode on (0,0,0) and my ARCamera is looking on the ground for example, I will have to look up to see the 3D Object. That's mean that somehow the position and the orientation of the camera of the device must be accessible.

So how do I know where the camera is and where she is looking to?

Thanks in advance.

2
That partially answer my question. I understand what the camera transform is, but i don't know how to use it to change the coordinates of my SCNode. Can you explain to me ? :)Alain Berrier

2 Answers

13
votes

You can use the ARCamera.transform property to get the current transform of your camera. The following code is from Introducing ARKit at WWDC 2017 which you probably want to watch as an introduction. We are using the transform to place a node 10 cm in front of the camera.

In Swift:

var translation = matrix_identity_float4x4
translation.columns.3.z = -0.1 // Translate 10 cm in front of the camera
node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)

In Objective-C

matrix_float4x4 translation = matrix_identity_float4x4;
translation.columns[3][2] = -0.1; // Translate 10 cm in front of the camera
node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)
0
votes

Alternatively, you can use the following approach for X, Y and Z axes simultaneously:

var translation = matrix_identity_float4x4

translation.columns.3 = simd_float4(0, 0.2, -0.5, 1)      // x, y, z, HC

aNode.simdTransform = matrix_multiply(translation, 
                                      currentFrame.camera.transform)

The last element in the columns.3 is the homogeneous coordinate, it equals 1.

Hope this helps.