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

Author Topic: Isometric Maps Pathfinding  (Read 3018 times)

0 Members and 1 Guest are viewing this topic.

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Isometric Maps Pathfinding
« on: September 04, 2016, 12:30:22 am »
Hello, I have to found the neighbours tiles of my current tiles for implementing A* pathfinding system to my game.

I use this for now :
for (int x = 1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if ((x == 0 && y == 0)
                    || (x == 1 && y == 0)
                    || (x == -1 && y == 0))
                    continue;
               
                int checkX = mapTile->getPositionOnMap().x + x;
                int checkY = mapTile->getPositionOnMap().y + y;
               
                if (checkX >= 0 && checkX < mWidth && checkY >= 0 && checkY < mHeight)
                {
                    neighbours.push_back(&mMap[checkY][checkX]);
                }
            }
        }
    }

 

But isn't good... i want to put the right 8 tiles around on the list but i can"t figure it out...

Screen of ID's & Positions on the map for help, thanks


lapinozz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Isometric Maps Pathfinding
« Reply #1 on: September 04, 2016, 12:55:47 am »
First I think your first for loop should be

for (int x = -1; x <= 1; x++)

And then the loops go over the 9 tiles but you only want to skip one, the center one so 0,0

if (x == 0 && y == 0)
    continue;
 
« Last Edit: September 04, 2016, 12:59:13 am by lapinozz »

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Isometric Maps Pathfinding
« Reply #2 on: September 04, 2016, 01:00:41 am »
Thanks to you but no, look at the map, for even or odd row it's different.. sometimes you have to start a x = 0 or x = -1 and after i'm loosing myself x')

lapinozz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Isometric Maps Pathfinding
« Reply #3 on: September 04, 2016, 01:19:55 am »
You could use one array of offset for the odd pos and another for the normal one:

const sf::Vector2i oddOffset[] = {
                                  {-1, 0},
                                  {-1, -1}, {-1, 1},
                                  {-2, 0}, {2, 0},
                                  {1, -1}, {1, 1},
                                  {1, 0}
                                 }
                                 
const sf::Vector2i normalOffset[] = {
                                     {0, 0},
                                     {0, -1}, {0, 1},
                                     {-2, 0}, {2, 0},
                                     {1, -1}, {1, 1},
                                     {1, 0}
                                    }

const bool isOdd = mapTile->getPositionOnMap().y & 1;


for(const auto& pos : isOdd ? oddOffset : normalOffset)
{
    int checkX = mapTile->getPositionOnMap().x + pos.x;
    int checkY = mapTile->getPositionOnMap().y + pos.y;

    if (checkX >= 0 && checkX < mWidth && checkY >= 0 && checkY < mHeight)
    {
        neighbours.push_back(&mMap[checkY][checkX]);
    }
}
 

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Isometric Maps Pathfinding
« Reply #4 on: September 04, 2016, 01:36:08 am »
Oh dude you are really really really great!!! I just change the offsets array like this but now it's working perfectly! :

const sf::Vector2i evenOffset[] = {
        {-1, 0},
        {-1, -1}, {-1, 1},
        {0, -2}, {0, 2},
        {0, -1}, {0, 1},
        {1, 0}
    };
   
    const sf::Vector2i oddOffset[] = {
        {-1, 0},
        {0, -1}, {0, 1},
        {0, -2}, {0, 2},
        {1, -1}, {1, 1},
        {1, 0}
    };
 

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Isometric Maps Pathfinding
« Reply #5 on: September 04, 2016, 10:32:40 am »
I have another issuees :/

in the main loop, i Done that for the player follow the path found by the pathfinding:

if (tileClicked != nullptr && tileClicked->isWalkable)
            p.followPath(pf.path);
 

but my player move only on the first elements of the path and I don't know why...

PS:
void Player::followPath(std::vector<MapTile*> path)
{
    mapTile = path.front();
   
    if (path.front() != path.back())
    {
        //std::cout << path.front()->getId() << " -> ";
        float px = mapTile->mConvex.getPosition().x;
        float py = mapTile->mConvex.getPosition().y;
        float ex = mTile.mConvex.getPosition().x;
        float ey = mTile.mConvex.getPosition().y;
        float x = (ex - px);
        float y = (ey - py);
        float v = sqrtf((x * x) + (y * y));
        float unitx = (x / v);
        float unity = (y / v);
        float movex = -mMovementSpeed * unitx;
        float movey = -mMovementSpeed / 2 * unity;
       
        if (mTile.getPositionInWindow() != mapTile->getPositionInWindow())
        {
            isMoving = true;
           
            if (movex < 0)
            {
                if (movey < 0)
                    move(UpLeft);
                else if (movey > 0)
                    move(DownLeft);
                else if (movey == 0)
                    move(Left);
            }
            else if (movex > 0)
            {
                if (movey < 0)
                    move(UpRight);
                else if (movey > 0)
                    move(DownRight);
                else if (movey == 0)
                    move(Right);
            }
            else if (movex == 0)
            {
                if (movey < 0)
                    move(Up);
                else if (movey > 0)
                    move(Down);
            }
        }
        else
        {
            //std::cout << mapTile->getId() << std::endl;
            path.erase(path.begin());
            //std::cout << path.front()->getId() << std::endl;
        }
    }
    else
    {
        counterAnimation = 0;
        isMoving = false;
    }
   
}