android - 在 ARCore 中始终旋转的垂直平面上添加的对象

标签 android kotlin arcore sceneform

enter image description here

我在 Sceneform ARFragment 的垂直平面上添加图像。但它总是被旋转。代码在水平面上运行良好。我在垂直平面上放置图像的代码如下:

arFragment.setOnTapArPlaneListener { hitResult: HitResult, 
                                         plane: Plane, 
                                   motionEvent: MotionEvent ->

    if(!isOnceTapedOnSurface) {
        val anchor = hitResult.createAnchor()
        val anchorNode = AnchorNode(anchor)
        anchorNode.setParent(arFragment.arSceneView.scene)

        andy = TransformableNode(arFragment.transformationSystem)

        if(plane.type == Plane.Type.VERTICAL) {
            val anchorUp = anchorNode.up
            andy.setLookDirection(Vector3.up(), anchorUp)
        }

        andy.setParent(anchorNode)
        andy.renderable = andyRenderable
        andy.select()

        // arFragment.arSceneView.planeRenderer.isVisible = false
        isOnceTapedOnSurface = true
    }
}

最佳答案

要解决此问题,您可以使用上述解决方案。但是您应该使用世界旋转来旋转对象。不要使用局部旋转。我们需要将旋转值归零。如果您使用局部旋转,则对象将表现为 anchor (父)旋转。所以通过使用世界旋转我们可以控制物体。

String planeType = "";

//点击表面时可以获得 anchor 方向

if (plane.getType() == Plane.Type.VERTICAL){
                planeType = "Vertical";
            }else if (plane.getType() == Plane.Type.HORIZONTAL_UPWARD_FACING){
                planeType = "Horizontal_Upward";
            }else if (plane.getType() == Plane.Type.HORIZONTAL_DOWNWARD_FACING){
                planeType = "Horizontal_Downward";
            }else {
                planeType = "Horizontal";
            }```


// First set object world rotation zero

transformableNode.setWorldRotation(Quaternion.axisAngle(new Vector3(0, 0f, 0), 0));


// check plane type is vertical or horizontal if it is vertical below logic will work.

if (planeType.equals("Vertical")) {

   Vector3 anchorUp = anchorNode.getUp();
   transformableNode.setLookDirection(Vector3.up(), anchorUp);

}

关于android - 在 ARCore 中始终旋转的垂直平面上添加的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106499/

相关文章:

android - import android.content.pm.packageParser 无法解析

android - 将 CGAffineTransform 从 iOS 移植到 Android

java - 将 ListView 中的复选框状态保存到 ArrayList 中

Android NumberPicker 在加载 View 时渲染滚动条

android - 错误 : type inference failed

android - ajax 调用可以在 Android 中完成吗?

java - 无法将 Java 代码转换为 Kotlin 以实现 OnChangeListener

android - 如何改变平面中节点的位置?

android - 将Unity项目导入 native 应用程序时出现问题

augmented-reality - 有没有支持ARCore的平板设备