gem install chingu-pathfinding
, which requires a c++ compiler.
class Map < Pathfinding
def initialize(width, height)
super(width, height, 5)
end
...
end
class Map < Pathfinding
def initialize(width, height)
super(width, height, 1)
end
...
end
class Map < Pathfinding
def initialize(width, height)
super(width, height, 1)
end
def blocked?(x,y)
x < 10 or x > 90 or y < 10 or y > 90
end
...
end
find_path(start_x, start_y, goal_x, goal_y)
, which will return a list of points (arrays of 2 elements) to pass through with the first points being [start_x, start_y] and the last being [goal_x, goal_y]. If you move linearly between the points, you will not hit any blocked pixels. If you can't reach the goal for some reason, nil is returned.find_path_update(start_x, start_y, goal_x, goal_y, path)
This uses the observation that if we move along the path and maybe the goal moves a bit, then it would be wasteful to throw the previous ideal path away. If your map is big enough it might be worth to use this instead of using find_path
for updates on the path finding.find_path
method to return a linear array of coordinates ([x1, y1, x2, y2, ...]
) to take a big load off the garbage collector. But I think working with such an array would be quite painful. :/
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill