我有一个包含子网格实体的包装实体:
<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>
我正在添加三到大炮和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 :(得分: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);
然后将质心应用于形状的偏移,并同步到A-Frame:
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);
}