Your code does exactly implement my instructions; however, I'm afraid that I made a mistake in my instructions, , and that is probably why you're seeing the wrong results.
In fact, when you do this:
intersect.x = v1.x - normal.x*dotp;intersect.y = v1.y - normal.y*dotp;intersect.z = v1.z - normal.z*dotp;
You're actually projecting the point down onto the triangle. The value of intersect computed here is the vector within the plane of the triangle that, when added to tri.v1, gives you the point of interest projected onto the plane of the triangle. This length of this has nothing to do with what you want.
Here's the fix, . Replace the code above with the following:
intersect.x = - normal.x*dotp;intersect.y = - normal.y*dotp;intersect.z = - normal.z*dotp;
With this code, intersect will be the vector that points from the point of interest (x,y,z) towards the triangle. This value of intersect is perpendicular to the triangle, and its magnitude is the minimum distance to the plane of the triangle.
Since you're using this for collision detection, you should note that this code really just gives the minimum distance to the plane of the triangle and not really to the triangle itself. It is entirely possible, even likely, that your point of interest may still be very far away from the triangle. If the triangle is horizontal, then the point may be horizontally a 1000 units away, but only 50 units perpendicular to the triangle's plane. How to fix this easily? You may want to do an oriented bounding box check first, make sure the point is contained within the bounding box before checking the minimum distance. If the point falls outside the bounding box, skip the more detailed check.
I do apologize for the error. I just didn't review it carefully enough to begin with.
Edited by - grhodes_at_work on July 13, 2001 1:26:24 PM