【问题标题】:Remove all rotation from anchor in ArCore从 ArCore 中的锚点移除所有旋转
【发布时间】:2020-01-30 10:27:10
【问题描述】:

所以我正在构建一个应用程序,其中所有用户放置的对象都应该面向相同的方向。为此,我需要从放置对象的锚点上移除所有旋转。这是从锚点读取姿势并放置对象的代码:

 for (ColoredAnchor coloredAnchor : anchors) {
        if (coloredAnchor.anchor.getTrackingState() != TrackingState.TRACKING) {
                continue;
        }
        // Get the current pose of an Anchor in world space. The Anchor pose is updated
        // during calls to session.update() as ARCore refines its estimate of the world.
        coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0);


        // Update and draw the model and its shadow.
        virtualObject.updateModelMatrix(anchorMatrix, scaleFactor / 12f);
        virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, arrowColor);
}

如何从矩阵中移除旋转但保持锚点的本地位置?

我知道你可以使用

coloredAnchor.anchor.getPose().getRotationQuaternion();

提取旋转和

Matrix.rotateM();

旋转锚矩阵。但是rotateM 以度为单位的角度作为输入,而getRotationQuaternion() 输出不同的格式。也许 on 可以将四元数的输出转换为度数,然后逆向旋转矩阵?

总而言之,3D 模型在现实世界中应该始终面向同一个方向。

【问题讨论】:

    标签: java android matrix arcore


    【解决方案1】:

    我想我解决了。我正在将四元数的值转换为欧拉角 (Conversion)。之后,我在 ar 对象上使用计算的反向旋转。我需要做更多的测试,但到目前为止它工作正常。

     coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0);
    
    double w = coloredAnchor.anchor.getPose().qw();
    double x = coloredAnchor.anchor.getPose().qx();
    double y = coloredAnchor.anchor.getPose().qy();
    double z = coloredAnchor.anchor.getPose().qz();
    
    double xAngle = 0;
    double yAngle = 0;
    double zAngle = 0;
    
    double sinr_cosp = 2 * (w * x + y * z);
    double cosr_cosp = 1 - 2 * (x * x + y * y);
    xAngle = Math.atan2(sinr_cosp, cosr_cosp);
    
    double sinp = 2 * (w * y - z * x);
    if (Math.abs(sinp) >= 1) {
        yAngle = Math.copySign(Math.PI / 2, sinp); // use 90 degrees if out of range
    } else {
        yAngle = Math.asin(sinp);
    }
    
    double siny_cosp = 2 * (w * z - x * y);
    double cosy_cosp = 1 - 2 * (y * y + z * z);
    
    zAngle = Math.atan2(siny_cosp, cosy_cosp);
    
    xAngle = Math.toDegrees(xAngle);
    yAngle = Math.toDegrees(yAngle);
    zAngle = Math.toDegrees(zAngle);
    
    Matrix.rotateM(anchorMatrix, 0, (float) -xAngle, 1f, 0f, 0f);
    Matrix.rotateM(anchorMatrix, 0, (float) -yAngle + DYNAMIC_BUILDING_FIX, 0f, 1f, 0f);
    Matrix.rotateM(anchorMatrix, 0, (float) -zAngle, 0f, 0f, 1f);
    
    virtualObject.updateModelMatrix(anchorMatrix, scaleFactor / 12f);
    virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, arrowColor);
    

    虽然这是一个相当冗长和 hacky 的解决方案,但也许有人有更好的想法来解决它。

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2022-09-24
      • 2019-01-14
      相关资源
      最近更新 更多