Game programming ai: scaling walls to find a player?
Asked Answered
D

1

7

I have been working on this artificial intelligence method for a while. It basically has an int for each direction the enemy could go if a wall were blocking his path to the player. This doesn't work in most cases. Sometimes the enemy will go through cracks it can't fit through. Other times it will be stuck on walls that have obvious gaps in them. I will attach my code, but if it looks too inefficient or just not the way to solve it I'm not opposed to changing my approach altogether. I just would like to know how these things are done normally, so that I can implement it in a better (and working!) way.

My code:

    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);
}
Deandre answered 8/11, 2012 at 17:47 Comment(1)
As Eric B says, Steering with collision detection is not guaranteed to reach the destination. The problem with moving to a Pathfinding algorithm as suggested will be less on implementing one (the "A*" algorithm is documented many places) but in making your world discrete so such an algorithm will work. From your isBoundingBoxCollision() calls I suspect you are trying to leverage the engine itself to find walls. Many games augment the level geometry with a movement mesh that is easy to query for the movement system to avoid the complexity of parsing the geometry.Buffalo
H
3

It appears that what you are currently trying to implement is known as Steering, but the way these things are normally done would be Pathfinding. Which you decide to use depends on your application. Steering is done by moving toward your target but changing direction if there is an obstacle, and it is not guaranteed to reach its destination. Pathfinding is usually done by constructing a graph of waypoints or areas that are "walkable" and then using an algorithm such as Dijkstra's to traverse it.

Hick answered 8/11, 2012 at 17:53 Comment(2)
A good reference is Programming Game AI by example, by Mat Buckland. It has very good examples, working code and covers State Machines, Steering Behaviors, Path Finding and many more.Skiascope
Thanks Eric B, I'll look into implementing a better system while still maintaining that kind of "dumb" feeling from the ai.Deandre

© 2022 - 2024 — McMap. All rights reserved.