有可能计算两个HitResult
之间的距离吗?
或者我们如何使用ARCore计算实际距离(例如米)?
在Java ARCore世界单位是米(我刚刚意识到我们可能没有记录这个... aaaand看起来像不。哎呀,bug提交)。通过减去两个Pose
s的平移分量,您可以得到它们之间的距离。您的代码看起来像这样:
首次命中为hitResult
:
startAnchor = session.addAnchor(hitResult.getHitPose());
第二次击中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);
假设这些命中结果不会发生在同一帧上,创建Anchor
很重要,因为每次调用Session.update()
时都可以重塑虚拟世界。通过使用锚点而不仅仅是姿势来保持该位置,其姿势将更新以跟踪这些重建中的物理特征。
你可以使用HitResult
提取两个getHitPose()姿势,然后比较它们的翻译成分(getTranslation())。翻译定义为
...从目的地(通常是世界)坐标系到本地坐标系的位置矢量,以目的地(世界)坐标表示。
至于这个物理单位,我找不到任何评论。使用经过校准的相机,这在数学上是可行的,但我不知道它们是否真的为此提供了API
答案是:是的,当然,你绝对可以计算两个HitResult
之间的距离。 ARCore
以及ARKit
框架的网格大小是meters
。有时,使用centimetres
更有用。以下是使用Java和古老的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)) {
// Blah-blah-blah...
}
}
// 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
或者,您也可以使用以下数学:
Pose pose0 = // first HitResult's Anchor
Pose pose1 = // second HitResult's Anchor
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;