问题描述
是否可以计算两个 HitResult
之间的距离?
Is it possible to calculate distance between two HitResult
`s ?
或者我们如何使用 ARCore 计算实际距离(例如米)?
Or how we can calculate real distance (e.g. meters) using ARCore?
推荐答案
在 Java ARCore 中,世界单位是米(我刚刚意识到我们可能不会记录这个... aaa 看起来不像.糟糕,提交错误).通过减去两个 Pose
的平移分量,您可以得到它们之间的距离.您的代码如下所示:
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:
第一次点击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()
时都可以重塑虚拟世界.代码>.通过使用锚点而不是仅姿势保持该位置,其姿势将更新以跟踪这些重塑中的物理特征.
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.
这篇关于如何使用 ARCore 测量距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!