我正在尝试播放声音,然后在两个不同类型的对象使用 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.overlapTest或 Scene.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/