【问题标题】:aframe physics - Collision between static bodiesaframe 物理 - 静态物体之间的碰撞
【发布时间】:2018-11-11 19:25:20
【问题描述】:

我正在使用aframe physics extra component 并试图检测两个静态物体之间的碰撞。

这是我的密码笔 - https://codepen.io/nirajupadhyay11/pen/gQraNq

但是,我不确定为什么它不起作用。

这里是代码 -

    <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
    <script src="https://unpkg.com/aframe-animation-component@^4.1.2/dist/aframe-animation-component.min.js"></script>
    <script src="https://cdn.rawgit.com/donmccurdy/aframe-physics-system/v3.1.2/dist/aframe-physics-system.min.js"></script>
    <script src="https://unpkg.com/aframe-physics-extras@0.1.2/dist/aframe-physics-extras.min.js"></script>

<a-scene physics="gravity: 0">

<a-entity id='ball' position="0 1 -3" material="color:green;" geometry="primitive:sphere; radius: 1;" static-body physics-collider="ignoreSleep: true"  animation="property: position; dur: 1500; easing: linear; to: 5 1 -3; dir:alternate; easing: easeInSine; loop:true">
</a-entity>

<a-plane color='grey' static-body rotation="-90 0 0" width="100" height="100"></a-plane>

<a-entity id='ball2' position="3 1 -3" material="color:red;" geometry="primitive:sphere; radius: 1;" static-body physics-collider="ignoreSleep: true"></a-entity>

</a-scene>

有人可以帮忙吗?

【问题讨论】:

    标签: physics aframe cannon.js


    【解决方案1】:

    它似乎正在工作。 physics-collider 提供了一个事件 collisions,当两个静态物体相互碰撞时会发出该事件。

    如果添加事件监听器

    // var obj = one of the static bodies
    obj.addEventListener("collisions", (e)=>{
      // collision!
    })
    

    查看here


    如果您预计其中一个球体飞走的实际碰撞,那么设置

    physics-collider="ignoreSleep: false"
    

    ignoreSleep 属性是负责唤醒沉睡体的标志 (docs)

    【讨论】:

    • 谢谢亚当。有没有办法知道碰撞的两个静态物体的 ID?我从事件侦听器中查看了 e 的值,但找不到。只有与physics-collider="ignoreSleep: true" 关联的静态体的id 在事件日志中可用(e)
    • 碰撞事件包含一个你正在碰撞的物体的数组。 event.detail.els[0].id 应该适用于第一个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多