【问题标题】:AFRAME raytrace get reference of intersected entityAFRAME 光线追踪获取相交实体的参考
【发布时间】:2018-11-22 06:46:58
【问题描述】:

我在 VR 控制器实体上附加了一个 raycaster 组件:

 <a-entity id="righthand" 
          vive-controls="hand: right; " 
          oculus-touch-controls="hand: right;"
          controls-ui
          collider-check
       >
        <a-entity raycaster="objects: .collidable; showLine: true; far: 100; " line="color: blue; opacity: 0.5" ></a-entity>

</a-entity>

我在场景中得到了一个实体,它将接收光线跟踪事件:

<a-entity id='myCube' class="collidable" position="0 1.25 -6"  obj-model="obj: #cube-obj; mtl: #cube-mtl" >
       </a-entity>

如何在“raycaster-intersected”事件中获取碰撞实体的 ID 或任何引用? 我尝试了以下代码,但似乎没有任何内容包含此数据:

AFRAME.registerComponent('collider-check', {
  dependencies: ['raycaster'],

  init: function () {
    this.el.addEventListener('raycaster-intersected', function (evt) {

      console.log(evt.detail.el);  // not here

      console.log(evt.detail.intersection); // not here

      console.log(evt.detail);// not here

      console.log('Player hit something!');
    });
  }
});

提前致谢。

---------更新-----------

@Piotr Adam Milewski 的回答是正确的。要监听的事件是 raycaster-intersection 而不是 raycaster-intersected。通过这种方式,您可以循环一个相交实体的数组。

是否有可能从 raycaster-intersected 获得相同的结果??如果该事件是在相交实体上发出的,那么应该可以获得它的 id 和其他属性。我认为每次发生相交事件时循环遍历一个数组并不理想。

【问题讨论】:

  • 我已经添加了使用目标事件的解决方案
  • 是的,现在可以了。也许 Aframe 文档应该提到这一切。
  • 你的碰撞检查应该附加到光线投射器上。上面的代码现在有两个 raycasters。

标签: javascript aframe


【解决方案1】:

来自docs:

  • raycaster-intersected 在相交实体上发出。它包含有关光线投射实体和交叉点详细信息的信息。
  • raycaster-intersection 在光线投射实体上发出,并包含相交实体的列表。

使用raycaster-intersection 时,请尝试访问evt.detail.els 以获取相交实体数组。示例here


由于raycaster-intersected 是在相交实体上发射的,因此您可以检测光线投射器是否触及您的目标。

target.addEventListener('raycaster-intersected', (e)=> { 
  // intersected, e.target contains the element
  // e.detail.getIntersection(e.target) contains info about the intersection
})

小提琴here。摆弄getIntersection()here。

【讨论】:

  • 我收到此错误:e.detail.getIntersection 不是函数。我试过 evt.detail.el.components.raycaster.getIntersection(this.el) 但为空。如果有帮助,我正在使用aframe.io/releases/0.8.2/aframe.min.js。
  • ngoKevin 提到它here,它似乎是在 master 分支上实现的,但我也从中得到了空值,所以我更新了 anwser (+fiddle)。
  • 代码不应该是raycaster-intersected吗? getIntersection需要传入相交实体,而不是raycaster实体。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2012-06-19
  • 1970-01-01
相关资源
最近更新 更多