首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏编程ai:翻墙找玩家?

游戏编程ai:翻墙找玩家?
EN

Stack Overflow用户
提问于 2012-11-09 01:47:33
回答 1查看 494关注 0票数 7

我研究这种人工智能方法已经有一段时间了。如果有一堵墙挡住了敌人通向玩家的道路,它基本上会为敌人可能去的每个方向设置一个int。这在大多数情况下是行不通的。有时敌人会穿过他们无法穿过的缝隙。其他时候,它会被贴在有明显缝隙的墙上。我会附上我的代码,但如果它看起来太低效或只是不是解决它的方法,我并不反对完全改变我的方法。我只是想知道这些事情是如何正常完成的,这样我就可以更好地实现它(并且工作!)道路。

我的代码:

代码语言:javascript
复制
    public void update(ArrayList<Wall> walls, Player p){

    findPlayer(p.getX(), p.getY());

    boolean isCollision = false;
    System.out.println(isCollision);
    //if movement straight towards the player is blocked, move along the walls
    for(Wall w : walls){
        if(Helper.isBoundingBoxCollision((int)(x + vectorToPlayer.getDX() * SPEED), (int)(y + vectorToPlayer.getDY() * SPEED), width, height, w.getX(), w.getY(), w.width, w.height)){
            isCollision = true;

            if(Math.abs(vectorToPlayer.getDX()) > Math.abs(vectorToPlayer.getDY())){
                if(vectorToPlayer.getDX() > 0)
                    WALL_COLLISION = 3;
                else
                    WALL_COLLISION = 1;
            }
            else if(Math.abs(vectorToPlayer.getDX()) <     Math.abs(vectorToPlayer.getDY())){
                if(vectorToPlayer.getDY() > 0)
                    WALL_COLLISION = 0;
                else
                    WALL_COLLISION = 2;
            }

        }
    }
    //System.out.println(isCollision);
    //set the direction to the straight on vector, to be reset if there is a collision on this path
    direction = vectorToPlayer;

    if(isCollision){
        //reset the variable, don't mind that what this is named is completely opposite = PIMPIN'
        isCollision = false;

        //scale dem walls son, and see when the path is clear
        for(Wall w : walls){
            if(WALL_COLLISION == 0 && !Helper.isBoundingBoxCollision(x + SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){
                WALL_COLLISION = 3;
                isCollision = true;
            }
            else if(WALL_COLLISION == 1 && !Helper.isBoundingBoxCollision(x, y + SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){
                WALL_COLLISION--;
                isCollision = true;
            }
            else if(WALL_COLLISION == 2 && !Helper.isBoundingBoxCollision(x - SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){
                WALL_COLLISION--;
                isCollision = true;
            }
            else if(WALL_COLLISION == 3 && !Helper.isBoundingBoxCollision(x, y - SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){
                WALL_COLLISION--;
                isCollision = true;
            }
        }

        //if there is NOT a wall on the designated side, set the vector accoridingly
        if(isCollision){
            if(WALL_COLLISION == 0)
                direction = new NVector(0, 1);
            else if(WALL_COLLISION == 1)
                direction = new NVector(1, 0);
            else if(WALL_COLLISION == 2)
                direction = new NVector(0, -1);
            else if(WALL_COLLISION == 3)
                direction = new NVector(-1, 0);
        }
    }
    x += Math.round(direction.getDX()*SPEED);
    y += Math.round(direction.getDY()*SPEED);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-09 01:53:17

看起来你目前正在尝试实现的东西被称为转向,但通常这些事情的实现方式应该是Pathfinding。您决定使用哪一个取决于您的应用程序。转向是向你的目标移动,但如果有障碍物,就改变方向,而且不能保证到达目的地。寻路通常是通过构建一个“可步行”的路点或区域的图形,然后使用an algorithm such as Dijkstra's遍历它来完成的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13294752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档