Esempio n. 1
0
        // Token: 0x0600242D RID: 9261 RVA: 0x00196788 File Offset: 0x00194988
        private void Update()
        {
            if (this.graph == null)
            {
                return;
            }
            Vector3 a = this.PointToGraphSpace(this.graph.center);
            Vector3 b = this.PointToGraphSpace(this.target.position);

            if (VectorMath.SqrDistanceXZ(a, b) > this.updateDistance * this.updateDistance)
            {
                this.UpdateGraph();
            }
        }
Esempio n. 2
0
        /// <summary>Update is called once per frame</summary>
        void Update()
        {
            if (graph == null)
            {
                return;
            }

            // Calculate where the graph center and the target position is in graph space
            var graphCenterInGraphSpace    = PointToGraphSpace(graph.center);
            var targetPositionInGraphSpace = PointToGraphSpace(target.position);

            // Check the distance in graph space
            // We only care about the X and Z axes since the Y axis is the "height" coordinate of the nodes (in graph space)
            // We only care about the plane that the nodes are placed in
            if (VectorMath.SqrDistanceXZ(graphCenterInGraphSpace, targetPositionInGraphSpace) > updateDistance * updateDistance)
            {
                UpdateGraph();
            }
        }
Esempio n. 3
0
 public static float SqrMagnitudeXZ(Vector3 a, Vector3 b)
 {
     return(VectorMath.SqrDistanceXZ(a, b));
 }
Esempio n. 4
0
        // this function keeps the path generated away from the edge of the nav mesh, replacing funnel path points which are on a triangle vertex
        // with new points which are offset into the triangle away from the vertex
        private void PadFunnelPath(ref List <Vector3> funnelPath, ref List <GraphNode> path, Path _p)
        {
            ABPath      abPath          = _p as ABPath;
            const float SamePositionSqr = 0.001f * 0.001f;

            bool skipFirstPoint = false;
            bool skipLastPoint  = false;

            if (null != abPath && funnelPath.Count > 0)
            {   // don't want to mess with the start point
                if (VectorMath.SqrDistanceXZ(funnelPath[0], abPath.originalStartPoint) < SamePositionSqr)
                {
                    skipFirstPoint = true;
                }
                // don't want to mess with the end point
                if (VectorMath.SqrDistanceXZ(funnelPath[funnelPath.Count - 1], abPath.originalEndPoint) < SamePositionSqr)
                {
                    skipLastPoint = true;
                }
            }

            int startNode = 0;

            for (int point = 0; point < funnelPath.Count; ++point)
            {
                Vector3 funnelPoint = funnelPath[point];

                if (0 == point && skipFirstPoint)
                {
                    continue;
                }

                if (funnelPath.Count - 1 == point && skipLastPoint)
                {
                    continue;
                }

                bool hasPointBeenRemoved = false; // we will remove the funnel path point if it is found to be on a triangle vertex (it'll be replaced with offset points)

                ++startNode;
                // go over all points apart from the end node, as we don't want to add new path points in the end node as that node contains the target position
                for (int node = startNode; node < path.Count - 1; ++node)
                {
                    if (node < 0 || node >= path.Count)
                    {
                        continue;
                    }
                    TriangleMeshNode theNode = path[node] as TriangleMeshNode;

                    Vector3[] vertices = new Vector3[3] {
                        (Vector3)theNode.GetVertex(0), (Vector3)theNode.GetVertex(1), (Vector3)theNode.GetVertex(2)
                    };

                    for (int vert = 0; vert < 3; ++vert)
                    {
                        if (VectorMath.SqrDistanceXZ(funnelPoint, vertices[vert]) < SamePositionSqr) // the triangle has a vertex on the funnel path
                        {
                            bool isNodeOnTheLine = false;
                            // in this section we test if the triangle has an edge on the funnel path
                            if (point < funnelPath.Count - 1) // we need another point after point
                            {
                                Vector3 nextFunnelPoint  = funnelPath[point + 1];
                                Vector3 vertNotOnTheLine = new Vector3(); // if two of our triangle verts are on the funnel path, this is the other vert
                                if (VectorMath.SqrDistanceXZ(vertices[(vert + 2) % 3], nextFunnelPoint) < SamePositionSqr)
                                {
                                    isNodeOnTheLine  = true;
                                    vertNotOnTheLine = vertices[(vert + 1) % 3]; // other point is next
                                }
                                else if (VectorMath.SqrDistanceXZ(vertices[(vert + 1) % 3], nextFunnelPoint) < SamePositionSqr)
                                {
                                    isNodeOnTheLine  = true;
                                    vertNotOnTheLine = vertices[(vert + 2) % 3]; // other point is prev
                                }

                                if (isNodeOnTheLine)
                                {
                                    Vector3 pushAwayDirNorm = vertNotOnTheLine - VectorMath.ClosestPointOnLine(funnelPoint, nextFunnelPoint, vertNotOnTheLine);
                                    pushAwayDirNorm.y = 0f;
                                    pushAwayDirNorm.Normalize();

                                    // add a new point to the funnel path which is offset at a right angle from triangle line which is coincident with the funnel path
                                    Vector3 lineStart       = funnelPoint + ((nextFunnelPoint - funnelPoint) * 0.25f);
                                    Vector3 firstPointToAdd = lineStart + pushAwayDirNorm * obstaclePadding;
                                    float   maxLineLen      = CalculateMaxLineLengthXZ(lineStart, firstPointToAdd, nextFunnelPoint, vertNotOnTheLine, vertices[vert]);
                                    firstPointToAdd = lineStart + pushAwayDirNorm * maxLineLen;
                                    AddPadPointToFunnelPath(ref funnelPath, ref point, ref hasPointBeenRemoved, firstPointToAdd);

                                    // add another point to the funnel path which is offset at a right angle from triangle line which is coincident with the funnel path
                                    lineStart = funnelPoint + ((nextFunnelPoint - funnelPoint) * 0.75f);
                                    Vector3 secondPointToAdd = lineStart + pushAwayDirNorm * obstaclePadding;
                                    maxLineLen       = CalculateMaxLineLengthXZ(lineStart, secondPointToAdd, nextFunnelPoint, vertNotOnTheLine, vertices[vert]);
                                    secondPointToAdd = lineStart + pushAwayDirNorm * maxLineLen;
                                    AddPadPointToFunnelPath(ref funnelPath, ref point, ref hasPointBeenRemoved, secondPointToAdd);
                                }
                            }

                            // if the node did not have an edge on the funnel path, let's just offset from the single triangle vertex on the funnel path
                            if (!isNodeOnTheLine)
                            {
                                Vector3 toPrevVert = (vertices[(vert + 2) % 3] - vertices[vert]);
                                Vector3 toNextVert = (vertices[(vert + 1) % 3] - vertices[vert]);

                                // calculate a line between the two triangle edges and offset a point on that line
                                Vector3 newNavPoint = Vector3.Lerp(vertices[vert] + toPrevVert.normalized, vertices[vert] + toNextVert.normalized, 0.5f);
                                float   maxLength   = CalculateMaxLineLengthXZ(vertices[vert], newNavPoint, vertices[(vert + 1) % 3], vertices[(vert + 2) % 3]);
                                Vector3 dirNorm     = (newNavPoint - vertices[vert]);
                                dirNorm.y   = 0f;
                                newNavPoint = vertices[vert] + dirNorm.normalized * Mathf.Min(obstaclePadding, maxLength);
                                AddPadPointToFunnelPath(ref funnelPath, ref point, ref hasPointBeenRemoved, newNavPoint);
                            }
                            startNode = node;
                            break;
                        }
                    }
                }
            }
        }