# A* pathfinding on a sphere projected cube (uneven planetary body)

This topic is 1731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, I'm new to the forum so I thought I'd just introduce myself within my first post, so hey!

I'm having some trouble wrapping my head around the best approach to generate the data for an A* pathfinding algorithm on my sphere project cube. I've posted my query on a few Q&A websites which has generated a lot of discussion but not many answers, and has lead me to think this is a problem that needs to be discussed on a forum!

http://gamedev.stackexchange.com/questions/53866/pathfinding-on-a-uneven-planetary-surface

http://stackoverflow.com/questions/16089074/a-pathfinding-over-multiple-grids

Background:

I've created a small planetary body from 6 planes forming a cube which have been sphere projected forming a sphere each face of my "sphere" has been procedurally displacement mapped to make uneven terrain. Illustrated below.

I want to be able to implement pathfinding so my agents can traverse the surface of the planet but I'm struggling to understand the best approach to generating the path finding data.

Obviously each face is a mesh of vertices with positions, I can easily take the positions of each vertex from each "sphere face" and generate one big list of nodes, but the problem comes when I'm trying to establish the adjacent nodes. I've considered that I could just precompute all 6 adjacent node for each and every node by doing a brute force distance check, but this just seems so inefficient. Or am I missing an obvious and simple solution?

To help better articulate my question this is my current planetary body:

Thanks, Caius.

Edited by Caiuseugene

##### Share on other sites

I'd put a node at the centre of each quad face on the cube, and use the original cube to get adjacency information (either 4 or 8 nodes depending on whether you want the diagonals connected). The only hassle is at the edges of the cube, not too difficult to cope with that though (and the corners have just 7 neighbours as well).

##### Share on other sites

use the original cube to get adjacency information (either 4 or 8 nodes depending on whether you want the diagonals connected)

Yes that does make sense, will have to sphere project the node data too in this case.

The only hassle is at the edges of the cube, not too difficult to cope with that though (and the corners have just 7 neighbours as well)

I think the issue is that each face of the cube is actually just a plane, so only contains vertex data about that face. I guess I could supply each face with there X+ X- Y+ Y- faces.

##### Share on other sites

Well you need to separate your sphere mesh representation from the node representation anyway. As long as you know which face is across from the cube edges it is pretty easy to build the adjacent node list. Using just 4 neighbouring nodes eliminates the 7 neighbour problem at the corners too.

##### Share on other sites

Well you need to separate your sphere mesh representation from the node representation anyway. As long as you know which face is across from the cube edges it is pretty easy to build the adjacent node list. Using just 4 neighbouring nodes eliminates the 7 neighbour problem at the corners too.

yes I understand now, it's going to be a pain getting the nodes from each adjacent face as rotation of the face as well as its adjacency to the "active face" will have to be calculated in order to return the correct edge. Would using just 4 neighbouring nodes make the algorithm much slower? Edited by Caiuse

##### Share on other sites

It looks like you will have to store your shortest path node list in memory somehow.

Also it seems that you need a bit more than brute force nearest nodes, you may need to exclude mountains and lakes, or assign appropriate values for hard-to-pass terrain.

So you may need to take that data you use for the terrain (i will call it "6 sphere face"), and use it to compute "shortest path node" only once (or only when it changes). This can be done and tested separately, until you get it right (even with smaller cube density if needed for testing purposes). In case of the corners, just allow null references for your nodes (if you need diagonal path too).

Every "shortest path node" may have a reference back to original "6 sphere face" node, so you get the real thing if needed.

This way your lookup algorithm remains simple and fast yet you do not loose any information about your world.

EDIT: As for adjacency of the faces, the most basic approach is naming things right. For example, of two futhermost corners of the box have respectively values of (-1, -1, -1) and (1, 1, 1), this information can be retained in cube face array. For example, cube face array may contain faces from (-1, -1, -1) to (-1, -1, 1) for U and from (-1, -1, -1) to (1, -1, -1) for V. A bit more math, but solving it would help to avoid a lot of repetition.

Edited by Nercury

##### Share on other sites

Well you need to separate your sphere mesh representation from the node representation anyway. As long as you know which face is across from the cube edges it is pretty easy to build the adjacent node list. Using just 4 neighbouring nodes eliminates the 7 neighbour problem at the corners too.

yes I understand now, it's going to be a pain getting the nodes from each adjacent face as rotation of the face as well as its adjacency to the "active face" will have to be calculated in order to return the correct edge. Would using just 4 neighbouring nodes make the algorithm much slower?

The less neighbouring nodes the faster the algorithm will run but the path won't be as optimal.

You only need to calculate the node connectivity information once. Try doing it with a 3x3 node layout per cube face first, it should scale up easily after that. You can always draw the network to check you have worked it out correctly.

Don't worry about impassable terrain or terrain with high movement cost at first, just get the network setup correctly. It's easy to add a prohibitively high cost for moving from one node to another if it is blocked later on.

You can optimise lookups once you have the network by having a 2d array of precalculated "next nodes" to travel to (will use a lot of memory if the number of nodes is high though). Say you want to go to node D from node A, and the optimal route is ABCD. Store B in the table for entry (A, D) i.e. the next node to travel to from A. The next node for travelling from B to D would then be C, so store that in the (B, D) entry. (You'd store C in the (D, A) entry assuming optimal route from D to A is DCBA).

##### Share on other sites

I would approach this problem the following way:

Build a graph G = (V, E) from your mesh, where the nodes V are the movable tiles (ie. faces) of your mesh

and the edges connect adjacent nodes.

In this case you could even use adjacency arrays, since for each node the number of adjacent nodes is constant.

Each node should have a reference to the face it represents or at least a position coordinate.

Finding a shortest path on graphs is a solved problem. Use your favorite algorithm for this, maybe A* or the

general dijkstra. Dont store shortest paths, recompute them. I doubt it will be a bottleneck.

Sidenote: as mentioned above, dont introduce special cases with null-pointers to handle impassable tiles.

Just use high costs. Speaking of costs: dont forget to test different measurements of distance.

normal l2-norm might not be the best on a spherical surface.

Edited by plainoldcj

##### Share on other sites

dont introduce special cases with null-pointers to handle impassable tiles

When I mentioned null pointers I was thinking about those corner tiles which have 7 neighbors instead of 8.

##### Share on other sites

You could construct a linked list to represent the planet's mesh.

enum Direction{
Above,
Below,
Left,
Right
};

struct Vertex{
float phi;
float theta;
float r;
float cost;

Vertex *neighbors;
};

Using this method, the only real effort required is in the initial assembly of the linked list. For pathfinding you would just iterate through the linked list like you would for a normal array.