8

I've begun experimenting with ARKit, and I have ran into an issue. I can't find a tutorial which shows how to place an object on the nearest surface.

Let's say I am using a .scn file that contains a flat object. I know how to place that where the camera is, but how would I get the distance to the nearest surface.

Such as in this image, where it detects how far away the table is.

Sorry if this sounds like a code-request, but I am not sure where to start, any guidance would be much appreciated

Will
  • 4,942
  • 2
  • 22
  • 47
  • If you're still interested in a tutorial, and for future people who check this question, there are 2 that really helped me: https://www.youtube.com/watch?v=bfAadJNX3Tc as well as this 4 part tutorial: https://blog.markdaws.net/apple-arkit-by-example-ef1c8578fb59. They both cover material that is essential in learning ARKit and are two different ways of placing objects and such. – Alan Aug 17 '17 at 08:31
  • Hi anybody tried placing a focus square image like this? – Tommy Sep 14 '20 at 10:36

1 Answers1

14

You can use the hitTest that might return you results that contains informations such as distance and transform: https://developer.apple.com/documentation/arkit/arhittestresult

for result in sceneView.hitTest(CGPoint(x: 0.5, y: 0.5), types: [.existingPlaneUsingExtent, .featurePoint]) {
  print(result.distance, result.worldTransform)
}
Guig
  • 9,891
  • 7
  • 64
  • 126