コード例 #1
0
    /// <summary>
    /// determines if the line segment defined by v1 and v2 intersects any geometry in the tree.
    /// </summary>
    /// <param name="v1">vertex that defines the start of the ray</param>
    /// <param name="v2">vertex that defines the end of the ray</param>
    /// <returns>true if the ray collides with the mesh</returns>
    bool SplitsRay(Vector3 v1, Vector3 v2)
    {
        var v1IsInFront = IsInFront(v1);
        var v2IsInFront = IsInFront(v2);
        var result      = v1IsInFront != v2IsInFront;

        if (!result)
        {
            /// both vertices are on the same side of the plane,
            /// so this node doesn't split anything. Check it's children.
            if (v1IsInFront && front != null)
            {
                result = front.SplitsRay(v1, v2);
            }
            else if (!v1IsInFront && back != null)
            {
                result = back.SplitsRay(v1, v2);
            }
        }
        else
        {
            /// this plane splits the ray, but the intersection point may not be within the face boundaries.
            /// 1. calculate the intersection of the plane and the ray : intersection
            /// 2. create two new line segments: v1->intersection and intersection->v2
            /// 3. Recursively check those two segments against the rest of the tree.
            var intersection = new Vector3();

            /// insert code to magically calculate the intersection here.

            var frontSegmentSplits = false;
            var backSegmentSplits  = false;


            if (front != null)
            {
                if (v1IsInFront)
                {
                    frontSegmentSplits = front.SplitsRay(v1, intersection);
                }
                else if (v2IsInFront)
                {
                    frontSegmentSplits = front.SplitsRay(v2, intersection);
                }
            }
            if (back != null)
            {
                if (!v1IsInFront)
                {
                    backSegmentSplits = back.SplitsRay(v1, intersection);
                }
                else if (!v2IsInFront)
                {
                    backSegmentSplits = back.SplitsRay(v2, intersection);
                }
            }

            result = frontSegmentSplits || backSegmentSplits;
        }

        return(result);
    }