Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure distance using ARCore?

Is it possible to calculate distance between two HitResult `s ?

Or how we can calculate real distance (e.g. meters) using ARCore?

like image 729
Alexey Podolian Avatar asked Aug 31 '17 13:08

Alexey Podolian


People also ask

How accurate is ARCore?

Google says the Measure app is accurate to within half an inch. Having tested it a few times, I'd say that's a safe bet. However, ARCore isn't as good at remembering where objects were when you move the camera to an entirely different part of a room and then come back to your measurement tool.

Does Google have a measuring app?

Measure Up uses WebXR to help you calculate the length, area and volume of the things around you - all straight from the browser. Follow the link to open Measure Up in Chrome for Android and start measuring the world in augmented reality. Built by Google Creative Lab using the WebXR API.


1 Answers

In Java ARCore world units are meters (I just realized we might not document this... aaaand looks like nope. Oops, bug filed). By subtracting the translation component of two Poses you can get the distance between them. Your code would look something like this:

On first hit as hitResult:

startAnchor = session.addAnchor(hitResult.getHitPose());

On second hit as hitResult:

Pose startPose = startAnchor.getPose();
Pose endPose = hitResult.getHitPose();

// Clean up the anchor
session.removeAnchors(Collections.singleton(startAnchor));
startAnchor = null;

// Compute the difference vector between the two hit locations.
float dx = startPose.tx() - endPose.tx();
float dy = startPose.ty() - endPose.ty();
float dz = startPose.tz() - endPose.tz();

// Compute the straight-line distance.
float distanceMeters = (float) Math.sqrt(dx*dx + dy*dy + dz*dz);

Assuming that these hit results don't happen on the same frame, creating an Anchor is important because the virtual world can be reshaped every time you call Session.update(). By holding that location with an anchor instead of just a Pose, its Pose will update to track the physical feature across those reshapings.

like image 82
Ian M Avatar answered Sep 18 '22 13:09

Ian M