Center navigation2d 'points' to tilemap tiles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stillinthe90s
:warning: Old Version Published before Godot 3 was released.

Short: Is there a way to center navigation2d points to tilemap tiles?

Long:
In an attempt of using tilmap with navpoly for tilebased movement, I tried centering the navigation points (there is a thread about this around here somewhere, although I can’t find it) like so:

#Calculate path from player/start position to a clicked position:
var clickpos = get_global_mouse_pos()
path =Array(nav.get_simple_path(player.get_global_pos(),clickpos,true))

#Store centered points in the array "centerpath":
for p in path:
	var centerpoint =map.map_to_world(map.world_to_map(p)) +Vector2(0,map.get_cell_size().y/2.0)
    centerpath.append(centerpoint)

#Use "centerpath" as actual navigation path:
path=centerpath

This will center the path’s points (white dots) but also result in some problems:
green = walkable, gray = not walkable, red = Start & Click positions

I guess this is because navigation2d adds its points like this:

Replacing bits of the code in order to get the relevant ‘walkable’ tiles:

for p in path:
	var tile= map.world_to_map(p)	#Get tileset coordinates at p.
	var tilepoint =map.map_to_world((tile))	#Get current tile's "global" coordinates.

…returns point 5 of 7 (in the example above) as a gray tile. Shouldn’t this only return coordinates from ‘walkable’ tiles, since it was taken from navigation2d’s path array in the first place?

The problem persists on square tilemaps and does not only allow cutting corners, but also stopping and traversing over some gray or empty tiles like this.

Is there a way of getting around this?

I ran in the same kind of problems and others more while trying to incorporate Godot’s A* on my projects, So I did my own. It’s really simple and you gain a lot more control over the way you want it to work. Would you consider it? This site has all you can possibly need for it, and I can provide my old project for you to mess around if you want.

Punpun | 2017-10-11 01:20

I had no such problems with the A* node. It actually worked really well. Problem is that using more than a handfull of tiles makes the initial calculations horribly slow. I’m not competent enough to tell whether this is a general A* thing or if the godot node is to blame. My implementation was semi-based on this demo project btw.
Having said that, I would love to mess around with your old project (that is, unless it has a broken A* that I could never hope to fix).

Oh, and thanks for the tip. Great read!

stillinthe90s | 2017-10-11 16:56

Idk either, but in my experience the Godot’s navigation and A* were always a hassle to adapt; more code went in corrections than in features… So making one is really much more efficient. My project is also isometric, so it’ll probably be easier to see the advantages this can potentially bring to your game.

Give it a try!

Punpun | 2017-10-11 17:20

Looks really promising! I’m going to mess around and see what it can do. A little confused by the double tilemap nodes… :slight_smile:
Cheers!

stillinthe90s | 2017-10-11 17:42

Wops, looks like I should have cleaned that. It’s because the original ideia was making maps with multiple heights (tilemaps), so one would be for aesthetics and the other would be for actual navigation data.
However I couldn’t get around the way the tilemap Y-sorting worked, so I just left it as it was. But you can easily define what is a valid tile and what isn’t by index from a single tilemap in code and getting rid off the nav tilemap.

Punpun | 2017-10-11 17:55

Got it working pretty smoothly with a single tilemap and diagonal movement. Tweaked a few things to make it work with different tile sizes.
Seriously, this is pretty darn good! You should clean it up a little bit and put it in the asset library, if it’s not already in there.

stillinthe90s | 2017-10-11 20:31

Yeah, maybe I should do it… Well, anyway, glad you liked it. Good luck on your project!

Punpun | 2017-10-11 22:02

And good luck with your stuff as well!

stillinthe90s | 2017-10-12 15:06