在 ARCore 中放置具有给定罗盘方位的对象

Placing an object with a given compass bearing in ARCore

我想使用 sceneform 将一个朝北的箭头放置到 ARCore 世界中。我正在尝试了解从 phone 的罗盘到 sceneform 的四元数的正确转换系统。

这是我用来解决问题的代码:

    //Get the phone's pose in ARCore
    Pose deviceOrientedPose = frame.getCamera().getDisplayOrientedPose().compose(
            Pose.makeInterpolated(
                    Pose.IDENTITY,
                    Pose.makeRotation(0, 0, (float)Math.sqrt(0.5f), (float)Math.sqrt(0.5f)),
                    dhelper.getRotation()));
    float[] devquat = deviceOrientedPose.getRotationQuaternion();
    //Get the phone's heading in relation to the real world
    float heading = compassListener.getBearing(); //Use ROTATION_VECTOR sensor
    Quaternion deviceFrame = new Quaternion();
    deviceFrame.set(devquat[0],devquat[1],devquat[2],devquat[3]);
    double[] rpy = quat2rpy(deviceFrame);
    //Rotate around y axis... rotation in this case is the desired rotation
    float rotAngle = ((360-rotation)+heading+(float)Math.toDegrees(rpy[1])+360))%360;
    Quaternion qt = Quaternion.axisAngle(Vector3.up(),rotAngle);

本质上,这里的想法是获取设备姿态而不考虑方向。随后,我听取了 ROTATION_VECTOR 传感器给出的方位。鉴于 AR-Core 的坐标系是右手坐标系,AR-Core 世界中的旋转并不遵循我们将航向描述为顺时针向北的惯例。 (360-rotation)+heading 本质上是将锚点旋转到朝北(通过 +heading),然后应用所需航向的旋转 (360-heading)。并将其应用于相机的当前旋转 (float)Math.toDegrees(rpy[1])+360