我得到了一个连接到vr控制器实体的光线投射器组件:
<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!');
});
}
});
提前谢谢。
---------更新----------------
@彼得·亚当·米莱夫斯基的回答是正确的。要侦听的事件是光线投射器相交,而不是射线投射器相交的。通过这种方式,您可以循环一个相交实体的数组。
是否可以从光线投射器相交得到相同的结果??如果该事件是在相交实体上发出的,那么应该可以获取其id和其他属性。我不认为每次发生交集事件时都在数组上循环是理想的。
来自文档:
raycaster-intersected
在相交实体上发出。它包含有关光线投射实体和交叉点详细信息的信息raycaster-intersection
在光线投射实体上发射,并包含相交实体的列表
使用raycaster-intersection
时,尝试访问相交实体数组的evt.detail.els
。示例
由于raycaster-intersected
是在相交实体上发射的,因此可以检测光线投射器是否接触到目标。
target.addEventListener('raycaster-intersected', (e)=> {
// intersected, e.target contains the element
// e.detail.getIntersection(e.target) contains info about the intersection
})
在这里打闹。在这里摆弄getIntersection()
。