コード例 #1
0
        public static void Optimize(ref List <AStarNode> path, ref List <Int3> vectorPath)
        {
            if (path == null || path.Count == 0 || vectorPath == null || vectorPath.Count != path.Count)
            {
                return;
            }

            funnelPath.Clear();
            left.Clear();
            right.Clear();

            // Add start point
            left.Add(vectorPath[0]);
            right.Add(vectorPath[0]);

            for (int i = 0; i < path.Count - 1; i++)
            {
                NavMeshNode node0 = path[i] as NavMeshNode;
                NavMeshNode node1 = path[i + 1] as NavMeshNode;
                // Get the portal between path[i] and path[i+1] and add it to the left and right lists
                bool portalWasAdded = node0.GetPortal(node1, left, right);

                if (!portalWasAdded)
                {
                    // Fallback, just use the positions of the nodes
                    left.Add(node0.position);
                    right.Add(node0.position);

                    left.Add(node1.position);
                    right.Add(node1.position);
                }
            }

            // Add end point
            left.Add(vectorPath[vectorPath.Count - 1]);
            right.Add(vectorPath[vectorPath.Count - 1]);

            if (!RunFunnel(left, right, funnelPath))
            {
                // If funnel algorithm failed, degrade to simple line
                funnelPath.Add(vectorPath[0]);
                funnelPath.Add(vectorPath[vectorPath.Count - 1]);
            }

            vectorPath.Clear();
            vectorPath.AddRange(funnelPath);

            left.Clear();
            right.Clear();
        }
コード例 #2
0
        public override bool LineCastForMoving(ref HitInfo hit, MoveType mov)
        {
            Int3 from = hit.from;
            Int3 to   = hit.to;

            hit.hitPosition = from;

            if (trace != null)
            {
                trace.Clear();
            }

            Int3 end    = to;
            Int3 origin = from;

            if (origin == end)
            {
                hit.hitPosition = from;
                return(false);
            }

            NavMeshNode node = GetNearest(from, NNConstraint.None).node;

            if (node == null)
            {
                Debug.LogError("Could not find a valid node to start from");
                hit.hitPosition = from;
                return(true);
            }

            origin = node.ClosestPointOnNode(origin);

            List <Int3> left = Util.ListPool <Int3> .Claim();

            List <Int3> right = Util.ListPool <Int3> .Claim();

            int counter = 0;

            while (true)
            {
                counter++;
                if (counter > 2000)
                {
                    Debug.LogError("Linecast was stuck in infinite loop. Breaking.");
                    Util.ListPool <Int3> .Release(left);

                    Util.ListPool <Int3> .Release(right);

                    return(true);
                }

                NavMeshNode newNode = null;

                if (trace != null)
                {
                    trace.Add(node);
                }

                if (node.ContainsPoint(end))
                {
                    hit.hitPosition = to;
                    Util.ListPool <Int3> .Release(left);

                    Util.ListPool <Int3> .Release(right);

                    return(false);
                }

                for (int i = 0; i < node.connections.Length; i++)
                {
                    left.Clear();
                    right.Clear();

                    NavMeshNode other = GetNode(node.connections[i]) as NavMeshNode;
                    if (!node.GetPortal(other, left, right))
                    {
                        continue;
                    }

                    Int3 a = left[0];
                    Int3 b = right[0];

                    //i.e Left or colinear
                    if (!VectorMath.RightXZ(a, b, origin))
                    {
                        if (VectorMath.RightXZ(a, b, end))
                        {
                            //Since polygons are laid out in clockwise order, the ray would intersect (if intersecting) this edge going in to the node, not going out from it
                            continue;
                        }
                    }

                    float factor1, factor2;

                    if (VectorMath.LineIntersectionFactorXZ(a, b, origin, end, out factor1, out factor2))
                    {
                        //Intersection behind the start
                        if (factor2 < 0)
                        {
                            continue;
                        }

                        if (factor1 >= 0 && factor1 <= 1)
                        {
                            newNode = other;
                            break;
                        }
                    }
                }

                if (newNode == null)
                {
                    //Possible edge hit
                    int vs = node.GetVertexCount();

                    for (int i = 0; i < vs; i++)
                    {
                        var a = node.GetVertex(i);
                        var b = node.GetVertex((i + 1) % vs);


                        //i.e left or colinear
                        if (!VectorMath.RightXZ(a, b, origin))
                        {
                            //Since polygons are laid out in clockwise order, the ray would intersect (if intersecting) this edge going in to the node, not going out from it
                            if (VectorMath.RightXZ(a, b, end))
                            {
                                //Since polygons are laid out in clockwise order, the ray would intersect (if intersecting) this edge going in to the node, not going out from it
                                continue;
                            }
                        }

                        float factor1, factor2;
                        if (VectorMath.LineIntersectionFactorXZ(a, b, origin, end, out factor1, out factor2))
                        {
                            if (factor2 < 0)
                            {
                                continue;
                            }

                            if (factor1 >= 0 && factor1 <= 1)
                            {
                                Vector3 intersectionPoint = (Vector3)a + (Vector3)(b - a) * factor1;
                                hit.hitPosition = new Int3(intersectionPoint);

                                Util.ListPool <Int3> .Release(left);

                                Util.ListPool <Int3> .Release(right);

                                return(true);
                            }
                        }
                    }

                    //Ok, this is wrong...
                    Debug.LogWarning("Linecast failing because point not inside node, and line does not hit any edges of it");

                    Util.ListPool <Int3> .Release(left);

                    Util.ListPool <Int3> .Release(right);

                    return(false);
                }

                node = newNode;
            }
        }