A TiledNavMesh is a continuous region, which is used for pathfinding.
Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NavMeshQuery"/> class.
        /// </summary>
        /// <param name="nav">The navigation mesh to query.</param>
        /// <param name="maxNodes">The maximum number of nodes that can be queued in a query.</param>
        /// <param name="rand">A random number generator for use in methods like <see cref="NavMeshQuery.FindRandomPoint()"/></param>
        public NavMeshQuery(TiledNavMesh nav, int maxNodes, Random rand)
        {
            this.nav = nav;

            nodePool = new NodePool(maxNodes/*, MathHelper.NextPowerOfTwo(maxNodes / 4)*/);
            tinyNodePool = new NodePool(64/*, 32*/);
            openList = new PriorityQueue<NavNode>(maxNodes);

            this.rand = rand;

            this.query = new QueryData();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NavMeshQuery"/> class.
        /// </summary>
        /// <param name="nav">The navigation mesh to query.</param>
        /// <param name="maxNodes">The maximum number of nodes that can be queued in a query.</param>
        /// <param name="rand">A random number generator for use in methods like <see cref="NavMeshQuery.FindRandomPoint()"/></param>
        public NavMeshQuery(TiledNavMesh nav, int maxNodes, Random rand)
        {
            this.nav = nav;

            areaCost = new float[byte.MaxValue + 1];
            for (int i = 0; i < areaCost.Length; i++)
                areaCost[i] = 1.0f;

            nodePool = new NodePool(maxNodes/*, MathHelper.NextPowerOfTwo(maxNodes / 4)*/);
            tinyNodePool = new NodePool(64/*, 32*/);
            openList = new PriorityQueue<Node>(maxNodes);

            this.rand = rand;
        }
Ejemplo n.º 3
0
        private void LoadNavMeshFromFile(string path)
        {
            try
            {

                tiledNavMesh = new NavMeshJsonSerializer().Deserialize(path);
                navMeshQuery = new NavMeshQuery(tiledNavMesh, 2048);
                hasGenerated = true;
                displayMode = DisplayMode.NavMesh;
            }
            catch (Exception e)
            {
                if (!interceptExceptions)
                    throw;
                else
                {
                    hasGenerated = false;
                    tiledNavMesh = null;
                    navMeshQuery = null;
                    Console.WriteLine("Navmesh loading failed with exception:" + Environment.NewLine + e.ToString());
                }
            }
        }
Ejemplo n.º 4
0
        private void GeneratePathfinding()
        {
            if (!hasGenerated)
                return;

            Random rand = new Random();
            NavQueryFilter filter = new NavQueryFilter();

            buildData = new NavMeshBuilder(polyMesh, polyMeshDetail, new SharpNav.Pathfinding.OffMeshConnection[0], settings);

            tiledNavMesh = new TiledNavMesh(buildData);
            navMeshQuery = new NavMeshQuery(tiledNavMesh, 2048);

            //Find random start and end points on the poly mesh
            /*int startRef;
            navMeshQuery.FindRandomPoint(out startRef, out startPos);*/

            SVector3 c = new SVector3(10, 0, 0);
            SVector3 e = new SVector3(5, 5, 5);
            navMeshQuery.FindNearestPoly(ref c, ref e, out startPt);

            navMeshQuery.FindRandomPointAroundCircle(ref startPt, 1000, out endPt);

            //calculate the overall path, which contains an array of polygon references
            int MAX_POLYS = 256;
            path = new Path();
            navMeshQuery.FindPath(ref startPt, ref endPt, filter, path);

            //find a smooth path over the mesh surface
            int npolys = path.Count;
            SVector3 iterPos = new SVector3();
            SVector3 targetPos = new SVector3();
            navMeshQuery.ClosestPointOnPoly(startPt.Polygon, startPt.Position, ref iterPos);
            navMeshQuery.ClosestPointOnPoly(path[npolys - 1], endPt.Position, ref targetPos);

            smoothPath = new List<SVector3>(2048);
            smoothPath.Add(iterPos);

            float STEP_SIZE = 0.5f;
            float SLOP = 0.01f;
            while (npolys > 0 && smoothPath.Count < smoothPath.Capacity)
            {
                //find location to steer towards
                SVector3 steerPos = new SVector3();
                StraightPathFlags steerPosFlag = 0;
                NavPolyId steerPosRef = NavPolyId.Null;

                if (!GetSteerTarget(navMeshQuery, iterPos, targetPos, SLOP, path, ref steerPos, ref steerPosFlag, ref steerPosRef))
                    break;

                bool endOfPath = (steerPosFlag & StraightPathFlags.End) != 0 ? true : false;
                bool offMeshConnection = (steerPosFlag & StraightPathFlags.OffMeshConnection) != 0 ? true : false;

                //find movement delta
                SVector3 delta = steerPos - iterPos;
                float len = (float)Math.Sqrt(SVector3.Dot(delta, delta));

                //if steer target is at end of path or off-mesh link
                //don't move past location
                if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
                    len = 1;
                else
                    len = STEP_SIZE / len;

                SVector3 moveTgt = new SVector3();
                VMad(ref moveTgt, iterPos, delta, len);

                //move
                SVector3 result = new SVector3();
                List<NavPolyId> visited = new List<NavPolyId>(16);
                NavPoint startPoint = new NavPoint(path[0], iterPos);
                navMeshQuery.MoveAlongSurface(ref startPoint, ref moveTgt, out result, visited);
                path.FixupCorridor(visited);
                npolys = path.Count;
                float h = 0;
                navMeshQuery.GetPolyHeight(path[0], result, ref h);
                result.Y = h;
                iterPos = result;

                //handle end of path when close enough
                if (endOfPath && InRange(iterPos, steerPos, SLOP, 1.0f))
                {
                    //reached end of path
                    iterPos = targetPos;
                    if (smoothPath.Count < smoothPath.Capacity)
                    {
                        smoothPath.Add(iterPos);
                    }
                    break;
                }

                //store results
                if (smoothPath.Count < smoothPath.Capacity)
                {
                    smoothPath.Add(iterPos);
                }
            }
        }
Ejemplo n.º 5
0
 private void LoadNavMeshFromFile(string path)
 {
     tiledNavMesh = new NavMeshJsonSerializer().Deserialize(path);
     displayMode = DisplayMode.NavMesh;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Serialize navigation mesh into external file
 /// </summary>
 /// <param name="path">path of file to serialize into</param>
 /// <param name="mesh">mesh to serialize</param>
 public abstract void Serialize(string path, TiledNavMesh mesh);
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NavMeshQuery"/> class.
 /// </summary>
 /// <param name="nav">The navigation mesh to query.</param>
 /// <param name="maxNodes">The maximum number of nodes that can be queued in a query.</param>
 public NavMeshQuery(TiledNavMesh nav, int maxNodes)
     : this(nav, maxNodes, new Random())
 {
 }
Ejemplo n.º 8
0
		private void LoadNavMeshFromFile(string path)
		{
			tiledNavMesh = new NavMeshJsonSerializer().Deserialize(path);
			navMeshQuery = new NavMeshQuery(tiledNavMesh, 2048);
			hasGenerated = true;
			displayMode = DisplayMode.NavMesh;
		}