I have displacement mapped a cube into a sphere, as illustrated in the photo:
I then use the center of each square as a node in this grid. My issue is I am trying to calculate the nodes that are adjacent to each other (for use in A*). Currently, all my nodes are stored in a one-dimensional array that can be visualised in a 2D net as so:
I have no idea how to determine the adjacent nodes on the edges and corners. I can detect them with a massive conditional, like this:
pos is the index of the node that I am trying to find the neighbors of, and width is the width of one side of the cube before it is mapped into a sphere.
if (
pos == 0 || pos == width - 1 || pos == (width * width) - 1 || pos == width * (width - 1) ||//bottom
pos == (width * width) || pos == (width * width) + (width - 1) || pos == (width * width) + ((width * width) - 1) || pos == (width * width) + (width * (width - 1)) ||//top
pos == (2 * (width * width)) || pos == (2 * (width * width)) + (width - 1) || pos == (2 * (width * width)) + ((width * width) - 1) || pos == (2 * (width * width)) + (width * (width - 1)) ||//front
pos == (3 * (width * width)) || pos == (3 * (width * width)) + (width - 1) || pos == (3 * (width * width)) + ((width * width) - 1) || pos == (3 * (width * width)) + (width * (width - 1)) ||//back
pos == (4 * (width * width)) || pos == (4 * (width * width)) + (width - 1) || pos == (4 * (width * width)) + ((width * width) - 1) || pos == (4 * (width * width)) + (width * (width - 1)) ||//right
pos == (5 * (width * width)) || pos == (5 * (width * width)) + (width - 1) || pos == (5 * (width * width)) + ((width * width) - 1) || pos == (5 * (width * width)) + (width * (width - 1)) //left
)
//CORNERS
{
//corner code here...
}
else if (
(pos < width - 1 && pos > 0) || (pos < (width * width) - 1 && pos > width * (width - 1)) || (pos % width == 0) || ((pos + 1) % width == 0) || //bottom
(pos < (width * width) + (width - 1) && pos > (width * width)) || (pos < (width * width) + ((width * width) - 1) && pos > (width * width) + (width * (width - 1))) ||//top
(pos < (2 * (width * width)) + (width - 1) && pos > (2 * (width * width))) || (pos < (2 * (width * width)) + ((width * width) - 1) && pos > (2 * (width * width)) + (width * (width - 1))) ||//front
(pos < (3 * (width * width)) + (width - 1) && pos > (3 * (width * width))) || (pos < (3 * (width * width)) + ((width * width) - 1) && pos > (3 * (width * width)) + (width * (width - 1))) ||//back
(pos < (4 * (width * width)) + (width - 1) && pos > (4 * (width * width))) || (pos < (4 * (width * width)) + ((width * width) - 1) && pos > (4 * (width * width)) + (width * (width - 1))) ||//right
(pos < (5 * (width * width)) + (width - 1) && pos > (5 * (width * width))) || (pos < (5 * (width * width)) + ((width * width) - 1) && pos > (5 * (width * width)) + (width * (width - 1))) //left
)
//EDGES
{
//edge code here...
}
else
{
//Normal adjacent detection here...
}
but that seems to be way more work than it should be.
I have seen someone with the same issue:
https://gamedev.stackexchange.com/questions/53866/pathfinding-on-a-uneven-planetary-surface
but I didn’t understand the answers that were provided, so if anyone can help, please try to explain it to me as basically as you can (I am fairly new to unity, but not programming in general), and maybe give some example or pseudo-code to help me visualize it. This problem is integral to my game and I am struggling to wrap my head around it.