selinux

Hi all

Ok, I'm trying to do ray picking in XNA with mesh positions and some terrain geometry, but I only found these classes:

  • Ray
  • Plane
  • BoundingBox
  • BoundingSphere
  • BoundingFrustum

Intuitively, I tried to use Ray with Plane to test for intersection, but ití»s actually impossible.

I need realistic intersection for vehicle positioning on my terrain, but I doní»t know how.

I search this forum and others but I didní»t find anything really useful.

I tried to use height-maps to store de heights, but the result isní»t good.

Any suggestions

Thanks in advance




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

Johnnylightbulb

That's like three questions in one, but let me try to help out:

Distance from point to the closest point on a plane, where 'plane' is a Plane instance, and 'point' is a Vector3:

float distance = Vector3.Dot(Vector3.Normalize(plane.Normal), point) - plane.D;

This gives you a distance, which you can infer quite a bit from. If distance < 0 then your point is on the back side of the plane, = 0 means the point is on the plane, and > 0 means in front of your plane.

For a ray, if the start position is on one side of the plane, and the point extended from the start towards the ray's direction (but much, much further) is on the other side, there is an intersection. For the point of intersection there would be some more math.





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

Michael Morton

There used to be several helper functions in the Plane class that were dropped in release 1.0. I'd like to see them put back in.




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

selinux.es

Thanks for the answer Johnny!

 

But isní»t enough for me :-P

 

That I really need is the intersection point between a Vector3 and a triangle. I think that I can define a Plane with the three points of the triangle, and intersect it with the Vector3. This is my question. Sorry for English.

 

Michael Morton says:

 

There used to be several helper functions in the Plane class that were dropped in release 1.0. I'd like to see them put back in

 

I doní»t remember these plane helper functionsíş There was something like Ií»m searching





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);
}






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

selinux.es

Thanks very much minahito. It's exactly what I need!





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

minahito

I noticed that Ray.Intersects(Plane) does not exist. I add such function here as notes:

        static public Nullable<float> Intersects(Ray ray, Plane plane)
        {
            float denom = Vector3.Dot(plane.Normal, ray.Direction);
            if (Math.Abs(denom) < float.Epsilon)
            {
                return null;
            }
            else
            {
                float nom = Vector3.Dot(plane.Normal, ray.Position) + plane.D;
                float t = -(nom / denom);

                if (t >= 0)
                    return t;

                return null;
            }
        }






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

selinux.es

Thanks a lot





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

Michael Morton

Triangle Intersection and related functions

Closest Point On Line Segment

Point Inside Edge

Point Inside Poly

Edge - Triangle Intersection

http://www.ziggyware.com/readarticle.php article_id=78

Various Plane functions missing from XNA

Plane Dot Coord

Plane Dot Normal

Ray Plane Intersection

Closest Point On Plane

Plane - Matrix Multiply

Distance from point to Plane

http://www.ziggyware.com/readarticle.php article_id=79