The shape is defined with the same arguments as the one in its draw code (x1,y1,z1,x2,y2,z2). The line is defined by point (x0,y0,z0) and vector (dx,dy,dz), where the point is any point on the line, and the vector is the line's direction vector. For example, a line going through points (x3,y3,z3) and (x4,y4,z4), the point would be (x3,y3,z3) and the vector (x4-x3,y4-y3,z4-z3).
The scripts automatically transform the line into a ray. The difference between these is that the line continues to the opposite side of the vector. In order to transform this back to checking for lines, delete the line in the script that says "This line makes it a ray".
Now there are also available additional codes that will find the point of intersection and the surface normal at that. See post #21.
Now you can also download and import all the scripts at the same time in the form of this gmres file. The scripts have their additional code for the point of intersection and surface normal in them by default.
If you have problems using these scripts, you can check out This example. It explains how to use the scripts.
Line - block:
///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects the block.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var a, b, c, xx, yy, zz, Dx, Dy, Dz, d; a = abs(argument3 - argument0)/2; b = abs(argument4 - argument1)/2; c = abs(argument5 - argument2)/2; xx = (argument3 + argument0)/2 - argument6; yy = (argument4 + argument1)/2 - argument7; zz = (argument5 + argument2)/2 - argument8; Dx = argument9; if (Dx == 0) {Dx = .000001;} Dy = argument10; if (Dy == 0) {Dy = .000001;} Dz = argument11; if (Dz == 0) {Dz = .000001;} d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector return ( (abs(yy - Dy * (xx + a) / Dx) <= b && abs(zz - Dz * (xx + a) / Dx) <= c) || (abs(yy - Dy * (xx - a) / Dx) <= b && abs(zz - Dz * (xx - a) / Dx) <= c) || (abs(xx - Dx * (yy + b) / Dy) <= a && abs(zz - Dz * (yy + b) / Dy) <= c) || (abs(xx - Dx * (yy - b) / Dy) <= a && abs(zz - Dz * (yy - b) / Dy) <= c) || (abs(xx - Dx * (zz + c) / Dz) <= a && abs(yy - Dy * (zz + c) / Dz) <= b) || (abs(xx - Dx * (zz - c) / Dz) <= a && abs(yy - Dy * (zz - c) / Dz) <= b) && (xx*Dx + yy*Dy + zz*Dz >= 0) //This line makes it a ray )}Line - ellipsoid:
///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects ellipsoid.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var a, b, c, Dx, Dy, Dz, A, B, C, x0, y0, z0, d; a = sqr(argument3 - argument0)/4; b = sqr(argument4 - argument1)/4; c = sqr(argument5 - argument2)/4; x0 = argument6 - (argument3 + argument0)/2; y0 = argument7 - (argument4 + argument1)/2; z0 = argument8 - (argument5 + argument2)/2; Dx = argument9; if (Dx == 0) Dx = .000001; Dy = argument10; if (Dy == 0) Dy = .000001; Dz = argument11; if (Dz == 0) Dz = .000001; d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector A = Dx * Dx / a + Dy * Dy / b + Dz * Dz / c; B = Dx * x0 / a + Dy * y0 / b + Dz * z0 / c; C = x0 * x0 / a + y0 * y0 / b + z0 * z0 / c - 1; C = B * B - A * C; if (C < 0) {return 0;} //<point of intersection and normal finding code goes here> if (sqrt(C ) + B)/A >= 0 {return 0;} //This line makes it a ray return 1; }Line - cone:
///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects the cone.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var a, b, c, Dx, Dy, Dz, A, B, C, x0, y0, z0, d; a = sqr(argument3 - argument0)/4; b = sqr(argument4 - argument1)/4; c = (argument5 - argument2); x0 = argument6 - (argument3 + argument0)/2; y0 = argument7 - (argument4 + argument1)/2; z0 = argument8 - argument5; Dx = argument9; if (Dx == 0) Dx = .000001; Dy = argument10; if (Dy == 0) Dy = .000001; Dz = argument11; if (Dz == 0) Dz = .000001; d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector A = Dx * Dx / a + Dy * Dy / b - Dz * Dz / c / c; B = Dx * x0 / a + Dy * y0 / b - Dz * z0 / c / c; C = x0 * x0 / a + y0 * y0 / b - z0 * z0 / c / c; C = B*B - A*C; if (C < 0) {return 0;} //<point of intersection and normal finding code goes here> if (z0/c + 1 <= 0) {A = (-B + sqrt( C))/A;} else {A = (-B - sqrt( C))/A;} if (A < 0) {return 0;} //This line makes it a ray return (abs( 0.5 + (A * Dz + z0)/c ) <= 0.5); }Line - cylinder:
///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects cylinder.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var a, b, c, Dx, Dy, Dz, A, B, C, x0, y0, z0, d; a = sqr(argument3 - argument0)/4; b = sqr(argument4 - argument1)/4; c = (argument5 - argument2)/2; x0 = argument6 - (argument3 + argument0)/2; y0 = argument7 - (argument4 + argument1)/2; z0 = argument8 - argument5; Dx = argument9; if (Dx == 0) Dx = .000001; Dy = argument10; if (Dy == 0) Dy = .000001; Dz = argument11; if (Dz == 0) Dz = .000001; d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector A = Dx * Dx / a + Dy * Dy / b; B = Dx * x0 / a + Dy * y0 / b; C = x0 * x0 / a + y0 * y0 / b - 1; C = B*B - A*C; if (C < 0) {return 0;} if (z0/c + 2 <= 0) {x0 = (-B + sqrt( C))/A;} else {x0 = (-B - sqrt( C))/A;} if (z0/c >= 0) {y0 = (-B + sqrt( C))/A;} else {y0 = (-B - sqrt( C))/A;} if (x0 < 0) {return 0;} //This line makes it a ray //<point of intersection and normal finding code goes here> return ((x0 * Dz + z0)/c >= -2 && (y0 * Dz + z0)/c <= 0); }Line - floor///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects a floor.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var xx, yy, zz, Dx, Dy, Dz, x0, y0, z0, d; xx = argument3 - argument0; yy = argument4 - argument1; zz = argument5 - argument2; x0 = argument6 - argument0; y0 = argument7 - argument1; z0 = argument8 - argument2; Dx = argument9; if (Dx == 0) Dx = .000001; Dy = argument10; if (Dy == 0) Dy = .000001; Dz = argument11; if (Dz == 0) Dz = .000001; d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector d = (zz * Dx - xx * Dz); if (d == 0) return 0; d = (xx * z0 - zz * x0)/d; if (d <= 0) return 0; //This line makes it a ray //<point of intersection and normal finding code goes here> return ((abs(2*(x0 + Dx*d) - xx) <= abs(xx) || abs(xx) < .001) && abs(2*(y0 + Dy*d) - yy) <= abs(yy) && (abs(2*(z0 + Dz*d) - zz) <= abs(zz) || abs(zz) < .001));}Line - wall///////////////////////////////////////////////////////////////////////////// Description: This script returns true if the line intersects a wall.//// arguments: (x1, y1, z1, x2, y2, z2, x0, y0, z0, dx, dy, dz)// The line is given in form (x0 + dxt, y0 + dyt, z0 + dzt), t is any real.//////////////////////////////// ~ Tepi //////////////////////////////////{ var xx, yy, zz, Dx, Dy, Dz, x0, y0, z0, d; xx = argument3 - argument0; yy = argument4 - argument1; zz = argument5 - argument2; x0 = argument6 - argument0; y0 = argument7 - argument1; z0 = argument8 - argument2; Dx = argument9; if (Dx == 0) Dx = .000001; Dy = argument10; if (Dy == 0) Dy = .000001; Dz = argument11; if (Dz == 0) Dz = .000001; d = sqrt(Dx*Dx + Dy*Dy + Dz*Dz); //The direction vector's magnitude Dx /= d; Dy /= d; Dz /= d; //Normalizing the direction vector d = (yy * Dx - xx * Dy); if (d == 0) return 0; d = (xx * y0 - yy * x0)/d; if (d <= 0) return 0; //This line makes it a ray //<point of intersection and normal finding code goes here> return ((abs(2*(x0 + Dx*d) - xx) <= abs(xx) || abs(xx) < .001) && (abs(2*(y0 + Dy*d) - yy) <= abs(yy) || abs(yy) < .001) && abs(2*(z0 + Dz*d) - zz) <= abs(zz));}The technique behind the block - line was to represent the block and the line algebrally*; as block=(|{center}-{point}| <= {axis}) and line=({point} = {linepoint}+{vector}*t), where t is any number. When solved for t, three (for x,y and z) possible intervals of t are found, and the satisfying values of t are obviously in the intersection of all those. In any case, atleast one of the interval 'endpoints' is inside that region. And those are the only t values that need to be checked. The values are integrated in the expressions for efficiency.
*"algebrally" is a new word I found. It makes this topic come as the first results of Google search for "algebrally", so it makes this topic unique. Well... in reality it is an equivalent to algebraically. Lol.
All the other ones were done in same algebral way, but for these I needed to yield shape equations I didn't know of. A great example of this is cone. Luckily the ellipse's equation was there to help to make some assumptions.
EDIT: I noticed a significant problem in Line - cone and Line - cylinder -scripts. They were only working in certain values of z1 and z2. However, now this problem is fixed.
EDIT2: Fixed another variable cc bug in Line - cone. All users are adviced to update their copies to these newly edited ones.
EDIT3: Added a gmres file.
EDIT4: Reuploaded the gmres file.
If you have any ideas to improve these by optimization or such, let me know. You could also let me know if you find important use for them. Have fun.
Edited by Tepi, 25 June 2012 - 04:20 PM.











