[Libgdx] Astar Pathfinding

Hello, i’ve been trying to implement pathfinding for monsters in my game. But yet i still fail to do it.

    public void onWalk(float deltaTime) {
        if (toPosition.equals(position)) {
            toPosition.set(getNextPosition(direction, position));
            return;
        }

        if (position.dst(toPosition) <= 0.1f) {
            position.set(toPosition);
            isMoving = false;
            return;
        }

        switch (direction) {
            case NORTH:
                position.y += speed * deltaTime;
                break;
            case EAST:
                position.x += speed * deltaTime;
                break;
            case SOUTH:
                position.y -= speed * deltaTime;
                break;
            case WEST:
                position.x -= speed * deltaTime;
                break;
            default:
                break;
        }
    }

    public Vector2 getNextPosition(Direction dir, Vector2 pos) {
        pos = new Vector2(pos.x, pos.y);
        switch (dir) {
            case NORTH:
                pos.y++;
                break;
            case EAST:
                pos.x++;
                break;
            case SOUTH:
                pos.y--;
                break;
            case WEST:
                pos.x--;
                break;
            default:
                break;
        }

        if (!world.isValidTile(pos)) {
	    isMoving = false;
            return position;
        }

        return pos;
    }
    public void onThink(float deltaTime) {
        lastWalkTime += deltaTime;
        if (lastWalkTime >= 1f) {
            followCreature(deltaTime);
            lastWalkTime = 0f;
        }
    }

    private void followCreature(float deltaTime) {
        if (attackedCreature == null) {
            return;
        }

        IntArray path = astar.getPath((int) position.x, (int) position.y, (int) attackedCreature.position.x, (int) attackedCreature.position.y);
        for (int i = 0, n = path.size; i < n; i += 2) {
            int x = path.get(i);
            int y = path.get(i + 1);
            
            position.set(x, y);
        }
    }

Now it looks like the monster is jumping one tile of the time, until it lands on the target. I would like it to move as a grid walking towards the target.

I use this: https://gist.github.com/NathanSweet/7587981

are they following the correct path, or intended path?

Is your grid perhaps 1:1 with your tile size?

Well i was able to make it now following me naturally with this code:

    private Vector2 targetPosition = new Vector2();
    private Vector2 fromPos = new Vector2();
    private Vector2 tmpPos = new Vector2();
    private float alpha = 0f;

    private void followCreature(float deltaTime) {
        if (targetPosition.isZero()) {
            fromPos.set(position);
            IntArray path = astar.getPath((int) position.x, (int) position.y, (int) attackedCreature.position.x, (int) attackedCreature.position.y);
            for (int i = 0, n = path.size; i < n; i += 2) {
                int x = path.get(i);
                int y = path.get(i + 1);
                targetPosition.set(x, y);
            }

            direction = getDirectionTo(position, targetPosition);
            rectangle.setPosition(targetPosition);
            return;
        }

        alpha += MathUtils.clamp(deltaTime * speed, 0f, 1f);
        if (alpha >= 1f) {
            position.set(targetPosition);
            targetPosition.setZero();
            alpha = 0f;
            stateTime = 0f;
            return;
        }

        tmpPos.set(fromPos).lerp(targetPosition, alpha);
        position.set(tmpPos);
        stateTime += deltaTime;
    }

But i have now two issues

  1. Since player is a collideable object, it seems it ignores following the player.

  2. The way i change direction, maybe is not the best way?

Ummm… The way I would do it, pathfinder would only be concerned with the terrain, not what’s on the terrain.

That, or have them target a place near the target, near enough to be useful.

Directions, do whatever works…

What do you mean? Since i store objects and creatures on the same quadTree

Um… Ive not used quad tree before.

Are you using your quad tree to define your Astar grid? I’m just not sure how things are structured to suggest something in that direction.

Anyway, if you can’t target the entity directly, have each entity draw a circle 1 min distance radius, then draw a line to that circle and the intersection cell can be the target.

No i use the quad tree to store the entities and check for collision. I found hackyway to fix the issue, i’m now sending a boolean to the collision check function, if its pathfinding check and not normal walking check. Then i stop the monster to walk into the player by checking the distance.

But should it not be possible to make pathfinding be 1 tile away from the target position, instead of pathfinding take you exactly to the target position.