-
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
(http://image.noelshack.com/fichiers/2016/35/1472941710-screen-shot-2016-09-04-at-12-27-48-am.png)
-
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;
-
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')
-
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]);
}
}
-
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}
};
-
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;
}
}