Javascript 在three.js中检测球体和三角形之间的碰撞
我想在three.js中找出三角形是否与球体发生碰撞 我已经使用光线投射器和球体的顶点实现了一种方法,但它并不总是有效,因为三角形可能在球体的两个顶点之间,因此无法检测到Javascript 在three.js中检测球体和三角形之间的碰撞,javascript,three.js,collision-detection,Javascript,Three.js,Collision Detection,我想在three.js中找出三角形是否与球体发生碰撞 我已经使用光线投射器和球体的顶点实现了一种方法,但它并不总是有效,因为三角形可能在球体的两个顶点之间,因此无法检测到 我想要一个完美的数学算法。我可以建议你看下一页,而不是一个完美的数学算法,这一页完全涵盖了我所理解的你的问题:嘿,我找到了解决方案: 该算法分两个阶段工作: 第一阶段 我使用三角形中的3个点创建了3条线: var line1 = new THREE.Line3(a,b); var line2 = new THREE.Lin
我想要一个完美的数学算法。我可以建议你看下一页,而不是一个完美的数学算法,这一页完全涵盖了我所理解的你的问题:嘿,我找到了解决方案: 该算法分两个阶段工作: 第一阶段 我使用三角形中的3个点创建了3条线:
var line1 = new THREE.Line3(a,b);
var line2 = new THREE.Line3(a,c);
var line3 = new THREE.Line3(b,c);
then I find the closest point from each of these 3 lines, to the center of the sphere :
var closestPoint1 = line1.closestPointToPoint(center, true);
var closestPoint2 = line2.closestPointToPoint(center, true);
var closestPoint3 = line3.closestPointToPoint(center, true);
then I calculate the distance of that point to the center :
// code for 1 line only
var distToCenter = closestPoint.distanceTo(center);
if(distToCenter <= radius){
// we have a collision
}
var line1=新的三行3(a,b);
var line2=新的三个。Line3(a,c);
var line3=新的三个。line3(b,c);
然后我找到从这三条线中的每一条到球体中心最近的点:
var closestPoint1=line1.closestPointToPoint(中心,真);
var closestPoint2=line2.closestPointToPoint(中心,真);
var closestPoint3=line3.closestPointToPoint(中心,真);
然后我计算该点到中心的距离:
//代码仅适用于1行
var distanceTo中心=closestPoint.distanceTo(中心);
如果(远程中心
var triangle = new THREE.Triangle( a,b,c );
var plane = triangle.plane();
var pp, cp;
var dp = Math.abs(plane.distanceToPoint(center));
if(dp <= radius){
pp = plane.projectPoint(center);
cp = triangle.containsPoint(pp);
if(cp === true){
// collision
}
}