美文网首页libGdx专题
寻找路径的处理思路

寻找路径的处理思路

作者: 大旺旺的弟弟小旺旺 | 来源:发表于2021-09-07 06:17 被阅读0次

以吃豆人为例,寻找出距离自己最近的吃豆人,然后鬼走向它,并把它吃掉。我们可以使用AStar进行寻找。每次只找出一个节点的位置,如果找不到那么就不在去寻找。而采取随机的方式寻找、
1.创建节点

    public Node(AStarMap map, int x, int y) {
        this.x = x;
        this.y = y;
        this.index = x * map.getHeight() + y;
        this.isWall = false;
        this.connections = new Array<Connection<Node>>();
    }

顶点先得到,根据坐标的长和宽将坐标编号。这个时候只是创建节点,对于里面是什么还没有做处理。
2.标记所有的墙

QueryCallback queryCallback = new QueryCallback() {
            @Override
            public boolean reportFixture(Fixture fixture) {
                wall = fixture.getFilterData().categoryBits == GameManager.WALL_BIT;
                return false; // stop finding other fixtures in the query area
            }
        };

        for (int y = 0; y < mapHeight; y++) {
            for (int x = 0; x < mapWidth; x++) {
                wall = false;
                world.QueryAABB(queryCallback, x + 0.2f, y + 0.2f, x + 0.8f, y + 0.8f);
                if (wall) {
                   aStarMap.getNodeAt(x, y).isWall = true;
                }
            }
        }

3.然后生成地图。
首先里面有墙,有路径,我们不管多余的,只管墙和路径,我们遍历所有的节点,然后检测是不是墙,是墙的就跳过,然后找出节点的上下左右,我的邻居就是什么东西,如果不是墙,我们就可以进行连接。

    public static MyGraph createGraph (AStarMap map) {
        final int height = map.getHeight();
        final int width = map.getWidth();
        MyGraph graph = new MyGraph(map);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Node node = map.getNodeAt(x, y);
                if (node.isWall) {
                    continue;
                } 
                // Add a connection for each valid neighbor
                for (int offset = 0; offset < NEIGHBORHOOD.length; offset++) {
                    int neighborX = node.x + NEIGHBORHOOD[offset][0];
                    int neighborY = node.y + NEIGHBORHOOD[offset][1];
                    if (neighborX >= 0 && neighborX < width && neighborY >= 0 && neighborY < height) {
                        Node neighbor = map.getNodeAt(neighborX, neighborY);
                        if (!neighbor.isWall) {
                            // Add connection to walkable neighbor
                            node.getConnections().add(new DefaultConnection<Node>(node, neighbor));
                        }
                    }
                }
                node.getConnections().shuffle();
            }
        }
        return graph;
    }

4.将可以连通的图给AStar寻路器

        this.pathFinder = new IndexedAStarPathFinder<MyNode>(createGraph(myAStarMap));
        this.connectionPath = new DefaultGraphPath<>();
        this.heuristic = new Heuristic<MyNode>() {
            @Override
            public float estimate(MyNode node, MyNode endNode) {
                return Math.abs(endNode.x - node.x)+Math.abs(endNode.y - node.y);
            }
        };

5.寻找路径

 public MyNode findNextNode(Vector2 source,Vector2 target){
        int sourceX = MathUtils.floor(source.x);
        int sourceY = MathUtils.floor(source.y);
        int targetX = MathUtils.floor(target.x);
        int targetY = MathUtils.floor(target.y);

        if (myAStarMap == null
                || sourceX < 0 || sourceX >= myAStarMap.getWidth()
                || sourceY < 0 || sourceY >= myAStarMap.getHeight()
                || targetX < 0 || targetX >= myAStarMap.getWidth()
                || targetY < 0 || targetY >= myAStarMap.getHeight()) {
            return null;
        }

        MyNode sourceNode = myAStarMap.getNodeAt(sourceX, sourceY);
        MyNode targetNode = myAStarMap.getNodeAt(targetX, targetY);
        connectionPath.clear();
        pathFinder.searchConnectionPath(sourceNode, targetNode, heuristic, connectionPath);
        return connectionPath.getCount() == 0?null : connectionPath.get(0).getToNode();

    }

开始节点 结束节点 让去找出目标路径,每次只找一个节点。

相关文章

  • 寻找路径的处理思路

    以吃豆人为例,寻找出距离自己最近的吃豆人,然后鬼走向它,并把它吃掉。我们可以使用AStar进行寻找。每次只找出一个...

  • PAT 甲级 刷题日记|A 1131 Subway Map (

    conjunction 结合 连接 同时发生 思路 这是一道典型的搜索类型的题目,处理起来比较麻烦。 寻找路径...

  • LeetCode-Simplify Path

    题目要求:路径简化Example: 最主要的是边界情况的处理:处理思路:利用vector存储每个有效路径单元如"/...

  • 学习日记-07-关于 广度优先搜索

    广度优先搜索(breadth-first search)处理是否有A到B的路径,如果有最短路径是什么。 思路:建立...

  • 处理问题的路径和思路

    我们每天都会遇到一些难办的事情,处理事情可以遵循以下路径:正因为事情难办,所以我们才有存在的价值,尽量保持好积极的...

  • cocos creator动画编辑器编辑地图路径

    思路 1、利用动画编辑器,设置一个路径,多个路径就编辑多个动画 2、用特定的代码对动画进行处理,获取到路径坐标,大...

  • 113.Path Sum II

    题目 解法:回溯 思路:用helper来维护相应的path路径,与112题目的DFS问题思路类似,分四步处理。注意...

  • 五. 二叉树 1 Lowest Common Ancestor

    思路: 自上而下分别找出A,B的路径,然后寻找最短相同部分,最后一个node就是LCA。(未经测试) 自下而上A,...

  • python视频分离音频,同时简单分轨

    首先,安装相应的音视频处理库: 然后,导入库,并读取相应的视频文件,将音频导出:(路径修改为自己的路径) 主要思路...

  • 437. 路径总和 III

    一 题目: 二 思路: 递归:每个结点作为开始结点向下寻找和为target的路径 注意: 1.结点本身可能就值为t...

网友评论

    本文标题:寻找路径的处理思路

    本文链接:https://www.haomeiwen.com/subject/ufcewltx.html