RayTriangleIntersect() public static method

public static RayTriangleIntersect ( Ray r, System.Vector3 vert0, System.Vector3 vert1, System.Vector3 vert2, float &distance ) : bool
r Ray
vert0 System.Vector3
vert1 System.Vector3
vert2 System.Vector3
distance float
return bool
        /// <summary>
        ///
        /// </summary>
        /// <param name="selectRay">A ray in Xna Coords that was cast according to the viewport</param>
        /// <param name="add">Whether to add the selected triangle to the selection</param>
        public void UpdateSelectedTriangle(Ray selectRay, bool add)
        {
            if (!add)
            {
                Vertices.Clear();
                Indices.Clear();
            }

            var pos3D = selectRay.Position;
            var dir3D = selectRay.Direction;

            PositionUtil.TransformXnaCoordsToWoWCoords(ref pos3D);
            PositionUtil.TransformXnaCoordsToWoWCoords(ref dir3D);
            var ray3D = new Ray(pos3D, dir3D);

            var pos2D = new Vector2(pos3D.X, pos3D.Y);
            var dir2D = new Vector2(dir3D.X, dir3D.Y).NormalizedCopy();
            var ray2D = new Ray2D(pos2D, dir2D);

            var closestTime = float.MaxValue;
            var closestVec0 = Vector3.Zero;
            var closestVec1 = Vector3.Zero;
            var closestVec2 = Vector3.Zero;

            foreach (var tile in _adtManager.MapTiles)
            {
                var results = new List <Index3>();
                if (!tile.GetPotentialColliders(ray2D, results))
                {
                    continue;
                }

                foreach (var tri in results)
                {
                    var vec0 = tile.TerrainVertices[tri.Index0];
                    var vec1 = tile.TerrainVertices[tri.Index1];
                    var vec2 = tile.TerrainVertices[tri.Index2];

                    float time;
                    if (!Intersection.RayTriangleIntersect(ray3D, vec0, vec1, vec2, out time))
                    {
                        continue;
                    }
                    if (time > closestTime)
                    {
                        continue;
                    }

                    closestTime = time;
                    closestVec0 = vec0;
                    closestVec1 = vec1;
                    closestVec2 = vec2;
                }
            }
            if (closestTime == float.MaxValue)
            {
                return;
            }
            AddSelectedTriangle(closestVec0, closestVec1, closestVec2);
        }
Exemplo n.º 2
0
        internal bool FirstPointOfIntersection(ref Ray ray, ref float tMax, Vector3[] vertices, out Vector3 pointOfIntersection)
        {
            var rayCopy = ray;

            var dist = float.MaxValue;

            VisitNodes(rootId, ref ray, ref tMax, (node) =>
            {
                if (node == null)
                {
                    return;
                }
                if (node.TriIndices.Length == 0)
                {
                    return;
                }

                for (var i = 0; i < node.TriIndices.Length; i++)
                {
                    var tri = node.TriIndices[i];
                    var v0  = vertices[tri.Index0];
                    var v1  = vertices[tri.Index1];
                    var v2  = vertices[tri.Index2];

                    float newDist;
                    if (!Intersection.RayTriangleIntersect(rayCopy, v0, v1, v2, out newDist))
                    {
                        continue;
                    }

                    //Collision happens behind the startPos
                    if (newDist < 0.0f)
                    {
                        continue;
                    }

                    dist = Math.Min(dist, newDist);
                }
            });

            if (dist < tMax)
            {
                pointOfIntersection = ray.Position + dist * ray.Direction;
                return(true);
            }

            pointOfIntersection = Vector3.Zero;
            return(false);
        }
Exemplo n.º 3
0
        internal bool IntersectsWith(ref Ray ray, ref float tMax, Vector3[] vertices)
        {
            var rayCopy = ray;

            var dist = float.MaxValue;

            VisitNodes(rootId, ref ray, ref tMax, (node) =>
            {
                if (node == null)
                {
                    return;
                }
                if (node.TriIndices.Length == 0)
                {
                    return;
                }
                foreach (var tri in node.TriIndices)
                {
                    var v1 = vertices[tri.Index0];
                    var v2 = vertices[tri.Index1];
                    var v3 = vertices[tri.Index2];

                    float newDist;
                    if (!Intersection.RayTriangleIntersect(rayCopy, v1, v2, v3, out newDist))
                    {
                        continue;
                    }

                    //Collision happens behind the startPos
                    if (newDist < 0.0f)
                    {
                        continue;
                    }

                    dist = Math.Min(dist, newDist);
                }
            });

            return(dist < tMax);
        }