Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (748)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (834)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Libgdx] Astar Pathfinding  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Offline printer

Junior Devvie


Medals: 1



« Posted 2018-01-30 23:05:21 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
    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;
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
    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
Offline bmanmcfly
« Reply #1 - Posted 2018-02-01 05:50:17 »

are they following the correct path, or intended path?

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

Junior Devvie


Medals: 1



« Reply #2 - Posted 2018-02-01 09:36:00 »

Well i was able to make it now following me naturally with this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
    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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bmanmcfly
« Reply #3 - Posted 2018-02-02 05:53:22 »

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...
Offline printer

Junior Devvie


Medals: 1



« Reply #4 - Posted 2018-02-05 15:51:46 »

What do you mean? Since i store objects and creatures on the same quadTree
Offline bmanmcfly
« Reply #5 - Posted 2018-02-06 03:36:18 »

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. 
Offline printer

Junior Devvie


Medals: 1



« Reply #6 - Posted 2018-02-07 09:10:10 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (316 views)
2017-12-31 17:17:51

xxMrPHDxx (97 views)
2017-12-31 17:15:51

xxMrPHDxx (148 views)
2017-12-28 18:11:33

Ecumene (320 views)
2017-09-30 02:57:34

theagentd (494 views)
2017-09-26 18:23:31

cybrmynd (531 views)
2017-08-02 12:28:51

cybrmynd (471 views)
2017-08-02 12:19:43

cybrmynd (449 views)
2017-08-02 12:18:09

Sralse (408 views)
2017-07-25 17:13:48

Archive (1315 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!