2017-09-07 9 views
1

私はメッシュと子エンティティを持つラッパーエンティティを持っている:aframe-physics-systemを使って子エンティティを持つエンティティのフィジックスコンパウンドシェイプを構築する方法は?

<a-entity id="parent" dynamic-body="shape: none"> 
    <a-entity id="box" geometry="primitive: box" position="1 2 3"></a-entity> 
    <a-entity geometry="primitive: sphere" position="3 4 5"></a-entity> 
</a-entity> 

Iは3〜大砲とCANNONで図形を追加しています。たとえば、ボックスを使用します。

parentEl = document.querySelector('#parent'); 
childEl = parentEl.querySelector('#box'); 
parentEl.body.addShape(
    mesh2shape(childEl.getObject3D('mesh'), {type: mesh2shape.Type.BOX}), 
    new CANNON.Vec3().copy(childEl.object3D.position), 
    new CANNON.Quaternion().copy(childEl.object3D.quaternion)); 
子供が相殺されているので

が、ダイナミック・ボディ/形状は、上記のコードを見て右がそれを適用することなく、親に追加されていますか?私は現在、オブジェクトをつかんで投げるコードを持っていますが、オブジェクトは非常に不規則に動作します(速すぎて地面に奇妙に転がり、手で振る)

答えて

0

私は、子供の平均位置に親エンティティの位置):

// Move center of mass, currently just the average of the positions of the shapes. 
    centerOfMass = new THREE.Vector3(); 
    for (i = 0; i < shapeEls.length; i++) { 
    centerOfMass.add(shapeEls[i].object3D.position); 
    } 
    centerOfMass.divideScalar(shapeEls.length); 
    body.position.copy(centerOfMass); 
    this.el.setAttribute('position', body.position); 

そして形状のオフセットに重心を適用し、また、フレームには同期:

 for (i = 0; i < shapeEls.length; i++) { 
     shapeEl = shapeEls[i]; 
     shapeOffset = new CANNON.Vec3().copy(shapeEl.object3D.position).vsub(centerOfMass); 
     body.addShape(
     this.getShape(shapeEl.getObject3D('mesh'), shapeEl.getAttribute('data-shape')), 
      shapeOffset, 
      new CANNON.Quaternion().copy(shapeEl.object3D.quaternion)); 
     shapeEl.setAttribute('position', shapeOffset); 
    } 
関連する問題