-1

In my 2d android game, I have an image of a map as the background. I want to spawn objects and make them walk to a given destination. However, I want them to walk on the actual roads. I tried to use pathfinding grid and navmesh, however, they are not accurate and therefore couldn't detect the actual small roads on the map. Any idea how I can achieve this?

Edit: I have a high res image with no labels. I also tried using a png of the roads layout and marking it as walkable but since the roads are small and too close to one another it doesn't draw the navmesh on them.

(red is the player, green is the given destination, blue is the path automatically created for red to walk on after the destination is given)

Example

UnBanned
  • 121
  • 11
  • try using waypoints and the rest the objects will follow whatever path the linked points will create – thunderkill Jan 11 '23 at 10:47
  • I thought about that, but since there are many small roads that are close to each other it would be extremely difficult, I would have to add so many points and it wouldn't guarantee the object wouldn't take a shortcut and go off-road, also it would affect performance – UnBanned Jan 11 '23 at 10:54

1 Answers1

1

I have an image of a map as the background

This is the wrong type of data to start of with. To do any kind of navigation you will need some type of graph data, either to create a navmesh from, or to implement your own pathfinding (A star is not that difficult to implement). But creating a graph from just a image of a map will be doomed to failure since roads will be covered by labels and other kinds of visual noise. It might be a feasible approach if you could get "clean" image of sufficient resolution, ideally without any kind of anti aliasing or other processing that could interfere with analysis.

You would be much better of to start with an object representation of your map and either convert this to an image, or correlate the object positions to coordinates on the image. You may for example use the open streetmap API for this kind of data. Or just create your graph by hand if you only have a single small map.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Maybe I didn't make it clear, I have a high res image with no labels. I also tried using a png of the roads layout and marking it as walkable but since the roads are small and too close to one another it doesn't draw the navmesh on them. I edited the question. – UnBanned Jan 11 '23 at 13:36
  • 1
    @Unbanned If the navmesh generation step is the problem you might just workaround it by treating each pixel as a graph node and running the path finding on this graph directly. But you any method you use will likely produce worse result than using the data the map was generated from. – JonasH Jan 11 '23 at 13:44