【问题标题】:Game programming ai: scaling walls to find a player?游戏编程ai:爬墙找玩家?
【发布时间】:2012-10-28 23:40:43
【问题描述】:

我一直在研究这种人工智能方法。它基本上有一个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);
}

【问题讨论】:

  • 正如 Eric B 所说,带碰撞检测的转向不能保证到达目的地。按照建议转移到寻路算法的问题在实现一个(“A *”算法在许多地方都有记录)时会更少,但在使你的世界离散以便这样的算法将起作用。从您的 isBoundingBoxCollision() 调用中,我怀疑您正试图利用引擎本身来寻找墙壁。许多游戏使用易于查询运动系统的运动网格来增加关卡几何图形,以避免解析几何图形的复杂性。
  • 好的,感谢您的澄清

标签: java game-ai


【解决方案1】:

您当前尝试实施的似乎称为 Steering,但通常执行这些操作的方式是 Pathfinding。您决定使用哪个取决于您的应用程序。转向是通过向目标移动但如果有障碍物改变方向来完成的,并且不能保证到达目的地。寻路通常是通过构建一个“可步行”的路点或区域图,然后使用an algorithm such as Dijkstra's 遍历它来完成的。

【讨论】:

  • 一个很好的参考是 Programming Game AI by example,作者是 Mat Buckland。它有非常好的示例、工作代码,涵盖状态机、转向行为、路径查找等等。
  • 感谢 Eric B,我会考虑实施一个更好的系统,同时仍然保持人工智能的那种“愚蠢”感觉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多