Javascript ThreeJS |检测一个对象何时离开另一个对象
我正在制作一个3JS项目,其中我有平面(Object3D)在球体(网格)内飞行 我试图检测平面和球体边界之间的碰撞,以便删除平面并使其在球体内的另一个位置重新出现 我的问题是,当一个对象离开另一个对象时,如何检测 我现在拥有的代码:Javascript ThreeJS |检测一个对象何时离开另一个对象,javascript,three.js,collision-detection,bounding-box,Javascript,Three.js,Collision Detection,Bounding Box,我正在制作一个3JS项目,其中我有平面(Object3D)在球体(网格)内飞行 我试图检测平面和球体边界之间的碰撞,以便删除平面并使其在球体内的另一个位置重新出现 我的问题是,当一个对象离开另一个对象时,如何检测 我现在拥有的代码: detectCollision(plane, sphere) { var boxPlane = new THREE.Box3().setFromObject(plane); boxPlane.applyMatrix4(plane.matrixWorld);
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")
}
})
在我的渲染函数中:
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
框3
不是您想要用来计算球体和平面碰撞的,因为框不考虑球体的曲率,也不会跟随平面的旋转
Three.js有一个更接近您需要的类。请记住,该类与使用SpheregeMetry
的网格不同,它更像是一个不会渲染到画布的数学助手。您可以根据需要使用其方法:
var sphereCalc = new THREE.Sphere( center, radius );
var point = new THREE.Vector3(10, 4, -6);
detectCollision() {
var collided = sphereCalc.containsPoint(point);
if (collided) {
console.log("Point is in sphere");
} else {
console.log("No collision");
}
return collided;
}
必须应用变换并检查循环中每个平面的所有4个点。请注意,有一种方法听起来似乎可以为您这样做,但它不同,因为它使用一个无限平面来计算交点,而不是一个定义了宽度和高度的平面,所以不要使用这种方法
编辑:
为了澄清,每个平面通常有4个顶点,因此您必须检查每个顶点,以查看球体是否包含4个点中的每一个
此外,该平面可能已移动和旋转,因此其原始顶点位置将应用变换矩阵。我认为您在示例中已经考虑到了这一点,但可能是这样的:
point.copy(vertex1);
point.applyMatrix4(plane.matrixWorld)
sphereCalc.containsPoint(point);
point.copy(vertex2);
point.applyMatrix4(plane.matrixWorld)
sphereCalc.containsPoint(point);
// ... and so on
首先感谢您的时间和良好的解释:)但有一点不清楚:“您必须应用变换并检查循环中每个平面的所有4个点”。你能更精确一点我应该用什么来做这个吗?(我是3J的初学者)当然,我对我的答案做了澄清。