游戏编程ai:缩放墙以找到玩家?

我一直在研究这种人工智能方法。 它基本上有一个敌人可以去的每个方向的一个int ,如果一堵墙挡住了他的路径。 这在大多数情况下不起作用。 有时敌人会穿过它无法穿透的裂缝。 其他时候它会粘在有明显间隙的墙上。 我会附上我的代码,但如果它看起来效率太低或者没有解决方法,我并不反对完全改变我的方法。 我只是想知道这些事情是如何正常完成的,以便我能够以更好的方式实现它(并且工作!)。

我的代码:

    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);
}

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

链接地址: http://www.djcxy.com/p/11411.html

上一篇: Game programming ai: scaling walls to find a player?

下一篇: Build native windows apps with HTML