简体   繁体   中英

pathfinding in an 2D Array

I know this is an question that get asked alot. I also stuck at it and looking for some help with it.

I do have a small Applikation where Monster should work to the Charakter. Its Gridbased so it's just possible to walk left right up down. I do have an Array where all blockd areas of the map are. All i need to get is the next step to get to the Character(left right up down of it). It would be great if they walk around trees for example. (simple -1 inside of the array)

Is there any simple solution for it or do i need to implement a A*(i tried but i totaly stuck at this)

I simply stared with this:

@Override
public Status getNextMove(int posX, int posY) {
    if (checkIfAggroRange(posX, posY)) {
        if (checkIfBeside(posX, posY))
            //turn to the character
            return getIdleStatus(posX, posY);
        else
            //here id like to add the algo and get the value
    } else {
        return moveRnd();
    }
}

private boolean checkIfAggroRange(int posX, int posY) {
    return Math.abs(this.screen.character.mapPos.x - posX) <= range
            && Math.abs(this.screen.character.mapPos.y - posY) <= range;
}

private boolean checkIfBeside(int posX, int posY) {
    return (Math.abs(this.screen.character.mapPos.x - posX) <= 1 && Math
            .abs(this.screen.character.mapPos.y - posY) <= 1);
}

It does already start aggro when they are in range of the monster and also turns the monster to the character so it can hit the character.

I do get the map simple by screen.map.maparray (int[xsize][ysize]) . xpos/ypos are the pos inside of the array.

If you need more information about it please let me know.

I suggest using A*, it uses simple heuristics to give you a path from a to b. As its a grid you can just use x,y coordinates which is then quite easy to implement A* with. So what i suggest doing is reading these 2,

http://wiki.gamegardens.com/Path_Finding_Tutorial

http://www.cokeandcode.com/main/tutorials/path-finding/

This should explain to you how A* works.The first article is well structured and you should definitely full read and try to understand before attempting to implement the algorithm. it will also give you ideas on tackling obstacles such as trees in your case

The second one is great, as the A* algorithm implemented is done well and has comments explaining all of it. It is a little over complicated and can be done with just 2-3 classes rather than the amount shown but it will certainly give you an idea of how everything works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM