Tea*_*ave 3 python graphics geometry intersection
喜.制作游戏并且正在寻找仅在3D空间中的正方形或矩形的光线交叉.有搜索网络,发现了很多解决方案,但我无法理解的是在2D中有线和线段交叉脚本,但我无法弄清楚必须使它成为3D.从它与正方形或矩形相交的一侧并不重要,但它必须能够反转交点矢量,以便以后可以测试距离se的距离,如果它发生在同一光线交叉点上的其他交叉点之前或之后.
将非常感谢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>;
Run Code Online (Sandbox Code Playgroud)
}
当您定义具有点(=矢量)和方向矢量的光线,以及具有点(=矢量)的矩形和表示边的两个矢量时,解决方案非常简单.
假设射线被定义为R0 + t * D,射线R0的原点在哪里,D是表示其方向和的方向的单位矢量t是它的长度.
矩形可以用角点P0和两个矢量表示S1,S2它们应该代表边(它们的长度等于边的长度).你将需要另一矢量N垂直于它的表面,其等于沿着的交叉乘积的单位矢量S1和S2.
现在,假设光线与矩形相交P.然后,射线的方向,D必须与正常角度成非零角度N.这可以通过检查来验证D.N < 0.
要找到交叉点,请假设P = R0 + a * D(该点必须在光线上).你需要找到anow.Find向量的值P0P.这必须垂直于N,这意味着P0P.N = 0减少到a = ((P0 - R0).N) / (D.N).
现在你需要检查点是否在矩形内.要做到这一点,需要投影Q1的P0P顺S1和Q2的P0P一起S2.那个点在里面的条件是0 <= length(Q1) <= length(S1)和0 <= length(Q2) <= length(S2).
此方法适用于任何类型的平行四边形,不仅适用于矩形.
在 R3 中为一条线创建一个向量方程,然后求解该线在您要测试它的矩形平面中的交点。之后,测试该解点是否在范围内就足够简单了。
解的参数 t 可以通过以下公式找到:
t = (a * (x0 - rx) + b * (y0 - ry) + c * (x0 - rz)) / (a * vx + b * vy + c * vz)
Run Code Online (Sandbox Code Playgroud)
在哪里:
a(x - x0) + b(y - y0) + c(z - z0) = 0
Run Code Online (Sandbox Code Playgroud)
是矩形所在平面的方程
和:
<x, y, z> = <rx + vx * t, ry + vy * t, rz + vz * t>
Run Code Online (Sandbox Code Playgroud)
是相关直线的矢量方程。
注意:
<rx, ry, rz>
Run Code Online (Sandbox Code Playgroud)
是矢量方程的初始点,并且
<vx, vy, vz>
Run Code Online (Sandbox Code Playgroud)
是上式的方向向量
之后,将参数 t 代入向量方程将为您提供测试距离的点。
