+4 votes

Please give me solution about the following issue.
Currently, I'm working on test project to study 2D navigation.
(It based on sample project "Navigation Polygon (2D)”.)

First, I have add Navigation2D node as root node.
And I have add NavigationPolygonInstance, “player” scene instance and “obstacle” scene instance to children of it.

"player" scene has been structed from KinematicBody2D which has Sprite and CollisionPolygon2D as child node.
"obstacle" scene has been structed from StaticBody2D which has Sprite and CollisionPolygon2D as child node.

For the NavigationPolygonInstance, I have add polygon like the following image preliminarily by editor.


“player” has been put at top-left side.
"obstacle" has been put at right side.


And, I have add a script to Navigation2D node, like the following.

extends Navigation2D

# Member variables
const SPEED = 200.0

var begin = Vector2()
var end = Vector2()
var path = []

func _process(delta):
    if (path.size() > 1):
        var to_walk = delta*SPEED
        while(to_walk > 0 and path.size() >= 2):
            var pfrom = path[path.size() - 1]
            var pto = path[path.size() - 2]
            var d = pfrom.distance_to(pto)
            if (d <= to_walk):
                path.remove(path.size() - 1)
                to_walk -= d
                path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d)
                to_walk = 0

        var atpos = path[path.size() - 1]

        if (path.size() < 2):
            path = []

func _update_path():
    var new_polygon = Vector2Array()
    var col_polygon = get_node("obstacle").get_node("CollisionPolygon2D").get_polygon()

    for vector in col_polygon:
        new_polygon.append(vector + get_node("obstacle").get_pos())

    var navi_polygon = get_node("NavigationPolygonInstance").get_navigation_polygon()

    var p = get_simple_path(begin, end, true)
    path = Array(p) # Vector2array too complex to use, convert to regular array


func _input(event):
    if (event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1):
        begin = get_node("player").get_pos()
        # Mouse to local navigation coordinates
        end = event.pos - get_pos()

func _ready():

This script works like the following flow.

(1) Detects the pressing of mouse button, and get cursor position.
(2) Gets a polygon from CollisionPolygon2D of “obstacle”.
And add the polygon to NavigationPolygonInstance.
(3) Get a path by calling “getsimplepath” function.
(4) Moves "player" along the path.

In short, I want to get a path from both default NavigationPolygon and collision polygon of object which has been put on scene.

But this script does not work well.
The following GIF image shows the movement of player.

failure movement

(“Visible Navigation” of debug option is enabled.)
After the pressing of mouse button, Gray area appears around "obstacle".
I think that it means the collision polygon of "obstacle" has been add to NavigationPolygonInstance.
But, “player” move through over the right side area. Why?

Any mistake in my code?
Or, any other procedure is needed?
Please any advice.

in Engine by (16 points)

I'm afraid I don't have an answer to this, but I'm playing around with doing the same in Godot 3.0 Beta 1, and I'm having the exact same issue. I can update the navmesh itself easily, but the Navigation2D node doesn't seem to register the modified navmesh.

1 Answer

0 votes

For anyone looking for the answer:

So here is how you get Navigation2D to recognize updates to the nav-mesh:

var navPolyInstance = $NavPolyInstance
navPolyInstance.enabled = false
navPolyInstance.enabled = true

I tried it for my own project, seems to do the trick.

A lot more info here:

by (25 points)
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 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.