android - 场景碰撞

标签 android arcore sceneform

我正在尝试播放声音,然后在两个不同类型的对象使用 Sceneform 发生碰撞时销毁它们。我看到 Sceneform 有一个碰撞 api ( https://developers.google.com/ar/reference/java/com/google/ar/sceneform/collision/package-summary ),但我不知道如何处理碰撞。我试过扩展碰撞形状,覆盖 shapeIntersection 方法,并为每个节点设置 Collision Shape 属性,但这似乎没有任何作用。似乎没有任何示例代码,但文档中提到了碰撞监听器。到目前为止,我一直在用蛮力方式进行检查,但我希望有一种更有效的方式。

编辑:我一直在尝试做这样的事情:

public class PassiveNode extends Node{

public PassiveNode() {
    PassiveCollider passiveCollider = new PassiveCollider(this);
    passiveCollider.setSize(new Vector3(1, 1, 1));
    this.setCollisionShape(passiveCollider);
}

public class PassiveCollider extends Box {
    public Node node; // Remeber Node this is attached to
    public PassiveCollider(Node node) {
        this.node = node;
    }
}

public class ActiveNode extends Node {

private Node node;
private Node target;
private static final float metersPerSecond = 1F;

public ActiveNode(Node target) {
    node = this;
    this.target = target;
    BallCollision ball = new BallCollision();
    ball.setSize(new Vector3(1, 1, 1));
    this.setCollisionShape(ball);
}

@Override
public void onUpdate(FrameTime frameTime) {
    super.onUpdate(frameTime);
    Vector3 currPos = this.getWorldPosition();
    Vector3 targetPos = target.getWorldPosition();
    Vector3 direction = Vector3.subtract(targetPos, currPos).normalized();
    this.setWorldPosition(Vector3.add(currPos, direction.scaled(metersPerSecond * frameTime.getDeltaSeconds())));
}

private class BallCollision extends Box {

    @Override
    protected boolean boxIntersection(Box box) {
        if (box instanceof PassiveNode.PassiveCollider) {
            //Play Sound
            node.setEnabled(false);
            ((PassiveNode.PassiveCollider) box).node.setEnabled(false);
            return true;
        }
        return false;
    }
}

其中 PassiveNode 位于平面上,ActiveNode 从相机“扔”到平面上的一个点。

最佳答案

您尝试覆盖的相交方法会进行数学计算以计算两个碰撞形状是否相交,我建议不要覆盖它们。

目前,没有监听器或方法可以覆盖以检测节点何时重叠。但是,您可以调用一些函数来测试重叠节点。

您可以使用 Scene.overlapTestScene.overlapTestAll ,它使用节点中的 CollisionShape。

默认情况下,它会根据附加到节点的可渲染对象的尺寸自动使用碰撞形状。您可以使用 Node.setCollisionShape覆盖节点的碰撞形状,或在没有 Renderable 的节点上设置碰撞。

你可以通过做这样的事情来达到你正在寻找的效果:

private void onUpdate(FrameTime frameTime) {
  ArrayList<Node> overlappedNodes = arSceneView.getScene().overlapTestAll(ballNode);
  for (Node node : overlappedNodes) {
    if (node instanceof PassiveNode) {
      // May want to use a flag to check that the node wasn't overlapping the previous frame.
      // Play sound if overlapping started.
    }
  }
}

关于android - 场景碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572532/

相关文章:

Android jetpack组件-抽屉导航上的toast点击

android - 轮询 ACTION_BATTERY_LOW

java - 为什么我的 fragment 不起作用

java - 如何创建纹理以覆盖在增强面部网格上?

kotlin - 如何调整 ViewRenderable 以匹配 AugmentedImage 大小?

android - 在Android Studio 4.0上,Gradle同步失败

android - 有没有办法让 Vuforia 和 ARCore 在同一个应用程序中?

augmented-reality - ARCore:显示在面部上方的模型

java - 如何重置或重启 ARCore session ?

java - 在 Sceneform 中拍照时隐藏 PlaneRenderer