Collision between a line and a face

331 views Asked by At

Ok, so on the internet, I have seen equations for solving this, but they require the normal of the plane, and are a lot higher math than I know.

Basically, if I have an x,y,z position (as well as x,y,z rotations) for my ray, and x,y,z for three points that represent my plane, How would I solve for the point of collision?

I have done 2D collisions before, but I am clueless on how this would work in 3D. Also, I work in java, though I understand C# well enough.

Thanks to the answer below, I was able to find the normal of my face. This then allowed me to, through trial and error and http://geomalgorithms.com/a05-_intersect-1.html, come up with the following code (hand made vector math excluded):

Vertice Vertice1 = faces.get(f).getV1();
Vertice Vertice2 = faces.get(f).getV2();
Vertice Vertice3 = faces.get(f).getV3();

Vector v1 = vt.subtractVertices(Vertice2, Vertice1);
Vector v2 = vt.subtractVertices(Vertice3, Vertice1);
Vector normal = vt.dotProduct(v1, v2);

//formula = -(ax + by + cz + d)/n * u where a,b,c = normal(x,y,z) and where u = the vector of the ray from camX,camY,camZ,
// with a rotation of localRotX,localRotY,localRotZ

double Collision = 
                 -(normal.x*camX + normal.y*camY + normal.z*camZ) / vt.dotProduct(normal, vt.subtractVertices(camX,camY,camZ,
                 camX + Math.sin(localRotY)*Math.cos(localRotX),camY + Math.cos(localRotY)*Math.cos(localRotX),camZ + Math.sin(localRotX)));

This code, mathimatically should work, but I have yet to properly test the code. Tough I will continue working on this, I consider this topic finished. Thank you.

1

There are 1 answers

0
Kevin Ward On BEST ANSWER

It would be very helpful to post one of the equations that you think would work for your situation. Without more information, I can only suggest using basic linear algebra to get the normal vector for the plane from the data you have.

In R3 (a.k.a. 3d math), the cross product of two vectors will yield a vector that is perpendicular to the two vectors. A plane normal vector is a vector that is perpendicular to the plane.

You can get two vectors that lie in your plane from the three points you mentioned. Let's call them A, B, and C.

v1 = B - A

v2 = C - A

normal = v1 x v2

Stackoverflow doesn't have Mathjax formatting so that's a little ugly, but you should get the idea: construct two vectors from your three points in the plane, take the cross product of your two vectors, and then you have a normal vector. You should then be closer to adapting the equation to your needs.