### Re: XNA Framework How to get distance from point to triangle or plane in XNA

minahito

Hi,

Here is a function of my library for Ray & Triangle. It may be buggy. I wrote it
based on OGRE. You get a distance which is from the ray's position to the intersecting point, so you can calculate the intersecting point as Vector3.

static public KeyValuePair<bool, float> Intersects(Ray ray, Vector3 a, Vector3 b, Vector3 c, Vector3 normal, bool positiveSide, bool negativeSide)

{

float t;

{

float denom = Vector3.Dot(normal, ray.Direction);

if (denom > float.Epsilon)

{

if (!negativeSide)

return new KeyValuePair<bool, float>(false, 0);

}

else if (denom < -float.Epsilon)

{

if (!positiveSide)

return new KeyValuePair<bool, float>(false, 0);

}

else

{

return new KeyValuePair<bool, float>(false, 0);

}

t = Vector3.Dot(normal, a - ray.Position) / denom;

if (t < 0)

{

// Interersection is behind origin

return new KeyValuePair<bool, float>(false, 0);

}

}

// Calculate the largest area projection plane in X, Y or Z.

int i0, i1;

{

float n0 = Math.Abs(normal.X);

float n1 = Math.Abs(normal.Y);

float n2 = Math.Abs(normal.Z);

i0 = 1;

i1 = 2;

if (n1 > n2)

{

if (n1 > n0) i0 = 0;

}

else

{

if (n2 > n0) i1 = 0;

}

}

float[] A = { a.X, a.Y, a.Z };

float[] B = { b.X, b.Y, b.Z };

float[] C = { c.X, c.Y, c.Z };

float[] R = { ray.Direction.X, ray.Direction.Y, ray.Direction.Z };

float[] RO = { ray.Position.X, ray.Position.Y, ray.Position.Z };

// Check the intersection point is inside the triangle.

{

float u1 = B[i0] - A[i0];

float v1 = B[i1] - A[i1];

float u2 = C[i0] - A[i0];

float v2 = C[i1] - A[i1];

float u0 = t * R[i0] + RO[i0] - A[i0];

float v0 = t * R[i1] + RO[i1] - A[i1];

float alpha = u0 * v2 - u2 * v0;

float beta = u1 * v0 - u0 * v1;

float area = u1 * v2 - u2 * v1;

float EPSILON = 1e-3f;

float tolerance = -EPSILON * area;

if (area > 0)

{

if (alpha < tolerance || beta < tolerance || alpha + beta > area - tolerance)

return new KeyValuePair<bool, float>(false, 0);

}

else

{

if (alpha > tolerance || beta > tolerance || alpha + beta < area - tolerance)

return new KeyValuePair<bool, float>(false, 0);

}

}

return new KeyValuePair<bool, float>(true, t);

}