Measuring distance with ARCore

Shibui Yusuke
3 min readFeb 16, 2020

Expanding world with AR

Augmented Reality allows us to programmatically work with 3 dimensional world using camera-equipped device. ARCore is one of the libraries for augmented reality that provides simple and powerful API’s to make a new application for Android, iOS, Unity and Unreal Engine. You may find many sample applications in the ARCore website.

Common thing you do with augmented reality application is using camera as interface to change the scene you can see. You may easily think of it to use for games, but I am expecting more possibilities to AR to attract real world.

Augmented reality technology allows us to understand 3 dimensional world with a monocular camera, which has been 2 dimensional, add or change objects in the scene, or even augment real objects to fantasy design.

Please refer this post for 3 dimensional world understanding with AR.

You can find various ideas here as well.

Distance in AR world

One of the things you can do is to measure distance using AR. If you have an experience using AR application, you may have twirled around your smartphone above the ground. Its aim is for the camera to recognize the 3 dimensional space. This will allow us to measure distances of space with ARCore.

https://developers.google.com/ar/develop/java/images/hellosceneform-demo.mp4

There are multiple distances you can measure.

  1. distance from your camera
  2. distance between two points
  3. distances between multiple points

Now, I would like to share the distance measurements of these three.

Distance from your camera

To tell the truth, you can do the distance measurement from your camera with this post.

Assuming that you have configured your Android Studio to use ARCore, you can place an anchor, a fixed location in the real world, and find the distance from your camera to the anchor.

placing anchor

The position of the anchor in the real world can be acquired with worldPosition as x, y and z values (width, height and depth). The position of camera is arFragment.arSceneView.arFrame.camera.post , also in x, y and z values of world position. Once you know the positions of two, you are ready to calculate distance using Eucledean distance.

distance = √[(x1 - x2)² + (y1 - y2)² + (z1 - z2)²]

Distance between 2 points

There is not much chance that you want to measure a distance from your camera. Rather, you may like to find the distance between certain points in the space.

To find the distance of 2 points, first you have to place 2 anchors in the space.

If you have 0 anchor, place one. If you already have 1, place another one with distance pop in the middle. The distance between 2 points can be calculated just like the distance from camera to an anchor, only with taking anchors’ worldPosition .

You may miss tap the anchor and want to move to the better position. That can be done with TransformableNode with the translationController.isEnabled = True . The TransformableNode in ARCore is a node that contains ability to move, change its size and rotate. Enabling movement, called translation will allow you to move the anchor back and forth.

Distances of multiple points

Using methodology of measuring distance of 2 points, you can easily extend it to measure distances between multiple points.

One thing to consider is UI, how to express it in the smartphone display. One way is do the same thing as the 2 distances: place the distance pop between anchors. Another way is to make a table. I did the latter way for now.

Make a table of distances, and add the distance values once an anchor is added.

You can find the whole code in my repository.

--

--

Shibui Yusuke

technical engineer of cloud, container, Kubernetes, ML, and AR.