黑。正在製作一款遊戲,並且只在三維空間中尋找到正方形或矩形的射線交叉點。有搜索網絡,發現了很多解決方案,但沒有我能理解的在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