I have two points in my world, P0 and P1 (xyz,..) which basically represent the start of my line and the end of my line. I also have a plane/side with 4 points, ABCD (xyz,..).
What I want to do is send a "ray" (in this case a line) through the plane and check whether or not it intersects with it. I have used this (How to find intersection point of a line in a plane in 3D space using MATLAB) question as a reference and this function mathlabcentral. I tried the example "isp-zax" gave and that does give me the same values as he does, but as soon as I plop in my own values it is clearly wrong. The answers formula;
>> AB = B-A
AB = 0.8660 0.5000 0
>> AD = D-A
AD = 0 0 1
>> n = cross(AB,AD)/sqrt(dot(cross(AB,AD),cross(AB,AD)))
n = 0.5000 -0.8660 0
>> [I,check]=plane_line_intersect(n,A,P0,P1)
I = 1.0961 44.5116 6.6948
check = 3
--
So the first set of line points (P) work perfectly fine which it should (see image) but if I move the end point of the line (P1) outside of the plane it still says it intersects (see 2nd image). What could cause this?
A = [1312.901701, 2360.12542566832, 74.24415425756794];
B = [1312.901701, 2423.58274087539, 88.31230234462595];
C = [1312.901701, 2371.241313396465, 24.103624956470995];
D = [1312.901701, 2434.698628603535, 38.171773043529];
P0 = [1495.468628, 2261.038086, 161.329498];
P1 = [1153.250854, 2479.341797, -14.787056];
--
This is it in javascript;
const ab = subtract(B, A);
const ad = subtract(D, A);
const n = divide(cross(ab, ad), sqrt(dot(cross(ab, ad), cross(ab, ad))));
const intersects = planeLineIntersect(n, A, P0, P1);
function planeLineIntersect(n, v0, p0, p1) {
const u = subtract(p1, p0);
const w = subtract(p0, v0);
const D = dot(n, u);
const N = -dot(n, w);
let check = 0;
if (abs(D) < 0.0000001) {
if (N === 0) {
check = 2;
return check;
}
check = 0;
return check;
}
const sI = divide(N, D);
const I = add(p0, multiply(sI, u)); // not used
if (sI < 0 || sI > 1) {
check = 3;
} else check = 1;
return check;
}