Vector2 points in get simple path

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

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

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)]

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

Andrea | 2021-01-16 13:14

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

ashish | 2021-01-17 15:15

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

Andrea | 2021-01-17 16:23

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

ashish | 2021-01-17 17:46

project folder

ashish | 2021-01-17 17:50

project folder

Will sharing the folder from my drive like this work?

ashish | 2021-01-17 17:52

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.

ashish | 2021-01-17 18:03

:bust_in_silhouette: Reply From: Andrea

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

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?

ashish | 2021-01-18 05:43

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)

Andrea | 2021-01-18 19:34

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

ashish | 2021-01-19 17:25