Simple Navigation2D / pathfinding issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jeto143

Disclaimer: I’m a Godot beginner and this is probably obvious.

I’m trying to make a very simple 2D pathfinding exercise using:

  • a Navigation2D node,
  • a TileMap node using a tileset edited via the new Godot 3.1 editor, where one single tile is created/declared with a full rectangular NavigationPolygon over it (this tile covers the whole map),
  • a simple KineticBody2D object (containing a Sprite and CollisionShape2D) that I’d like to move around within that map.

I’m using the code from recent GDQuest tutorials, which simply waits for mouse clicks, calls Navigation2D.get_simple_path to fetch the path toward the click, then moves the object along it.

My Issue

When moving the object in a “long” diagonal, get_simple_path will return 3 or more points instead of 2 and the object will therefore move in two or more unnatural steps towards its target.

Is there any reason for this? I’ve followed other tutorials where this didn’t happen, using “manual” NavigationPolygonInstance. I cannot figure out any other difference.

Demo (minimal) project

:bust_in_silhouette: Reply From: splite


------------- original answer ---------------

The title of this question should be “Navigation with Tilemap has never heard about Pythagorean theorem:smiley:

The truth is, i dont know.

I tried your project and i dont see anything wrong (well, nitpicking, you ignore the fact that get_simple_path is returning points relative to navigation2d node, so you should offset them to your tank space – but in your case, you dont have to, navigation offset to common parent with your tank is [0;0])

I recreated your project from scratch and have exactly the same problem:

Godot 3.1.0

(you can download it here)

Godot 3.0.6

Better but still not clean path
You can download this project here

@splite Thank you very much for looking into this!

I actually spoke with KidsCanCode on Discord, who’s the guy who made this tutorial. I’ve downloaded its source here from GitHub and opened the corresponding project, tiled the middle part of the map (there were some “holes”) and hit F5 to get the exact same kind of result:

tilemap_navigation_3.1 15_4_2019 21_18_52

Now if instead of using per-tile navigation polygons, I replace it with a single NavigationPolygonInstance covering the whole navigable space, it behaves just fine:

tilemap_navigation_3.1 15_4_2019 21_16_59

(KidsCanCode told me he was curious and suspected a bug as well, and would take a look when he finds the time.)

Note that that project isn’t using the new Godot 3.1 Tileset editor at all, so it isn’t related to it.

I’m tempted to open an issue on the official GitHub and see what the Godot people tell me there. Do you think that would be appropriate?

Jeto143 | 2019-04-17 06:15

Yes, of course, they are nice guys + good thing is, you did your reserach first (you asked here). This is a great preparation for opening a issue.

Try to make things clear & give them your minimal project (the one you posted here is great - btw, you can just drag&drop file to github issue), link this question (or send there link to your issue, i will repost my gifs :slight_smile: )

splite | 2019-04-17 07:39

Just did:

I took the liberty to use one of your nice animated gifs :slight_smile:

Thanks again!

Jeto143 | 2019-04-20 14:25

Ok, i was thinking, maybe we are doing it wrong - maybe you are supposed to have own NavigationPolygon and what you paint on your tiles is like “DO NOT WALK THERE” polys (so your walkable location is XOR between big NavigationPoly and small polys from tiles…

Or throw away the big NavigationPoly entirely - and you are walking “outside” of the tile navi polys.

Either way, what you paint on tileset is “do not walk here”. And if thats true, Godot team did a poor job telegraphing the idea.

Cant test it right now (dont have any free time :frowning: )

Edit: well, i tested it and nope, doesnt work at all

Lets wait what guys say officially at your issue (worst case scenario is they tell us how to do it properly :slight_smile: )

By the way, for capturing gif, i am using

splite | 2019-04-23 07:55

:bust_in_silhouette: Reply From: Migoun

The problem with Godot’s navigation system is, it goes in any direction over the tilemaps. If you have a game where you want the path to go straight or diagonal over the tilemap, you have to write the algorithm either yourself or use something like SmartTilemap2D