2017-07-03 147 views
0

嗨我是AFrame的新手,並試圖做碰撞檢測b/w 2具有靜態身體組件附着他們的身體。aframe物理檢測碰撞b/w 2個靜態物體

我不會爲什麼事件相撞不會被解僱,因爲它的工作正常b/w 1個動態和1個靜態主體,但不能與2個靜態主體一起工作。

請提出一種方法來檢測碰撞b/w 2靜態物體使用aphysics或者如果有任何其他方式讓我知道。

我用移動的setInterval()靜體,改變使用setAttribute('position','x y z');

由於一噸提前的位置。

回答

1

a-frame physics documentation

靜態體:一個位置固定的或動畫對象。其他物體可能會與靜態物體碰撞,但靜態物體本身不受重力和碰撞的影響。

靜態物體不受碰撞的影響。


如果它們不是動態的,我會建議跟蹤它們在tick上的位置+音量並檢查它們是否發生碰撞(對於原始對象應該很容易)。


如果您有兩個球體(s1(x1,y1,z1)和s2(x2,y2,z2)),您可以在場景中添加一個主組件,使用之間的簡單距離2分formula

tick:function(){ 
    if(Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2) + Math.pow((z1-z2),2)))<(sRadius+s2Radius)){ 
    //do stuff when spheres collide 
    } 
} 

從我所看到的,three.js所擁有自己的相交方法,你可以找到他們here(方法:intersect()intersectsBox(),等...)。要訪問three.js對象,請獲取el.object3D引用。

交點對象的定義如下:

{ distance, point, face, faceIndex, indices, object } 
distance – distance between the origin of the ray and the intersection 
point – point of intersection, in world coordinates 
face – intersected face 
faceIndex – index of the intersected face 
indices – indices of vertices comprising the intersected face 
object – the intersected object 

three.js documentation說明。

對於其他基元的交集,您需要研究交集的算法,因爲我腦海中的算法效率非常低。

+0

你能告訴我如何構建該組件,也許一個僞代碼將有助於謝謝..... – user287332

+0

@ user287332已更新,是好的,還是您需要更多信息? –

+2

謝謝你的回答讓我朝着正確的方向....我在組件的tick中使用它:function(){firstBB = new THREE.Box3()。setFromObject(matchingElements [i] .object3D); \t \t \t \t secondBB = new THREE.Box3()。setFromObject(this.el.object3D); \t \t \t \t var collision = firstBB。intersectsBox(secondBB); \t \t \t \t \t \t \t \t如果(碰撞){ \t \t \t \t \t this.el.emit( '碰撞',{值:2,EL:this.el}); – user287332