在 three.js 中检测球体和三角形之间的碰撞
Detect collision between sphere and triangle in three.js
我想在 three.js
中找出三角形是否与球体发生碰撞
我已经使用光线投射器和球体的顶点实现了一种方法,但它并不总是有效,因为三角形可能是球体的 "between" 2 个顶点,因此无法检测到。
我想要一个完美的数学算法
我建议您查看以下页面,而不是完美的数学算法,据我所知,它完全涵盖了您的问题:Three.js - Accurate ray casting for collision detection
嘿,我找到了解决方案:
该算法在 2 个阶段工作 :
阶段 1
我使用三角形的 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
}
这就是三角形的线条。如果没有线与球体相交,我将在阶段 2 中检查三角形的“主体”:
阶段 2
我使用三角形的 3 点创建了一个 THREE.Triangle,然后我使用该三角形创建了一个平面,最后我找到了从该平面到球体中心的最近点。如果那个点“属于”三角形,我们就会发生碰撞:
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
}
}
如果在第一阶段和第二阶段都没有检测到碰撞,则三角形不与球体发生碰撞。
我的解决方案在我的项目中完美运行。
遇到问题请告知
我想在 three.js
中找出三角形是否与球体发生碰撞我已经使用光线投射器和球体的顶点实现了一种方法,但它并不总是有效,因为三角形可能是球体的 "between" 2 个顶点,因此无法检测到。
我想要一个完美的数学算法
我建议您查看以下页面,而不是完美的数学算法,据我所知,它完全涵盖了您的问题:Three.js - Accurate ray casting for collision detection
嘿,我找到了解决方案:
该算法在 2 个阶段工作 :
阶段 1
我使用三角形的 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
}
这就是三角形的线条。如果没有线与球体相交,我将在阶段 2 中检查三角形的“主体”:
阶段 2
我使用三角形的 3 点创建了一个 THREE.Triangle,然后我使用该三角形创建了一个平面,最后我找到了从该平面到球体中心的最近点。如果那个点“属于”三角形,我们就会发生碰撞:
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
}
}
如果在第一阶段和第二阶段都没有检测到碰撞,则三角形不与球体发生碰撞。
我的解决方案在我的项目中完美运行。
遇到问题请告知