Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARKit – Position of the ARCamera

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.

like image 736
Alain Berrier Avatar asked Jul 15 '17 16:07

Alain Berrier


1 Answers

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)
like image 140
jlsiewert Avatar answered Oct 04 '22 14:10

jlsiewert