From 96d98d8c4e5caad127185bfc0b259cc4d310868e Mon Sep 17 00:00:00 2001
From: smix8 <52464204+smix8@users.noreply.github.com>
Date: Thu, 9 Jun 2022 11:39:23 +0200
Subject: [PATCH] Backport default World navigation maps
Backports default navigation maps created with each World or World2D.
---
doc/classes/ProjectSettings.xml | 12 ++++++++++++
doc/classes/World.xml | 3 +++
doc/classes/World2D.xml | 3 +++
scene/resources/world.cpp | 13 +++++++++++++
scene/resources/world.h | 3 +++
scene/resources/world_2d.cpp | 15 +++++++++++++++
scene/resources/world_2d.h | 2 ++
7 files changed, 51 insertions(+)
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml
index 2e0401513d9..4530c6c5867 100644
--- a/doc/classes/ProjectSettings.xml
+++ b/doc/classes/ProjectSettings.xml
@@ -990,6 +990,18 @@
The policy to use for unhandled Mono (C#) exceptions. The default "Terminate Application" exits the project as soon as an unhandled exception is thrown. "Log Error" logs an error message to the console instead, and will not interrupt the project execution when an unhandled exception is thrown.
[b]Note:[/b] The unhandled exception policy is always set to "Log Error" in the editor, which also includes C# [code]tool[/code] scripts running within the editor as well as editor plugin code.
+
+ Default cell size for 2D navigation maps. See [method Navigation2DServer.map_set_cell_size].
+
+
+ Default edge connection margin for 2D navigation maps. See [method Navigation2DServer.map_set_edge_connection_margin].
+
+
+ Default cell size for 3D navigation maps. See [method NavigationServer.map_set_cell_size].
+
+
+ Default edge connection margin for 3D navigation maps. See [method NavigationServer.map_set_edge_connection_margin].
+
Maximum amount of characters allowed to send as output from the debugger. Over this value, content is dropped. This helps not to stall the debugger connection.
diff --git a/doc/classes/World.xml b/doc/classes/World.xml
index 9c6a52e3cc8..c8111587b60 100644
--- a/doc/classes/World.xml
+++ b/doc/classes/World.xml
@@ -21,6 +21,9 @@
The World's fallback_environment will be used if the World's [Environment] fails or is missing.
+
+ The [RID] of this world's navigation map. Used by the [NavigationServer].
+
The World's visual scenario.
diff --git a/doc/classes/World2D.xml b/doc/classes/World2D.xml
index fef03eb04b6..3940ab8e18c 100644
--- a/doc/classes/World2D.xml
+++ b/doc/classes/World2D.xml
@@ -18,6 +18,9 @@
Direct access to the world's physics 2D space state. Used for querying current and potential collisions. When using multi-threaded physics, access is limited to [code]_physics_process(delta)[/code] in the main thread.
+
+ The [RID] of this world's navigation map. Used by the [Navigation2DServer].
+
The [RID] of this world's physics space resource. Used by the [Physics2DServer] for 2D physics, treating it as both a space and an area.
diff --git a/scene/resources/world.cpp b/scene/resources/world.cpp
index f5d33eb5f8d..43d167a4294 100644
--- a/scene/resources/world.cpp
+++ b/scene/resources/world.cpp
@@ -35,6 +35,7 @@
#include "scene/3d/camera.h"
#include "scene/3d/visibility_notifier.h"
#include "scene/scene_string_names.h"
+#include "servers/navigation_server.h"
struct SpatialIndexer {
Octree octree;
@@ -245,6 +246,10 @@ RID World::get_scenario() const {
return scenario;
}
+RID World::get_navigation_map() const {
+ return navigation_map;
+}
+
void World::set_environment(const Ref &p_environment) {
if (environment == p_environment) {
return;
@@ -296,6 +301,7 @@ void World::get_camera_list(List *r_cameras) {
void World::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_space"), &World::get_space);
ClassDB::bind_method(D_METHOD("get_scenario"), &World::get_scenario);
+ ClassDB::bind_method(D_METHOD("get_navigation_map"), &World::get_navigation_map);
ClassDB::bind_method(D_METHOD("set_environment", "env"), &World::set_environment);
ClassDB::bind_method(D_METHOD("get_environment"), &World::get_environment);
ClassDB::bind_method(D_METHOD("set_fallback_environment", "env"), &World::set_fallback_environment);
@@ -305,6 +311,7 @@ void World::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_fallback_environment", "get_fallback_environment");
ADD_PROPERTY(PropertyInfo(Variant::_RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space");
ADD_PROPERTY(PropertyInfo(Variant::_RID, "scenario", PROPERTY_HINT_NONE, "", 0), "", "get_scenario");
+ ADD_PROPERTY(PropertyInfo(Variant::_RID, "navigation_map", PROPERTY_HINT_NONE, "", 0), "", "get_navigation_map");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState", 0), "", "get_direct_space_state");
}
@@ -320,6 +327,11 @@ World::World() {
PhysicsServer::get_singleton()->area_set_param(space, PhysicsServer::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/3d/default_angular_damp", 0.1));
ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/default_angular_damp", PropertyInfo(Variant::REAL, "physics/3d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"));
+ navigation_map = NavigationServer::get_singleton()->map_create();
+ NavigationServer::get_singleton()->map_set_active(navigation_map, true);
+ NavigationServer::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/3d/default_cell_size", 0.25));
+ NavigationServer::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/3d/default_edge_connection_margin", 0.25));
+
#ifdef _3D_DISABLED
indexer = NULL;
#else
@@ -330,6 +342,7 @@ World::World() {
World::~World() {
PhysicsServer::get_singleton()->free(space);
VisualServer::get_singleton()->free(scenario);
+ NavigationServer::get_singleton()->free(navigation_map);
#ifndef _3D_DISABLED
memdelete(indexer);
diff --git a/scene/resources/world.h b/scene/resources/world.h
index e7d50f1eb22..5dff58797d5 100644
--- a/scene/resources/world.h
+++ b/scene/resources/world.h
@@ -47,6 +47,8 @@ class World : public Resource {
private:
RID space;
RID scenario;
+ RID navigation_map;
+
SpatialIndexer *indexer;
Ref environment;
Ref fallback_environment;
@@ -70,6 +72,7 @@ protected:
public:
RID get_space() const;
RID get_scenario() const;
+ RID get_navigation_map() const;
void set_environment(const Ref &p_environment);
Ref get_environment() const;
diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp
index 9c73fa627e5..63ab21b51bc 100644
--- a/scene/resources/world_2d.cpp
+++ b/scene/resources/world_2d.cpp
@@ -34,6 +34,7 @@
#include "scene/2d/camera_2d.h"
#include "scene/2d/visibility_notifier_2d.h"
#include "scene/main/viewport.h"
+#include "servers/navigation_2d_server.h"
#include "servers/physics_2d_server.h"
#include "servers/visual_server.h"
@@ -326,6 +327,10 @@ RID World2D::get_space() {
return space;
}
+RID World2D::get_navigation_map() {
+ return navigation_map;
+}
+
void World2D::get_viewport_list(List *r_viewports) {
for (Map::Element *E = indexer->viewports.front(); E; E = E->next()) {
r_viewports->push_back(E->key());
@@ -335,11 +340,13 @@ void World2D::get_viewport_list(List *r_viewports) {
void World2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_canvas"), &World2D::get_canvas);
ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space);
+ ClassDB::bind_method(D_METHOD("get_navigation_map"), &World2D::get_navigation_map);
ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state);
ADD_PROPERTY(PropertyInfo(Variant::_RID, "canvas", PROPERTY_HINT_NONE, "", 0), "", "get_canvas");
ADD_PROPERTY(PropertyInfo(Variant::_RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space");
+ ADD_PROPERTY(PropertyInfo(Variant::_RID, "navigation_map", PROPERTY_HINT_NONE, "", 0), "", "get_navigation_map");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "Physics2DDirectSpaceState", 0), "", "get_direct_space_state");
}
@@ -359,11 +366,19 @@ World2D::World2D() {
ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/default_linear_damp", PropertyInfo(Variant::REAL, "physics/2d/default_linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"));
Physics2DServer::get_singleton()->area_set_param(space, Physics2DServer::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/2d/default_angular_damp", 1.0));
ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/default_angular_damp", PropertyInfo(Variant::REAL, "physics/2d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"));
+
+ // Create and configure the navigation_map to be more friendly with pixels than meters.
+ navigation_map = Navigation2DServer::get_singleton()->map_create();
+ Navigation2DServer::get_singleton()->map_set_active(navigation_map, true);
+ Navigation2DServer::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/2d/default_cell_size", 1));
+ Navigation2DServer::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/2d/default_edge_connection_margin", 1));
+
indexer = memnew(SpatialIndexer2D);
}
World2D::~World2D() {
VisualServer::get_singleton()->free(canvas);
Physics2DServer::get_singleton()->free(space);
+ Navigation2DServer::get_singleton()->free(navigation_map);
memdelete(indexer);
}
diff --git a/scene/resources/world_2d.h b/scene/resources/world_2d.h
index 4f5c1490292..b771206dd3f 100644
--- a/scene/resources/world_2d.h
+++ b/scene/resources/world_2d.h
@@ -44,6 +44,7 @@ class World2D : public Resource {
RID canvas;
RID space;
+ RID navigation_map;
SpatialIndexer2D *indexer;
@@ -65,6 +66,7 @@ protected:
public:
RID get_canvas();
RID get_space();
+ RID get_navigation_map();
Physics2DDirectSpaceState *get_direct_space_state();