8

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.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Alain Berrier
  • 621
  • 6
  • 23
  • 2
    Possible duplicate of [ARKit - Get current position of 'camera' in scene](https://stackoverflow.com/questions/45084187/arkit-get-current-position-of-camera-in-scene) – jlsiewert Jul 15 '17 at 17:34
  • 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 Jul 15 '17 at 17:59

2 Answers2

14

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)
orta
  • 4,225
  • 1
  • 26
  • 34
jlsiewert
  • 3,494
  • 18
  • 41
  • That's what I call a true hero ! Thanks a lot ! And if I want to offset the x and y coordinates it's on the 3rd column too ? – Alain Berrier Jul 16 '17 at 13:36
  • 1
    You should read and learn some basics in 3d graphics and [transformation matrices](https://en.wikipedia.org/wiki/Transformation_matrix#Examples_in_3D_computer_graphics) – jlsiewert Jul 16 '17 at 14:22
  • can you replicate in Objective c ? – Devang Tandel Sep 09 '17 at 10:15
  • @Dev_Tandel It would be something like this: ``` 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); ``` – Awesomeness Sep 09 '17 at 14:07
  • @orangenkopf - Thank you i converted your other answer – Devang Tandel Sep 09 '17 at 14:13
  • Hi @jlsiewert! I am wondering whether you could help me with this Scenekit related question: https://stackoverflow.com/questions/63023016/swift-correct-the-arfaceanchor-from-left-handed-to-right-handed-coordinate-syst – swiftlearneer Jul 22 '20 at 03:57
  • Hey @jlsiewert, thank you so much for your detailed answers! I and my team stuck on an issue for days ah :/ I posted an interesting question: https://stackoverflow.com/questions/63662318/arkit-apply-filter-cifilter-to-a-specific-part-vertex-of-an-arfaceanchor Would love your suggestions! – Roi Mulia Aug 31 '20 at 07:30
0

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, w

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

The last element in the columns.3 is the homogeneous coordinate (w), it's equal to 1.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220