嗨,您正在制作一款游戏,并且正在寻找在3D空间中仅针对正方形或矩形的光线相交。已经在网上搜索了许多解决方案,但是没有一种我能够理解的方法。我有一个2D的直线和线段相交脚本,但我无法弄清楚如何将其转换为3D。
重要的是它与正方形或矩形的哪一侧相交并不重要,但必须能够检索交点向量,以便稍后可以测试距离,以查看它是否发生在同一光线交点之前或之后。
非常感谢任何使用Python或其他类似脚本语言的示例。
编辑:不知道如何修改2D以显示示例,但已经创建了一个新的并发布了两个版本。
重要的是它与正方形或矩形的哪一侧相交并不重要,但必须能够检索交点向量,以便稍后可以测试距离,以查看它是否发生在同一光线交点之前或之后。
非常感谢任何使用Python或其他类似脚本语言的示例。
编辑:不知道如何修改2D以显示示例,但已经创建了一个新的并发布了两个版本。
//this is the exaple it test a ray onto a plane then look to se if that point is in the rectangle and saves it to test for distanse later
list Faces; //triangle faces
list Points; //
vector FindPoint(){
//calcute the point of intersection onto the plane and returns it
//if it can intersect
//else return ZERO_VECTOR
}
integer point-in-quadrilateral(){
//return 1 if the point is in the rectangular on the plane
//else return 0
}
default{
state_entry(){
integer n = (Faces != []); //return number of elements
integer x = 0;
while(x < n){
vector intersection = FindPoint( FromList(Faces, x) ); //take out a element and runs it trough the function
if(intersection != ZERO_VECTOR){
integer test = point-in-quadrilateral( FromList(Faces, x) ); //find out if the point is in rectangular
if(test == 1){ //if so
Points += intersection; //save the point
}
}
++x;
}
float first; //the distanse to the box intersection
integer l = (Points != []);
integer d;
while(d < l){
if(Dist( FromList(Points, d) ) < first) //if the new distanse is less then first
return 0; //then end script
++d;
}
}
}
//this is the 2D version
vector lineIntersection(vector one, vector two, vector three, vector four){
float bx = two.x - one.x;
float by = two.y - one.y;
float dx = four.x - three.x;
float dy = four.y - three.y;
float b_dot_d_perp = bx*dy - by*dx;
if(b_dot_d_perp == 0.0) {
return ZERO_VECTOR;
}
float cx = three.x-one.x;
float cy = three.y-one.y;
float t = (cx*dy - cy*dx) / b_dot_d_perp;
if(LineSeg){ //if true tests for line segment
if((t < 0.0) || (t > 1.0)){
return ZERO_VECTOR;
}
float u = (cx * by - cy * bx) / b_dot_d_perp;
if((u < 0.0) || (u > 1.0)) {
return ZERO_VECTOR;
}
}
return <one.x+t*bx, one.y+t*by, 0.0>;
}
P0
替代P
:http://imgur.com/a/Dt6ZvIV - Smartskaft2P0P.N = 0
跳跃到了a = ((P0 - R).N) / (D.N)
,能否详细说明一下? - Post169a < 0
,则没有交点。 - 김선달