代码之家  ›  专栏  ›  技术社区  ›  Niraj

同一个物理球在碰撞时分裂成更小的球

  •  0
  • Niraj  · 技术社区  · 6 年前

    在我的游戏中,我制作了一个球体(使用A球体创建),一个动态物体,与另一个动态物体(A盒)碰撞。

    碰撞后,球体分裂成多个较小的球体。我需要阻止这种分裂

    这是密码笔- sphere breaks on collision with dynamic body

    这是随附的代码-

    HTML

    <a-scene physics="debug: true; gravity: -5.0">
      <a-entity camera="userHeight: 1.6"
                    look-controls
                    kinematic-body>
            <a-entity cursor
                      position="0 0 -1"
                      geometry="primitive: circle; radius: 0.01; segments: 4;"
                      material="color: #FF4444; shader: flat"></a-entity>
        <a-entity position="0 0 1" id="attachment"></a-entity>
      </a-entity>
    
      <a-entity geometry="primitive: box; height:2" material="color: black; shader: flat" position="0 2 -5" dynamic-body></a-entity>
    
      <a-plane static-body color="#ccc" height="100" width="100" position="0 -0.1 0" rotation="-90 0 0"></a-plane>
    </a-scene>
    

    和JS

    const scene = document.querySelector('a-scene');
    const camera = document.querySelector('[camera]');
    const attachment = document.querySelector('#attachment');
    
    function spawnBullet() {
        let entity = document.createElement('a-sphere');
        let impulseAmount = 8;
    
        entity.setAttribute('radius', 1);
        // Set initial position of projectile to that of the camera.
        entity.setAttribute('position', camera.getAttribute('position'));
        entity.setAttribute('color', '#00FFCC');
        entity.setAttribute('shader', 'flat');
        entity.setAttribute('mass', 10);
    
        // Append projectile to the scene, not to the camera, to
        // avoid all sorts of complications. Most notably, CANNON.js
        // has no scene graph or nesting.
        scene.appendChild(entity);
    
        entity.setAttribute('dynamic-body', true);  
    
        entity.addEventListener('body-loaded', function(){
          // Can't apply forces during the same tick that attaches the body, because
          // it hasn't been fully synced to the physics sim. (bug)
          setTimeout(function () {
            let pStart = new CANNON.Vec3();
            // Use an origin point behind the head, not at the head, so
            // there's a useful vector between the origin and the projectile.
            pStart.copy(attachment.object3D.getWorldPosition());
            let force = entity.body.position.vsub(pStart);
            force.normalize(); 
            force.scale(impulseAmount, force);
            entity.body.applyImpulse(force, entity.body.position);
          }, 0);
    
          entity.addEventListener('collide', function(e){
            console.log("hit");
          })
        });
    }
    
    if (scene.hasLoaded) init(); // change 2
    else scene.addEventListener('loaded', init);
    
    function init () {
      // any code that appends things to the scene
      scene.addEventListener('click', spawnBullet);
    }
    

    有没有一种方法可以阻止这种情况,使球体在碰撞后保持完整?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Piotr Adam Milewski    6 年前

    倾听 click 现场事件适得其反,因为你有三个事件
    -当单击某个对象时,光标会发出一个
    - target 单击时发出一个
    -画布会发出一个(鼠标单击,而不是A帧光标-在dom canvas元素上)。

    你可以在里面看到 this 例如,单击场景中的任何对象,然后签出控制台。


    你可以听 mousedown 每次单击光标时由光标发出的事件。过来看 here
    如果你在一台个人电脑上,你也可以听到点击aframe画布。如果你在 vive 然后打电话给 shootBullet 当扳机被扳动时。

    检查它是否正常工作 here .

        2
  •  2
  •   Don McCurdy    6 年前

    球体没有破裂,发生的是 click 每次单击都会多次调用事件处理程序,并一次创建多个球体。我不太清楚为什么会这样。一种方法,使用 AFRAME.utils.throttle 要防止创建球体太快:

    spawnBullet = AFRAME.utils.throttle(spawnBullet, 100);
    scene.addEventListener('click', spawnBullet);
    

    更新到较新版本的A-Frame也可以解决此问题。

    推荐文章