【问题标题】:Collisions not working using Aframe 1.2.0 & Ammo.js碰撞无法使用 Aframe 1.2.0 和 Ammo.js
【发布时间】:2021-11-17 15:04:38
【问题描述】:

我开始使用 Aframe 1.2.0 和 Ammo.js 制作游戏,因为 CANNON.js 支持将来可能会被弃用。

我有一个非常简单的起点 - 一个静态框 - 一个动态球体 - 单击球体以发射 - 它应该从盒子反弹,但它只是穿过它。我尝试将拍摄功能放入组件中,但发生了同样的事情。

此处出现故障 -> https://descriptive-truthful-sneezeweed.glitch.me

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta id="theme-color" name="theme-color" content="#ffffff">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

<script src="https://mixedreality.mozilla.org/ammo.js/builds/ammo.wasm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/aframe-physics-system@4.0.1/dist/aframe-physics-system.min.js"></script>
</head>

<body>
    <a-scene id="mainScene" vr-mode-ui="enabled: false" physics=" driver: ammo; debug: true; debugDrawMode: 1;"
    device-orientation-permission-ui="enabled: false"
    renderer='antialias: true; colorManagement: true; sortObjects: false; precision: high; logarithmicDepthBuffer: false; physicallyCorrectLights: false;'
    raycaster="objects: .clickable"
    cursor="fuse: false; rayOrigin: mouse">

        <a-box id="backboard" position="0 2 -5" width="1" height="1" depth="0.2" ammo-body="type: static; emitCollisionEvents: true;" ammo-shape="type: box" material="color: tomato;"></a-box>
        <a-sphere id="ball" position="0 0 -2" radius="0.15" ammo-shape="type: sphere; fit: manual; sphereRadius: 0.15;" material="color: cyan;"></a-sphere>

    </a-scene>

<script>

$("#ball").addClass("clickable");
ball.addEventListener("click", shoot);
backboard.addEventListener("collidestart", function() {
    console.log("HIT");
});

function shoot() {
    ball.setAttribute("ammo-body","type: dynamic;");
    const force = new Ammo.btVector3(0, 7, -3);
    const pos = new Ammo.btVector3(ball.object3D.position.x, ball.object3D.position.y, ball.object3D.position.z);
    ball.body.applyImpulse(force, pos);
    Ammo.destroy(force);
    Ammo.destroy(pos);
}

</script>
</body>
</html>

【问题讨论】:

    标签: game-physics aframe ammo.js


    【解决方案1】:

    似乎错误是由只有ammo-shape 组件的球体引起的,没有主体。如果两者都在点击时附加,那么它按预期工作:

    <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
    <script src="https://mixedreality.mozilla.org/ammo.js/builds/ammo.wasm.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/aframe-physics-system@4.0.1/dist/aframe-physics-system.min.js"></script>
    <script>
      AFRAME.registerComponent("foo", {
        init: function() {
          // when clicked attach the body and the shape, and apply the impulse
          this.el.addEventListener("click", evt => {
            this.el.setAttribute("ammo-body", {
              type: "dynamic"
            });
            this.el.setAttribute("ammo-shape", {
              type: "sphere",
              fit: "manual",
              sphereRadius: 0.15
            });
            const force = new Ammo.btVector3(0, 7, -3);
            const pos = new Ammo.btVector3(this.el.object3D.position.x, ball.object3D.position.y, ball.object3D.position.z);
            ball.body.applyImpulse(force, pos);
            Ammo.destroy(force);
            Ammo.destroy(pos);
          })
          // check if the events are working by changing a the boxes color
          document.querySelector("#backboard").addEventListener("collidestart", evt => {
            document.querySelector("#backboard").setAttribute("color", "green");
          })
        }
      })
    // the rest is like in the scene from the question
    </script>
    <a-scene id="mainScene" vr-mode-ui="enabled: false" 
              physics=" driver: ammo; debug: true; debugDrawMode: 1;" 
              device-orientation-permission-ui="enabled: false" 
              renderer='antialias: true; colorManagement: true; sortObjects: false; precision: high; logarithmicDepthBuffer: false; physicallyCorrectLights: false;'
              raycaster="objects: .clickable" 
              cursor="rayOrigin: mouse">
    
      <a-box    id="backboard" position="0 2 -5" width="1" height="1" depth="0.2" 
                ammo-body="type: static; emitCollisionEvents: true;" ammo-shape="type: box"  
                material="color: tomato;"></a-box>
      <a-sphere id="ball" class="clickable" position="0 0 -2" radius="0.15" 
                material="color: cyan;" foo></a-sphere>
    </a-scene>

    【讨论】:

    • 一如既往的 Piotr 救援 - 谢谢人
    猜你喜欢
    • 2020-04-27
    • 2016-06-01
    • 2022-06-14
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多