2

I wish to know what direction my camera is facing in ARKit, so that the operation I worked on the objects in front of me is correct (says swipe left to move left).

Right now I have tried using POV direction (does not work as it is always positive) and node direction (photo.worldFront.z - this will work unless rotation is applied, once rotated more than 180 degrees the direction is wrong).

Any suggestions?

Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

1 Answers1

6

You were right looking at pointOfView first.

Hovewer while rotation and eulerAngles both describe what direction the camera is facing, it's not straightforward to use them when working with objects such as placing or moving objects in front of you which is the space of the camera.

Instead, set up your position or movement vector in world space, then transform it to the camera's space using the camera's transform.

In Obj-C it would go like this:

simd_float4x4 transform = self.sceneView.pointOfView.simdTransform;
simd_float4 myPosInWorldSpace = simd_make_float4(0.0, 0.0, -0.5, 1);
simd_float4 myPosInCamSpace = simd_mul(transform, myPosInWorldSpace);

Set the position of an object to myPosInCamSpace. The example above puts the object at z=-0.5 in camera space, which is half meter ahead of the camera in AR.

To move an object that is already in front of the camera to 5 centimeters to the right, goes like this:

simd_float4x4 transform = self.sceneView.pointOfView.simdTransform;
transform.columns[3].xyz=0; //drop cam translation
simd_float4 myVecInWorldSpace = simd_make_float4(0.05, 0.0, 0.0, 1);
simd_float4 myVecInCamSpace = simd_mul(transform, myVecInWorldSpace);

Add myVecInCamSpace to the position of the object in front of you.

First Amendment

If you just need camera yaw in full 360° (−π, π] for logging or whatever:

simd_float4x4 transform = self.sceneView.pointOfView.simdTransform;
simd_float3 cam_right = transform.columns[0].xyz; //for completeness
simd_float3 cam_up = transform.columns[1].xyz;  //for completeness
simd_float3 cam_ahead = transform.columns[2].xyz;
float yaw = atan2f(cam_ahead.x, cam_ahead.z);
diviaki
  • 428
  • 2
  • 15