我正在尝试播放声音,然后在使用 Sceneform 碰撞时破坏两种不同类型的两个对象。我看到 Sceneform 有一个碰撞 API(https://developers.google.com/ar/reference/java/com/google/ar/sceneform/collision/package-summary),但我不知道该怎么做在碰撞中。我尝试过扩展碰撞形状,覆盖 shapeIntersection 方法,并为每个节点设置碰撞形状属性,但这似乎没有任何作用。似乎没有任何示例代码,但文档提到了碰撞侦听器。到目前为止,我一直在进行蛮力检查,但我希望有一种更有效的方法。
编辑:我一直在尝试做这样的事情:
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 从相机“扔”到平面上的一个点。