Erich_L the godot pahtfinding is designed to work with navmesh. for something were you have a premade "structure", it is easier to just write a pathfinding algorithm in gdscript, like I did.
this is one of the less specialized ones I made, you can customize a pathfinding algorithm to your needs very easily:
extends Node
func find_shortest_path(closest_node : Vector2i, target : Vector2i) -> PackedVector2Array:
var map_size_shift : Vector2i = Vector2i(TerrainMap.hmapsiz, TerrainMap.hmapsiz)#terrain.map_size/2)
closest_node += map_size_shift
target += map_size_shift
var node_parents : Dictionary = Dictionary()
var follow_path : PackedVector2Array = PackedVector2Array()
var goal : Vector2i
goal = find_path(closest_node, target, node_parents)
if goal == closest_node:
follow_path.append(closest_node - map_size_shift)
print("failed find path")
return follow_path
var curr : Vector2i = goal
while(curr != closest_node):
follow_path.append(curr - map_size_shift)
curr = node_parents.get(curr)
#print("found path")
return follow_path
var directions : Array[Vector2i] = [Vector2i.DOWN, Vector2i.LEFT, Vector2i.UP, Vector2i.RIGHT]
func sort_ascending(a, b) -> bool:
if a[0] > b[0]:
return true
return false
func order_by_size(direc : Array[Vector2i], goal : Vector2i):
#astar
if direc:
var distance : Array = []#Array()# : Array[float, Vector2i]
#var positions : Array[Vector2i]
for i : Vector2i in direc:
distance.append([abs(goal - i).length(), i])
distance.sort_custom(sort_ascending)
direc.clear()
for i in distance:
direc.append(i[1])
func check_adjacents(pos : Vector2i) -> Array[Vector2i]:
var arr : Array[Vector2i] = []
if pos.x > 1 and pos.x < TerrainMap.map_size-1 and pos.y > 1 and pos.y < TerrainMap.map_size-1:
for i : Vector2i in directions:
var cpos : Vector2i = pos + i
var direc : int = TerrainMap.Voxel_road[(cpos.y * TerrainMap.map_size) + cpos.x]
if direc == 1:
arr.append(cpos)
return arr
func find_path(pos : Vector2i, goal : Vector2i, node_parents : Dictionary):
var queue : Array[Vector2i] = []
var exploredNodes : Dictionary = Dictionary()
var curr_node : Vector2i
queue.push_front(pos)
while(queue.size() != 0):
curr_node = queue.pop_front()
if curr_node == goal:
#reach destination
return curr_node
#terrain.Voxel_road[(goal.y * terrain.map_size) + goal.x]
var neigh : Array[Vector2i] = check_adjacents(curr_node)
#order by size
order_by_size(neigh, goal)
if neigh:
for i : Vector2i in neigh:
if exploredNodes.get(i, null) == null:
exploredNodes[i] = true
node_parents[i] = curr_node
queue.push_front(i)
return pos #no path found
I think you have to replace check_adjacents
with a way to find the nodes connected to the current node, and use your nodes
instead of Vector2i
, since mine was designed around tiles.