Example #1
0
        public static void run(String[] args)
        {
            Logger log = new Logger();
            Stopwatch s = new Stopwatch();

            log.addToLog("Map initializing...");
            AStarMap map = new AStarMap(mapData.getMapWidth(), mapData.getMapHeight(), mapData.getObstacleMap());

            log.addToLog("Heuristic initializing...");
            //AStarHeuristic heuristic = new ClosestHeuristic();
            AStarHeuristic heuristic = new DiagonalHeuristic();

            log.addToLog("AStar initializing...");
            AStar aStar = new AStar(map, heuristic);

            log.addToLog("Calculating shortest path...");
            s.Start();
            List<Point> shortestPath = aStar.calcShortestPath(startX, startY, goalX, goalY);
            s.Stop();

            log.addToLog("Time to calculate path in milliseconds: " + s.ElapsedMilliseconds);

            log.addToLog("Printing map of shortest path...");
            new PrintMap(map, shortestPath);
        }
Example #2
0
        public static void run(String[] args)
        {
            Logger    log = new Logger();
            Stopwatch s   = new Stopwatch();

            log.addToLog("Map initializing...");
            AStarMap map = new AStarMap(mapData.getMapWidth(), mapData.getMapHeight(), mapData.getObstacleMap());

            log.addToLog("Heuristic initializing...");
            //AStarHeuristic heuristic = new ClosestHeuristic();
            AStarHeuristic heuristic = new DiagonalHeuristic();

            log.addToLog("AStar initializing...");
            AStar aStar = new AStar(map, heuristic);

            log.addToLog("Calculating shortest path...");
            s.Start();
            List <Point> shortestPath = aStar.calcShortestPath(startX, startY, goalX, goalY);

            s.Stop();

            log.addToLog("Time to calculate path in milliseconds: " + s.ElapsedMilliseconds);

            log.addToLog("Printing map of shortest path...");
            new PrintMap(map, shortestPath);
        }
        public List<Point> findStraightPath(Point start, Point goal)
        {
            log.addToLog("AStar Heuristic initializing...");
            AStarHeuristic heuristic = new DiagonalHeuristic();

            log.addToLog("AStar initializing...");
            AStar aStar = new AStar(map, heuristic);

            log.addToLog("Calculating shortest path with AStar...");
            List<Point> shortestPath = aStar.calcShortestPath(start.x, start.y, goal.x, goal.y);

            //log.addToLog("Printing map of shortest path...");
            //new PrintMap(map, shortestPath);

            log.addToLog("Calculating optimized waypoints...");
            s.Start();
            List<Point> waypoints = calcStraightPath(shortestPath);
            s.Stop();
            log.addToLog("Time to calculate waypoints: " + s.ElapsedMilliseconds + " ms");

            return waypoints;
        }
        public List <Point> findStraightPath(Point start, Point goal)
        {
            log.addToLog("AStar Heuristic initializing...");
            AStarHeuristic heuristic = new DiagonalHeuristic();

            log.addToLog("AStar initializing...");
            AStar aStar = new AStar(map, heuristic);

            log.addToLog("Calculating shortest path with AStar...");
            List <Point> shortestPath = aStar.calcShortestPath(start.x, start.y, goal.x, goal.y);

            //log.addToLog("Printing map of shortest path...");
            //new PrintMap(map, shortestPath);

            log.addToLog("Calculating optimized waypoints...");
            s.Start();
            List <Point> waypoints = calcStraightPath(shortestPath);

            s.Stop();
            log.addToLog("Time to calculate waypoints: " + s.ElapsedMilliseconds + " ms");

            return(waypoints);
        }
Example #5
0
        public List <Vector2> findStraightPath(Vector2 start, Vector2 goal, float cellSize)
        {
            Point startCell = getCellWithErrorTolerate(start.x, start.y);

            // 起点不可达
            if (startCell == null)
            {
                return(null);
            }
            Point goalCell = posToCell(goal, cellSize);

            // optimized, check can straight pass
            Point hitPoint = raycast(start, goal, cellSize);

            if (hitPoint.Equals(goalCell))
            {
                List <Vector2> waypoints = new List <Vector2>();
                waypoints.Add(start);
                waypoints.Add(goal);
                return(waypoints);
            }

            log.addToLog("AStar Heuristic initializing...");
            AStarHeuristic heuristic = new DiagonalHeuristic();

            log.addToLog("AStar initializing...");
            AStar aStar = new AStar(map, heuristic);

            log.addToLog("Calculating shortest path with AStar...");
            List <Point> shortestPath = aStar.calcShortestPath(startCell.x, startCell.y, goalCell.x, goalCell.y);

            // 终点不可达
            if (shortestPath == null || shortestPath.Count == 0)
            {
                return(null);
            }

            List <Vector2> posPath = new List <Vector2>();

            posPath.Add(start);

            // 起点和终点在同一个cell
            if (shortestPath.Count == 1)
            {
                posPath.Add(goal);
                return(posPath);
            }
            else
            {
                float   fixSize = map.getErrorTolerate();
                Vector2 pos1    = cellToPos(shortestPath[0], cellSize);
                for (int i = 1; i < shortestPath.Count; ++i)
                {
                    Vector2 pos2 = cellToPos(shortestPath[i], cellSize);
                    Vector2 dir  = (pos2 - pos1).normalized;
                    float   midx = (pos1.x + pos2.x) / 2;
                    float   midy = (pos1.y + pos2.y) / 2;
                    posPath.Add(new Vector2(midx - fixSize * dir.x, midy - fixSize * dir.y));
                    posPath.Add(new Vector2(midx + fixSize * dir.x, midy + fixSize * dir.y));

                    pos1 = pos2;
                }

                posPath.Add(goal);
            }

            log.addToLog("Calculating optimized waypoints...");
            s.Start();
            posPath = calcStraightPath(posPath, cellSize);
            s.Stop();
            log.addToLog("Time to calculate waypoints: " + s.ElapsedMilliseconds + " ms");

            return(posPath);
        }