Hei。ゲームを作っており、3D空間でのみ正方形や長方形に光線の交差点を探しています。ウェブを検索して多くの解決策を見つけましたが、私が理解できるものは何も2Dとラインセグメントの交差点スクリプトを持っていませんが、私はそれを3Dにしなければなりません。 どちらの側からも正方形または長方形と交差することは重要ではありませんが、交差点ベクトルを再度取得できる必要があるため、後で同じ光線交差点の他の交差点の前後に発生した場合は距離をテストできます。いけないexapleを表示する2Dを変更する必要があります知っているが、新しいとの両方を掲示作っ:Pythonや他の類似のスクリプト言語で3Dのレイと正方形/長方形の交差
どれ例が大幅に
編集
を理解されるであろう。//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>;
}
レスポンスありがとうございます。あなたはそれのいくつかの部分をexplaneできますか? "x0 y0 z0"イコライザはx、y、zの等価になり、2番目の方程式はゼロになりますか?もしそうなら、それぞれの値をどのように計算するのですか? – TeaWave
本質的に、はい。x0、y0、z0は、あなたの四角形と同じ平面にあるSOMEポイント(任意のポイントでもよい)の値です。その式の中のx、y、zについては、上に列挙した線方程式(r_ + v_ * t)の値を差し込みます。あなたはコードを書く上でどのように計画を立てることができますか?おそらくあなたがうまくいったと言った2dバージョンの例でしょうか?私はそれをより良く説明できるかもしれません。 –
exapleと2D版のメインポストを編集しました。 a、b、cが平面の単位ベクトルであれば、何らかの創造力を発揮し、負傷していますか?線方程式のx0、y0、z0であるか?複数の組み合わせを実行しましたが、ゼロに等しくすることができます。 – TeaWave