0 votes

How do I access the points in the path of Navigation2D getsimplepath?

I am unable to access the points when I print the points of the PoolVector2Array. The path is showing a null array. When I convert the PoolVector2Array to an Array, and then print the array that has the points of the path, I get a Vector2(0,0) output. What am I doing wrong?

Please help.

Here is my code:

extends Node2D
onready var start_point = $Sprite
onready var finish_point = $Sprite2
onready var navig = $Navigation2D
onready var line = $line
var start = Vector2(0,0)
var finish = Vector2(0,0)
var path = PoolVector2Array()

func _ready():
    finish =finish_point.position
    start = start_point.position
    print(start, finish)
    path = navig.get_simple_path(start, finish)
    print(path)
    var vectors = PoolVector2Array(Array([path]))
    print(vectors)

This is the output:

(186, 260)(1298, 260)
[ ]
[(0, 0)]

Godot version 3.2.2
in Engine by (100 points)
edited by

did you set up the navigation nodes correctly?
are those point inside the navigation polygon?

Hi,
Thanks for your reply. Attaching how I have set up the Navigation2D node and NavigationPolygonInstance.
The output that I get with the same code and this setup is:

(408, 200)(1296, 200)
[(408, 200), (1296, 200)]
[(0, 0)]

https://ibb.co/qgQCQG4

can you share the project folder entirely?
from the picture is not clear yet

Hi,
Thanks for looking into my problem.
How does one share the project folder entirely?

project folder

Will sharing the folder from my drive like this work?

The aim of the app is to move the left TouchButton (will change it to KinematicBody) to the right in a straight line. If the player moves vertically up or down as per the move of the mouse button, then bring the player to the nearest point of the path.

1 Answer

+1 vote

i get the very same output

(408, 200)(1296, 200)
[(408, 200), (1296, 200)]
[(0, 0)]

which is correct as it is possible to reach the finish point with a single step from the starting point, therefore the path contains only 2 point: start, finish.
below the result if I set the finish point as 1220,440. You can see the path contains 2 more change of direction points.

(408, 200)(1220, 440)
[(408, 200), (384, 248), (384, 312), (1220, 440)]
[(0, 0)]

you can visualise the path adding these 2 lines

func _draw():
    draw_polyline(path, Color(1,1,1), 1)

i'm not sure what happens with var vectors = PoolVector2Array(Array([path])), i think it's not possible to convert an array to a poolvector2 array that easily.
Said that, i dont understand what is the point of doing this double conversion, as path is already a PoolVector2Array

by (1,459 points)

Thank you so much for looking into the problem.
I was trying to make a vector array so that I could resort the array so formed.
Does this mean that if the y value (or x point, if you are going up and down) of the next point is same as the previous point, then one reaches the destination in one hop?

well no, that depends solely on the shape of the nav polygon: if the path is free from start to finish, it will reach it one jump.
Although there is another thing to consider:

https://docs.godotengine.org/en/stable/classes/class_navigation.html#class-navigation-method-get-simple-path

get simple path has actually 3 arguments: start point, end point and optimization (which is true by default).
If true, the path will only consist of 90° and 45° change of direction, if false it will consider any angle (it will be a little slower, but barely noticible)

Thank you very much. Your inputs have been very helpful.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.