黑。正在制作一款游戏,并且只在三维空间中寻找到正方形或矩形的射线交叉点。有搜索网络,发现了很多解决方案,但没有我能理解的在2D中有一个线条和线段交集脚本,但我无法弄清楚必须使它3D。 它与正方形或矩形相交的哪一侧并不重要,但它必须能够检测交点矢量的点,以便稍后可以测试距离,如果它发生在相同射线交点上的其他交点之前或之后。3D中的光线和正方形/矩形交点
在Python或其他类似的脚本语言的所有示例将不胜感激
编辑:不知道有修改2D显示的的exaple但提出了新的发布都。
//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>;
}
感谢您的回复。你能解释一下它的一部分吗? “x0 y0 z0”是否等于x,y和z使得第二个方程等于零?如果有的话如何计算每个人的价值? – TeaWave 2012-01-11 18:02:36
本质上,是的。x0,y0和z0是与您的矩形位于同一平面上的SOME点(可以是任意点)的值。至于该方程中的x,y和z,可以插入上面列出的线方程(r_ + v_ * t)的值。你能更具体地说明你打算编写代码的方式吗?也许你说你能解决的二维版本的例子?我可能能够更好地解释这种方式。 – 2012-01-13 02:27:10
编辑了一个实例和2D版本的主要帖子。如果a,b和c是飞机的单位矢量,是否已经做了更多的创建和缠绕?并且是x0,y0和z0的线性方程?已经运行了多种组合,但可以将其设置为等于零。 – TeaWave 2012-01-14 12:58:21