【发布时间】:2020-05-21 14:51:28
【问题描述】:
我正在制作一个 ThreeJS 项目,其中有 planes (Object3D) 在 sphere (Mesh) 内飞行。
我正在尝试检测平面与球体边界之间的碰撞,以便删除该平面并使其重新出现在球体内部的另一个位置。
我的问题是如何检测一个对象何时离开另一个对象?
我现在的代码:
detectCollision(plane, sphere) {
var boxPlane = new THREE.Box3().setFromObject(plane);
boxPlane.applyMatrix4(plane.matrixWorld);
var boxSphere = new THREE.Box3().setFromObject(sphere);
boxSphere.applyMatrix4(sphere.matrixWorld);
return boxPlane.intersectsBox(boxSphere);
}
在我的渲染函数中:
var collision = this.detectCollision(plane, this.radar)
if (collision == true) {
console.log("the plane is inside the sphere")
}
else {
console.log("the plane is outside the sphere")
}
})
问题是当飞机在球体内时,我得到 true 和 false 基本上所有直到所有平面离开球体为止。那时我有一个 false 并且没有更多 true。
【问题讨论】:
标签: javascript three.js collision-detection bounding-box