Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do i make enemy move by found shortest path?  (Read 3020 times)

0 Members and 1 Guest are viewing this topic.

sensus12

  • Newbie
  • *
  • Posts: 6
    • View Profile
How do i make enemy move by found shortest path?
« on: December 22, 2014, 11:53:02 am »
Hello, i made a BFS algorithm, it finds shortest path. My map is represented as TileMap from Vertex tutorial.
So when i have path, how do i make someone move by it? My tilemap is 1d array.
My BFS code is (don't know if u actually need this):
void GraphFinder::shortestPath(int s, int e)
{
    std::queue<int> q;
        int v;
        bool found = false;
        bool *visited = new bool[414]; //414 is amount of tiles
        int *path = new int[414]; //414 is amount of tiles

        for(int i = 0; i < 414; ++i) //414 is amount of tiles
                visited[i] = false;

        path[s] = -1;

        q.push(s);
        visited[s] = true;

        while(!q.empty())
        {
                v = q.front();
                q.pop();
                if(v == e)
                {
                        found = true;
                        break;
                }
                for(auto i = adjList[v].begin(); i != adjList[v].end(); ++i)
                {
                        int u = *i;
                        if(!visited[u])
                        {
                                path[u] = v;
                                q.push(u);
                                visited[u] = true;
                        }
                }
        }
        if(!found) std::cout << "Way not found" << std::endl;
                else
        while(v > -1)
        {
                std::cout << v << std::endl;
                v = path[v];
        }
        while(!q.empty()) q.pop();
        delete [] path;
        delete [] visited;
}
 
I'm using SFML 2.1

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How do i make enemy move by found shortest path?
« Reply #1 on: December 22, 2014, 12:34:34 pm »
First, don't use raw new/delete without a very good reason.  In this case, just a statically-allocated array is fine (ie, just bool[414] visited; no pointers or anything).

Now, this is basically asking us to write your code for you.  I assume the conceptual problem here is that this BFS algorithm is executed once, while the movement has to be done bit-by-bit over the course of several frames.  To help build that fundamental understanding of how you write code that gets executed bit-by-bit over several frames, I would start by writing something much simpler.  For instance, make a circle that moves toward the mouse cursor whenever the left mouse button is held down.  The official tutorials should be more than enough to figure this out.

Once you know the basics, the high-level architectural stuff you'll need to know are covered in:
http://gameprogrammingpatterns.com/game-loop.html
http://gameprogrammingpatterns.com/update-method.html

By the way, the direct answer to your question is "you give that someone an update method that makes it move one step along the path."

sensus12

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How do i make enemy move by found shortest path?
« Reply #2 on: December 22, 2014, 01:09:54 pm »
You didn't understand m. I'm not asking for code, BFS searches through the map and finds shortest path. I know how to make circles/sprites move, but i don't know how to make a sprite move from tile 285->284->283.. and so on to some point. I'm asking for tips, not for code.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How do i make enemy move by found shortest path?
« Reply #3 on: December 22, 2014, 01:18:05 pm »
I know how to make circles/sprites move, but i don't know how to make a sprite move from tile 285->284->283.. and so on to some point. I'm asking for tips, not for code.

I'm not really sure what we could tell you aside from what was in my last post: You give the entity an update() method that moves it some distance along the path, and ensure it gets called once every frame.  Presumably this entity would store the current path as one of its members.  If you can already do basic movement, and you know where each numbered tile is on the screen, there's really nothing else to it.

If you need help filling in one of the details, you have to explain to us where exactly you're stuck.

sensus12

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How do i make enemy move by found shortest path?
« Reply #4 on: December 22, 2014, 02:25:38 pm »
Yeah, my english isn't that good to communicate so it's hard for me to ask good questions.
My main problem is/was that, i needed to have a position of each tile from bfs algorithm. In vertex array tutorials there is TileMap example it has "width" and "height" of array. When i have my path i need to get position of each tile, but i didn't have an idea how.
I am thinking about doing something like:

actuallTileNumber/width * tileSize.x = position.x and
actuallTileNumber/height * tileSize.y = position.y
then i just move sprite to the certain position, and get next tile.

I'm not really sure if it is correct and not sure if it would work, i can't test it now.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How do i make enemy move by found shortest path?
« Reply #5 on: December 22, 2014, 07:55:38 pm »
The general idea is right, but that's definitely not correct.

I can't tell you what the exact correct code for this is without knowing exactly how you've implemented your tilemap, but right now it sounds like you're giving each tile a random number that doesn't actually help you identify where it is.  Shouldn't each tile be identified by something useful like its x/y position in the tilemap?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do i make enemy move by found shortest path?
« Reply #6 on: December 22, 2014, 08:01:51 pm »
If the tiles are organised in the same way as in the Vertex Array tutorial then the tutorial itself already has the code you need. It's something similar to:
x = (tileNumber % numberOfTilesPerRow) * tileSize.x;
y = (tileNumber / numberOfTilesPerRow) * tileSize.y;
and is common to use a one-dimenstion array as a two-dimensional one.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

grok

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • Email
Re: How do i make enemy move by found shortest path?
« Reply #7 on: December 24, 2014, 02:03:49 pm »
a bit offtopic, but this line
    while(!q.empty()) q.pop();
 
is needless.

and the function is dangerous because of:
path[s] = -1;
 

It would be better to add an assertion that s  >=0 && s <= 414, or perform an explicit checking.
If someone invoke this function, passing "s", say, 415, it will crash :)

back to the topic:
your implementation solves the problem:
"is it possible to get to the destination point/place from the start point?"
so, it is a boolean function (I don't take into account that your function returns void, let's assume it returns a boolean value).
I don't believe this is exactly what you try to achieve, you are rather interested in "is it possible to get to the destination point/place from the start point? and if yes, then give me the list/array of the steps one should perform to achieve that".
In other words, it should return some kind of std::vector<Coordinate/Point/Ints/Tiles Indexes/etc> as a result.
Hope that helps.
« Last Edit: December 24, 2014, 02:21:59 pm by grok »