Skip to content

Commit

Permalink
'Navigation Polygon 2D' ported to Godot 4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex2782 committed Sep 21, 2023
1 parent 1113baf commit fb780cd
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 104 deletions.
11 changes: 5 additions & 6 deletions 2d/navigation/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Navigation Polygon 2D

Example of using 2D navigation using a
[`NavigationPolygon`](https://docs.godotengine.org/en/latest/classes/class_navigationpolygon.html)
in a [`NavigationPolygonInstance`](https://docs.godotengine.org/en/latest/classes/class_navigationpolygoninstance.html) node.
It uses the 2D navigation API to request a path between two points,
and then traverses the resulting path.
Example of using 2D navigation using:
- [`NavigationRegion2D`](https://docs.godotengine.org/en/latest/classes/class_navigationregion2d.html)
- [`NavigationPolygon`](https://docs.godotengine.org/en/latest/classes/class_navigationpolygon.html)
- [`NavigationAgent2D`](https://docs.godotengine.org/en/latest/classes/class_navigationagent2d.html)

Language: GDScript

Renderer: GLES 2
Renderer: Forward+

Check out this demo on the asset library: https://godotengine.org/asset-library/asset/117

Expand Down
36 changes: 36 additions & 0 deletions 2d/navigation/character.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extends CharacterBody2D


var movement_speed: float = 200.0
@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D


func _ready():
# These values need to be adjusted for the actor's speed
# and the navigation layout.
navigation_agent.path_desired_distance = 2.0
navigation_agent.target_desired_distance = 2.0
navigation_agent.debug_enabled = true


# The "click" event is a custom input action defined in
# Project > Project Settings > Input Map tab.
func _unhandled_input(event):
if not event.is_action_pressed("click"):
return
set_movement_target(get_global_mouse_position())


func set_movement_target(movement_target: Vector2):
navigation_agent.target_position = movement_target


func _physics_process(_delta):
if navigation_agent.is_navigation_finished():
return

var current_agent_position: Vector2 = global_position
var next_path_position: Vector2 = navigation_agent.get_next_path_position()

velocity = current_agent_position.direction_to(next_path_position) * movement_speed
move_and_slide()
2 changes: 1 addition & 1 deletion 2d/navigation/character.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ dest_files=["res://.godot/imported/character.png-7a996d3b758d22c506b76a7c1539128
[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
Expand Down
22 changes: 22 additions & 0 deletions 2d/navigation/character.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[gd_scene load_steps=4 format=3 uid="uid://ct7veakwiei3h"]

[ext_resource type="Script" path="res://character.gd" id="1_8uimh"]
[ext_resource type="Texture2D" uid="uid://b0wokaenwu7pj" path="res://character.png" id="1_rmg12"]

[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_20ukx"]
radius = 7.0
height = 22.0

[node name="Character" type="CharacterBody2D"]
script = ExtResource("1_8uimh")

[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(0, -3)
scale = Vector2(0.3, 0.3)
texture = ExtResource("1_rmg12")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -3)
shape = SubResource("CapsuleShape2D_20ukx")

[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."]
32 changes: 0 additions & 32 deletions 2d/navigation/level.tscn

This file was deleted.

2 changes: 1 addition & 1 deletion 2d/navigation/map.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ dest_files=["res://.godot/imported/map.png-9eea34967fae34f4388f4a32a16da936.ctex
[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
Expand Down
60 changes: 0 additions & 60 deletions 2d/navigation/navigation.gd

This file was deleted.

18 changes: 18 additions & 0 deletions 2d/navigation/navigation.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_scene load_steps=4 format=3 uid="uid://bjgad00c2xiuc"]

[ext_resource type="Texture2D" uid="uid://bk26gi6qsuh18" path="res://map.png" id="2_nxfkp"]
[ext_resource type="NavigationPolygon" uid="uid://bk5r48dcijlqt" path="res://navigation_polygon.res" id="3_6c0vu"]
[ext_resource type="PackedScene" uid="uid://ct7veakwiei3h" path="res://character.tscn" id="4_n6iop"]

[node name="Navigation" type="Node2D"]

[node name="Map" type="Sprite2D" parent="."]
z_index = -1
position = Vector2(400, 302)
texture = ExtResource("2_nxfkp")

[node name="NavigationRegion2D" type="NavigationRegion2D" parent="."]
navigation_polygon = ExtResource("3_6c0vu")

[node name="Character" parent="." instance=ExtResource("4_n6iop")]
position = Vector2(211, 141)
Binary file added 2d/navigation/navigation_polygon.res
Binary file not shown.
8 changes: 4 additions & 4 deletions 2d/navigation/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ config/name="Navigation Polygon 2D"
config/description="Example of using 2D navigation using a NavigationPolygon in a
NavigationPolygonInstance node. It uses the 2D navigation API to request
a path between two points, and then traverses the resulting path."
run/main_scene="res://level.tscn"
config/features=PackedStringArray("4.0")
config/icon="res://icon.webp"
config/tags=PackedStringArray("2d", "ai", "demo", "official")
run/main_scene="res://navigation.tscn"
config/features=PackedStringArray("4.1")
config/icon="res://icon.webp"

[display]

Expand All @@ -29,7 +29,7 @@ window/stretch/aspect="expand"

click={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null)
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}

Expand Down

0 comments on commit fb780cd

Please sign in to comment.