- Home /
Calculating nodes around corners of cube for pathfinding
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.
Answer by DeusIgnis · Jan 07, 2018 at 01:30 PM
I figured this out eventually. While my solution is not the prettiest, it works. All I am doing is converting the index of the node into a Vector3 based on its position on the cube. Then I check the 26 adjacent spaces in 3D space and discard all where the index is not found.
Your answer
Follow this Question
Related Questions
how to make a planet 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Subdivide mesh C#... help 0 Answers
How to make planet walker camera ? 4 Answers