AR 虚拟对象(箭头)未沿着源线和目标线移动

发布于 2025-01-10 15:59:24 字数 2608 浏览 0 评论 0原文

我是 ARCore android 新手。我正在使用 ARCore android 实现室内导航。我通过以下代码计算了从正北连接源和目的地(经纬度)的线的角度:

private void getBearing(LatLng sourceLatLng, LatLng destLatLng) {
    Double sourceLat = sourceLatLng.latitude / 180 * Math.PI;
    Double sourceLong = sourceLatLng.longitude / 180 * Math.PI;
    Double destLat = destLatLng.latitude / 180 * Math.PI;
    Double destLong = destLatLng.longitude / 180 * Math.PI;

    double y = sin(destLong - sourceLong) * cos(destLat);
    double x = cos(sourceLat) * sin(destLat) - sin(sourceLat) * cos(destLat) * cos(destLong - sourceLong);

    double tan2 = atan2(y, x);                
    headingAngle = tan2 * 180 / Math.PI;     
    if (bearingAngle < 0) {
        bearingAngle = bearingAngle + 360;
    }
}

然后使用加速度计、磁力计从正北(方位角)计算设备方向的角度。我想沿着连接源和目的地的线(纬度、经度)对齐用户设备。然后计算目的地方位角如下:

val currentDestinationAzimuth = (headingAngle - currentAzimuth + 360) % 360

并按 currentDestinationAzimuth 角度旋转箭头,使其与源和目的地对齐。但箭头并没有沿着源头和目的地前进。它正在偏离其预期位置(沿着源和目的地)。我在谷歌上搜索了很多,并对代码进行了更改,但此时仍然感到震惊。以下是 3D 模型渲染的代码。

if (this.headingAngle >= this.mAzimuth && this.headingAngle <= this.mAzimuth + err ) {
        Session session = arFragment.getArSceneView().getSession();
        Vector3 cameraPos = arFragment.getArSceneView().getScene().getCamera().getWorldPosition();
        cameraForward = arFragment.getArSceneView().getScene().getCamera().getForward(); 
        Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(1.0f));
        Pose pose = Pose.makeTranslation(position.x, -1.0f, position.z);
        Anchor anchor = session.createAnchor(pose);
        anchorNode = new AnchorNode(anchor);                                 
        anchorNode.setParent(arFragment.getArSceneView().getScene());
        Node node = new Node();
        node.setLocalScale(new Vector3(0.40f, 0.40f, 0.40f));
        node.setRenderable(modelRenderable);
        node.setParent(anchorNode);
        arFragment.getArSceneView().getScene().addChild(anchorNode);
          double currentDestinationAzimuth = 0;
            Quaternion q1 = node.getLocalRotation();
            System.out.println("diff world rot : " + node.getWorldRotation());
            float angleOfRotation = (float) (headingAngle - initialFacing)
            currentDestinationAzimuth = (initialHeading - bearingOfLatLong + 360) % 360;
            Quaternion q2 = Quaternion.axisAngle(new Vector3(0f, 1f, 0f), (float) currentDestinationAzimuth);
            node.setWorldRotation(Quaternion.multiply(q1, q2));

}

我在某个地方做错了什么吗?请指导。

感谢您的任何帮助...

I am new to ARCore android. I was implementing indoor navigation using ARCore android. I calculated angle of line joining source and destination (Lat Long) from True north from following code :

private void getBearing(LatLng sourceLatLng, LatLng destLatLng) {
    Double sourceLat = sourceLatLng.latitude / 180 * Math.PI;
    Double sourceLong = sourceLatLng.longitude / 180 * Math.PI;
    Double destLat = destLatLng.latitude / 180 * Math.PI;
    Double destLong = destLatLng.longitude / 180 * Math.PI;

    double y = sin(destLong - sourceLong) * cos(destLat);
    double x = cos(sourceLat) * sin(destLat) - sin(sourceLat) * cos(destLat) * cos(destLong - sourceLong);

    double tan2 = atan2(y, x);                
    headingAngle = tan2 * 180 / Math.PI;     
    if (bearingAngle < 0) {
        bearingAngle = bearingAngle + 360;
    }
}

Then calculated angle of device orientation from True North (Azimuth Angle) using accelerometer, magnetometer. I want to align user device along the line joining source and destination (Lat,Long). And then calculated destination azimuth as following :

val currentDestinationAzimuth = (headingAngle - currentAzimuth + 360) % 360

And rotated arrow by currentDestinationAzimuth angle to make it align with source and destination. But arrow is not getting along the source and destination. It is getting diverted from its expected placement (along source and destination). I googled a lot and made changes in codes but still struck at this point. Below is code for 3d Model Rendering.

if (this.headingAngle >= this.mAzimuth && this.headingAngle <= this.mAzimuth + err ) {
        Session session = arFragment.getArSceneView().getSession();
        Vector3 cameraPos = arFragment.getArSceneView().getScene().getCamera().getWorldPosition();
        cameraForward = arFragment.getArSceneView().getScene().getCamera().getForward(); 
        Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(1.0f));
        Pose pose = Pose.makeTranslation(position.x, -1.0f, position.z);
        Anchor anchor = session.createAnchor(pose);
        anchorNode = new AnchorNode(anchor);                                 
        anchorNode.setParent(arFragment.getArSceneView().getScene());
        Node node = new Node();
        node.setLocalScale(new Vector3(0.40f, 0.40f, 0.40f));
        node.setRenderable(modelRenderable);
        node.setParent(anchorNode);
        arFragment.getArSceneView().getScene().addChild(anchorNode);
          double currentDestinationAzimuth = 0;
            Quaternion q1 = node.getLocalRotation();
            System.out.println("diff world rot : " + node.getWorldRotation());
            float angleOfRotation = (float) (headingAngle - initialFacing)
            currentDestinationAzimuth = (initialHeading - bearingOfLatLong + 360) % 360;
            Quaternion q2 = Quaternion.axisAngle(new Vector3(0f, 1f, 0f), (float) currentDestinationAzimuth);
            node.setWorldRotation(Quaternion.multiply(q1, q2));

}

Am i doing something wrong somewhere? Please guide.

Thanks for any help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文