Is it possible to calculate distance between two HitResult
`s ?
Or how we can calculate real distance (e.g. meters) using ARCore?
Is it possible to calculate distance between two HitResult
`s ?
Or how we can calculate real distance (e.g. meters) using ARCore?
You can extract the two HitResult
poses using getHitPose() and then compare their translation component (getTranslation()).
The translation is defined as
...the position vector from the destination (usually world) coordinate frame to the local coordinate frame, expressed in destination (world) coordinates.
As for the physical unit of this I could not find any remark. With a calibrated camera this should be mathematically possible but I don't know if they actually provide an API for this
The answer is: Yes, you definitely can calculate distance between two HitResult
's. The working units of ARCore
, as well as ARKit
, are meters
. Sometimes, it's more useful to use centimetres
. Here are a few ways how you do it with Java and great old Pythagorean theorem
.
import com.google.ar.core.HitResult
MotionEvent tap = queuedSingleTaps.poll();
if (tap != null && camera.getTrackingState() == TrackingState.TRACKING) {
for (HitResult hit : frame.hitTest(tap)) {
// some logic...
}
}
// Here's the principle how you can calculate the distance
// between two anchors in 3D space using Java:
private double getDistanceMeters(Pose pose0, Pose pose1) {
float distanceX = pose0.tx() - pose1.tx();
float distanceY = pose0.ty() - pose1.ty();
float distanceZ = pose0.tz() - pose1.tz();
return Math.sqrt(distanceX * distanceX +
distanceY * distanceY +
distanceZ * distanceZ);
}
// Convert Meters into Centimetres
double distanceCm = ((int)(getDistanceMeters(pose0, pose1) * 1000))/10.0f;
// pose0 is the location of first Anchor
// pose1 is the location of second Anchor
Alternatively, you can use the following math:
Pose pose0 = firstAnchor.getPose() // first pose
Pose pose1 = secondAnchor.getPose() // second pose
double distanceM = Math.sqrt(Math.pow((pose0.tx() - pose1.tx()), 2) +
Math.pow((pose0.ty() - pose1.ty()), 2) +
Math.pow((pose0.tz() - pose1.tz()), 2));
double distanceCm = ((int)(distanceM * 1000))/10.0f;
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
Pose
s you can get the distance between them. Your code would look something like this:On first hit as
hitResult
:On second hit as
hitResult
: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 callSession.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.