navigation_agent_3d.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /**************************************************************************/
  2. /* navigation_agent_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "navigation_agent_3d.h"
  31. #include "scene/3d/navigation/navigation_link_3d.h"
  32. #include "servers/navigation_server_3d.h"
  33. void NavigationAgent3D::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent3D::get_rid);
  35. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent3D::set_avoidance_enabled);
  36. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent3D::get_avoidance_enabled);
  37. ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent3D::set_path_desired_distance);
  38. ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent3D::get_path_desired_distance);
  39. ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
  40. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
  41. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
  42. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
  43. ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationAgent3D::set_height);
  44. ClassDB::bind_method(D_METHOD("get_height"), &NavigationAgent3D::get_height);
  45. ClassDB::bind_method(D_METHOD("set_path_height_offset", "path_height_offset"), &NavigationAgent3D::set_path_height_offset);
  46. ClassDB::bind_method(D_METHOD("get_path_height_offset"), &NavigationAgent3D::get_path_height_offset);
  47. ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationAgent3D::set_use_3d_avoidance);
  48. ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationAgent3D::get_use_3d_avoidance);
  49. ClassDB::bind_method(D_METHOD("set_keep_y_velocity", "enabled"), &NavigationAgent3D::set_keep_y_velocity);
  50. ClassDB::bind_method(D_METHOD("get_keep_y_velocity"), &NavigationAgent3D::get_keep_y_velocity);
  51. ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
  52. ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
  53. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
  54. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
  55. ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent3D::set_time_horizon_agents);
  56. ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent3D::get_time_horizon_agents);
  57. ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent3D::set_time_horizon_obstacles);
  58. ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent3D::get_time_horizon_obstacles);
  59. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
  60. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
  61. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
  62. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
  63. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
  64. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
  65. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
  66. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
  67. ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent3D::set_pathfinding_algorithm);
  68. ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent3D::get_pathfinding_algorithm);
  69. ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent3D::set_path_postprocessing);
  70. ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent3D::get_path_postprocessing);
  71. ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent3D::set_path_metadata_flags);
  72. ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent3D::get_path_metadata_flags);
  73. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
  74. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
  75. ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent3D::set_target_position);
  76. ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent3D::get_target_position);
  77. ClassDB::bind_method(D_METHOD("set_simplify_path", "enabled"), &NavigationAgent3D::set_simplify_path);
  78. ClassDB::bind_method(D_METHOD("get_simplify_path"), &NavigationAgent3D::get_simplify_path);
  79. ClassDB::bind_method(D_METHOD("set_simplify_epsilon", "epsilon"), &NavigationAgent3D::set_simplify_epsilon);
  80. ClassDB::bind_method(D_METHOD("get_simplify_epsilon"), &NavigationAgent3D::get_simplify_epsilon);
  81. ClassDB::bind_method(D_METHOD("set_path_return_max_length", "length"), &NavigationAgent3D::set_path_return_max_length);
  82. ClassDB::bind_method(D_METHOD("get_path_return_max_length"), &NavigationAgent3D::get_path_return_max_length);
  83. ClassDB::bind_method(D_METHOD("set_path_return_max_radius", "radius"), &NavigationAgent3D::set_path_return_max_radius);
  84. ClassDB::bind_method(D_METHOD("get_path_return_max_radius"), &NavigationAgent3D::get_path_return_max_radius);
  85. ClassDB::bind_method(D_METHOD("set_path_search_max_polygons", "max_polygons"), &NavigationAgent3D::set_path_search_max_polygons);
  86. ClassDB::bind_method(D_METHOD("get_path_search_max_polygons"), &NavigationAgent3D::get_path_search_max_polygons);
  87. ClassDB::bind_method(D_METHOD("set_path_search_max_distance", "distance"), &NavigationAgent3D::set_path_search_max_distance);
  88. ClassDB::bind_method(D_METHOD("get_path_search_max_distance"), &NavigationAgent3D::get_path_search_max_distance);
  89. ClassDB::bind_method(D_METHOD("get_path_length"), &NavigationAgent3D::get_path_length);
  90. ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent3D::get_next_path_position);
  91. ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent3D::set_velocity_forced);
  92. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
  93. ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent3D::get_velocity);
  94. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
  95. ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent3D::get_current_navigation_result);
  96. ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent3D::get_current_navigation_path);
  97. ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent3D::get_current_navigation_path_index);
  98. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
  99. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
  100. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
  101. ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent3D::get_final_position);
  102. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
  103. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent3D::set_avoidance_layers);
  104. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent3D::get_avoidance_layers);
  105. ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent3D::set_avoidance_mask);
  106. ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent3D::get_avoidance_mask);
  107. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent3D::set_avoidance_layer_value);
  108. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent3D::get_avoidance_layer_value);
  109. ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent3D::set_avoidance_mask_value);
  110. ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent3D::get_avoidance_mask_value);
  111. ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent3D::set_avoidance_priority);
  112. ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent3D::get_avoidance_priority);
  113. ADD_GROUP("Pathfinding", "");
  114. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
  115. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_path_desired_distance", "get_path_desired_distance");
  116. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_target_desired_distance", "get_target_desired_distance");
  117. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01,or_greater,suffix:m"), "set_path_height_offset", "get_path_height_offset");
  118. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "0.01,100,0.1,or_greater,suffix:m"), "set_path_max_distance", "get_path_max_distance");
  119. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  120. ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
  121. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered,None"), "set_path_postprocessing", "get_path_postprocessing");
  122. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_metadata_flags", PROPERTY_HINT_FLAGS, "Include Types,Include RIDs,Include Owners"), "set_path_metadata_flags", "get_path_metadata_flags");
  123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "simplify_path"), "set_simplify_path", "get_simplify_path");
  124. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "simplify_epsilon", PROPERTY_HINT_RANGE, "0.0,10.0,0.001,or_greater,suffix:m"), "set_simplify_epsilon", "get_simplify_epsilon");
  125. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_length", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater,suffix:m"), "set_path_return_max_length", "get_path_return_max_length");
  126. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_radius", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater,suffix:m"), "set_path_return_max_radius", "get_path_return_max_radius");
  127. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_search_max_polygons", PROPERTY_HINT_RANGE, "0,4096,1,or_greater"), "set_path_search_max_polygons", "get_path_search_max_polygons");
  128. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_search_max_distance"), "set_path_search_max_distance", "get_path_search_max_distance");
  129. ADD_GROUP("Avoidance", "");
  130. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  131. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  132. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_height", "get_height");
  133. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
  134. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,10000,0.01,or_greater,suffix:m"), "set_neighbor_distance", "get_neighbor_distance");
  135. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
  136. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_agents", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_agents", "get_time_horizon_agents");
  137. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_obstacles", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_obstacles", "get_time_horizon_obstacles");
  138. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.01,10000,0.01,or_greater,suffix:m/s"), "set_max_speed", "get_max_speed");
  139. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance");
  140. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_y_velocity"), "set_keep_y_velocity", "get_keep_y_velocity");
  141. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  142. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");
  143. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");
  144. ADD_SIGNAL(MethodInfo("path_changed"));
  145. ADD_SIGNAL(MethodInfo("target_reached"));
  146. ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  147. ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  148. ADD_SIGNAL(MethodInfo("navigation_finished"));
  149. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
  150. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent3D::set_debug_enabled);
  151. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent3D::get_debug_enabled);
  152. ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent3D::set_debug_use_custom);
  153. ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent3D::get_debug_use_custom);
  154. ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent3D::set_debug_path_custom_color);
  155. ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent3D::get_debug_path_custom_color);
  156. ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent3D::set_debug_path_custom_point_size);
  157. ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent3D::get_debug_path_custom_point_size);
  158. ADD_GROUP("Debug", "debug_");
  159. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
  160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
  161. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
  162. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "0,50,0.01,or_greater,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
  163. }
  164. #ifndef DISABLE_DEPRECATED
  165. // Compatibility with Godot 4.0 beta 10 or below.
  166. // Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.
  167. bool NavigationAgent3D::_set(const StringName &p_name, const Variant &p_value) {
  168. if (p_name == "time_horizon") {
  169. set_time_horizon_agents(p_value);
  170. return true;
  171. }
  172. if (p_name == "target_location") {
  173. set_target_position(p_value);
  174. return true;
  175. }
  176. if (p_name == "agent_height_offset") {
  177. set_path_height_offset(p_value);
  178. return true;
  179. }
  180. return false;
  181. }
  182. bool NavigationAgent3D::_get(const StringName &p_name, Variant &r_ret) const {
  183. if (p_name == "time_horizon") {
  184. r_ret = get_time_horizon_agents();
  185. return true;
  186. }
  187. if (p_name == "target_location") {
  188. r_ret = get_target_position();
  189. return true;
  190. }
  191. if (p_name == "agent_height_offset") {
  192. r_ret = get_path_height_offset();
  193. return true;
  194. }
  195. return false;
  196. }
  197. #endif // DISABLE_DEPRECATED
  198. void NavigationAgent3D::_notification(int p_what) {
  199. switch (p_what) {
  200. case NOTIFICATION_POST_ENTER_TREE: {
  201. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  202. // cannot use READY as ready does not get called if Node is re-added to SceneTree
  203. set_agent_parent(get_parent());
  204. set_physics_process_internal(true);
  205. if (agent_parent && avoidance_enabled) {
  206. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  207. }
  208. #ifdef DEBUG_ENABLED
  209. if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
  210. debug_path_dirty = true;
  211. }
  212. #endif // DEBUG_ENABLED
  213. } break;
  214. case NOTIFICATION_PARENTED: {
  215. if (is_inside_tree() && (get_parent() != agent_parent)) {
  216. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  217. // PARENTED notification fires also when Node is added in scripts to a parent
  218. // this would spam transforms fails and world fails while Node is outside SceneTree
  219. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  220. set_agent_parent(get_parent());
  221. set_physics_process_internal(true);
  222. }
  223. } break;
  224. case NOTIFICATION_UNPARENTED: {
  225. // if agent has no parent no point in processing it until reparented
  226. set_agent_parent(nullptr);
  227. set_physics_process_internal(false);
  228. } break;
  229. case NOTIFICATION_EXIT_TREE: {
  230. set_agent_parent(nullptr);
  231. set_physics_process_internal(false);
  232. #ifdef DEBUG_ENABLED
  233. if (debug_path_instance.is_valid()) {
  234. RS::get_singleton()->instance_set_visible(debug_path_instance, false);
  235. }
  236. #endif // DEBUG_ENABLED
  237. } break;
  238. case NOTIFICATION_SUSPENDED:
  239. case NOTIFICATION_PAUSED: {
  240. if (agent_parent) {
  241. NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  242. }
  243. } break;
  244. case NOTIFICATION_UNSUSPENDED: {
  245. if (get_tree()->is_paused()) {
  246. break;
  247. }
  248. [[fallthrough]];
  249. }
  250. case NOTIFICATION_UNPAUSED: {
  251. if (agent_parent) {
  252. NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  253. }
  254. } break;
  255. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  256. if (agent_parent && avoidance_enabled) {
  257. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  258. }
  259. if (agent_parent && target_position_submitted) {
  260. if (velocity_submitted) {
  261. velocity_submitted = false;
  262. if (avoidance_enabled) {
  263. if (!use_3d_avoidance) {
  264. if (keep_y_velocity) {
  265. stored_y_velocity = velocity.y;
  266. }
  267. velocity.y = 0.0;
  268. }
  269. NavigationServer3D::get_singleton()->agent_set_velocity(agent, velocity);
  270. }
  271. }
  272. if (velocity_forced_submitted) {
  273. velocity_forced_submitted = false;
  274. if (avoidance_enabled) {
  275. NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);
  276. }
  277. }
  278. }
  279. #ifdef DEBUG_ENABLED
  280. if (debug_path_dirty) {
  281. _update_debug_path();
  282. }
  283. #endif // DEBUG_ENABLED
  284. } break;
  285. }
  286. }
  287. void NavigationAgent3D::_validate_property(PropertyInfo &p_property) const {
  288. if (p_property.name == "keep_y_velocity" && use_3d_avoidance) {
  289. p_property.usage = PROPERTY_USAGE_NONE;
  290. return;
  291. }
  292. }
  293. NavigationAgent3D::NavigationAgent3D() {
  294. agent = NavigationServer3D::get_singleton()->agent_create();
  295. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  296. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  297. NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  298. NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  299. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  300. NavigationServer3D::get_singleton()->agent_set_height(agent, height);
  301. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  302. NavigationServer3D::get_singleton()->agent_set_avoidance_layers(agent, avoidance_layers);
  303. NavigationServer3D::get_singleton()->agent_set_avoidance_mask(agent, avoidance_mask);
  304. NavigationServer3D::get_singleton()->agent_set_avoidance_priority(agent, avoidance_priority);
  305. NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
  306. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, avoidance_enabled);
  307. if (avoidance_enabled) {
  308. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  309. }
  310. // Preallocate query and result objects to improve performance.
  311. navigation_query = Ref<NavigationPathQueryParameters3D>();
  312. navigation_query.instantiate();
  313. navigation_result = Ref<NavigationPathQueryResult3D>();
  314. navigation_result.instantiate();
  315. #ifdef DEBUG_ENABLED
  316. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  317. #endif // DEBUG_ENABLED
  318. }
  319. NavigationAgent3D::~NavigationAgent3D() {
  320. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  321. NavigationServer3D::get_singleton()->free(agent);
  322. agent = RID(); // Pointless
  323. #ifdef DEBUG_ENABLED
  324. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  325. ERR_FAIL_NULL(RenderingServer::get_singleton());
  326. if (debug_path_instance.is_valid()) {
  327. RenderingServer::get_singleton()->free(debug_path_instance);
  328. }
  329. if (debug_path_mesh.is_valid()) {
  330. RenderingServer::get_singleton()->free(debug_path_mesh->get_rid());
  331. }
  332. #endif // DEBUG_ENABLED
  333. }
  334. void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
  335. if (avoidance_enabled == p_enabled) {
  336. return;
  337. }
  338. avoidance_enabled = p_enabled;
  339. if (avoidance_enabled) {
  340. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, true);
  341. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  342. } else {
  343. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, false);
  344. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  345. }
  346. }
  347. bool NavigationAgent3D::get_avoidance_enabled() const {
  348. return avoidance_enabled;
  349. }
  350. void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
  351. if (agent_parent == p_agent_parent) {
  352. return;
  353. }
  354. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  355. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  356. if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
  357. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  358. agent_parent = Object::cast_to<Node3D>(p_agent_parent);
  359. if (map_override.is_valid()) {
  360. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
  361. } else {
  362. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
  363. }
  364. // create new avoidance callback if enabled
  365. if (avoidance_enabled) {
  366. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  367. }
  368. } else {
  369. agent_parent = nullptr;
  370. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  371. }
  372. }
  373. void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
  374. if (navigation_layers == p_navigation_layers) {
  375. return;
  376. }
  377. navigation_layers = p_navigation_layers;
  378. if (target_position_submitted) {
  379. _request_repath();
  380. }
  381. }
  382. uint32_t NavigationAgent3D::get_navigation_layers() const {
  383. return navigation_layers;
  384. }
  385. void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  386. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  387. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  388. uint32_t _navigation_layers = get_navigation_layers();
  389. if (p_value) {
  390. _navigation_layers |= 1 << (p_layer_number - 1);
  391. } else {
  392. _navigation_layers &= ~(1 << (p_layer_number - 1));
  393. }
  394. set_navigation_layers(_navigation_layers);
  395. }
  396. bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
  397. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  398. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  399. return get_navigation_layers() & (1 << (p_layer_number - 1));
  400. }
  401. void NavigationAgent3D::set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm) {
  402. if (pathfinding_algorithm == p_pathfinding_algorithm) {
  403. return;
  404. }
  405. pathfinding_algorithm = p_pathfinding_algorithm;
  406. navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
  407. }
  408. void NavigationAgent3D::set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing) {
  409. if (path_postprocessing == p_path_postprocessing) {
  410. return;
  411. }
  412. path_postprocessing = p_path_postprocessing;
  413. navigation_query->set_path_postprocessing(path_postprocessing);
  414. }
  415. void NavigationAgent3D::set_simplify_path(bool p_enabled) {
  416. simplify_path = p_enabled;
  417. navigation_query->set_simplify_path(simplify_path);
  418. }
  419. bool NavigationAgent3D::get_simplify_path() const {
  420. return simplify_path;
  421. }
  422. void NavigationAgent3D::set_simplify_epsilon(real_t p_epsilon) {
  423. simplify_epsilon = MAX(0.0, p_epsilon);
  424. navigation_query->set_simplify_epsilon(simplify_epsilon);
  425. }
  426. real_t NavigationAgent3D::get_simplify_epsilon() const {
  427. return simplify_epsilon;
  428. }
  429. void NavigationAgent3D::set_path_return_max_length(float p_length) {
  430. path_return_max_length = MAX(0.0, p_length);
  431. navigation_query->set_path_return_max_length(path_return_max_length);
  432. }
  433. float NavigationAgent3D::get_path_return_max_length() const {
  434. return path_return_max_length;
  435. }
  436. void NavigationAgent3D::set_path_return_max_radius(float p_radius) {
  437. path_return_max_radius = MAX(0.0, p_radius);
  438. navigation_query->set_path_return_max_radius(path_return_max_radius);
  439. }
  440. float NavigationAgent3D::get_path_return_max_radius() const {
  441. return path_return_max_radius;
  442. }
  443. void NavigationAgent3D::set_path_search_max_polygons(int p_max_polygons) {
  444. path_search_max_polygons = p_max_polygons;
  445. navigation_query->set_path_search_max_polygons(path_search_max_polygons);
  446. }
  447. int NavigationAgent3D::get_path_search_max_polygons() const {
  448. return path_search_max_polygons;
  449. }
  450. void NavigationAgent3D::set_path_search_max_distance(float p_distance) {
  451. path_search_max_distance = MAX(0.0, p_distance);
  452. navigation_query->set_path_search_max_distance(path_search_max_distance);
  453. }
  454. float NavigationAgent3D::get_path_search_max_distance() const {
  455. return path_search_max_distance;
  456. }
  457. float NavigationAgent3D::get_path_length() const {
  458. return navigation_result->get_path_length();
  459. }
  460. void NavigationAgent3D::set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_path_metadata_flags) {
  461. if (path_metadata_flags == p_path_metadata_flags) {
  462. return;
  463. }
  464. path_metadata_flags = p_path_metadata_flags;
  465. }
  466. void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
  467. if (map_override == p_navigation_map) {
  468. return;
  469. }
  470. map_override = p_navigation_map;
  471. NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
  472. if (target_position_submitted) {
  473. _request_repath();
  474. }
  475. }
  476. RID NavigationAgent3D::get_navigation_map() const {
  477. if (map_override.is_valid()) {
  478. return map_override;
  479. } else if (agent_parent != nullptr) {
  480. return agent_parent->get_world_3d()->get_navigation_map();
  481. }
  482. return RID();
  483. }
  484. void NavigationAgent3D::set_path_desired_distance(real_t p_path_desired_distance) {
  485. if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
  486. return;
  487. }
  488. path_desired_distance = p_path_desired_distance;
  489. }
  490. void NavigationAgent3D::set_target_desired_distance(real_t p_target_desired_distance) {
  491. if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
  492. return;
  493. }
  494. target_desired_distance = p_target_desired_distance;
  495. }
  496. void NavigationAgent3D::set_radius(real_t p_radius) {
  497. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  498. if (Math::is_equal_approx(radius, p_radius)) {
  499. return;
  500. }
  501. radius = p_radius;
  502. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  503. }
  504. void NavigationAgent3D::set_height(real_t p_height) {
  505. ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive.");
  506. if (Math::is_equal_approx(height, p_height)) {
  507. return;
  508. }
  509. height = p_height;
  510. NavigationServer3D::get_singleton()->agent_set_height(agent, height);
  511. }
  512. void NavigationAgent3D::set_path_height_offset(real_t p_path_height_offset) {
  513. path_height_offset = p_path_height_offset;
  514. }
  515. void NavigationAgent3D::set_use_3d_avoidance(bool p_use_3d_avoidance) {
  516. use_3d_avoidance = p_use_3d_avoidance;
  517. NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
  518. notify_property_list_changed();
  519. }
  520. void NavigationAgent3D::set_keep_y_velocity(bool p_enabled) {
  521. keep_y_velocity = p_enabled;
  522. stored_y_velocity = 0.0;
  523. }
  524. bool NavigationAgent3D::get_keep_y_velocity() const {
  525. return keep_y_velocity;
  526. }
  527. void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
  528. if (Math::is_equal_approx(neighbor_distance, p_distance)) {
  529. return;
  530. }
  531. neighbor_distance = p_distance;
  532. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  533. }
  534. void NavigationAgent3D::set_max_neighbors(int p_count) {
  535. if (max_neighbors == p_count) {
  536. return;
  537. }
  538. max_neighbors = p_count;
  539. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  540. }
  541. void NavigationAgent3D::set_time_horizon_agents(real_t p_time_horizon) {
  542. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  543. if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {
  544. return;
  545. }
  546. time_horizon_agents = p_time_horizon;
  547. NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  548. }
  549. void NavigationAgent3D::set_time_horizon_obstacles(real_t p_time_horizon) {
  550. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  551. if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {
  552. return;
  553. }
  554. time_horizon_obstacles = p_time_horizon;
  555. NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  556. }
  557. void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
  558. ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");
  559. if (Math::is_equal_approx(max_speed, p_max_speed)) {
  560. return;
  561. }
  562. max_speed = p_max_speed;
  563. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  564. }
  565. void NavigationAgent3D::set_path_max_distance(real_t p_path_max_distance) {
  566. if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
  567. return;
  568. }
  569. path_max_distance = p_path_max_distance;
  570. }
  571. real_t NavigationAgent3D::get_path_max_distance() {
  572. return path_max_distance;
  573. }
  574. void NavigationAgent3D::set_target_position(Vector3 p_position) {
  575. // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed.
  576. // Revisit later when the navigation server can update the path without requesting a new path.
  577. target_position = p_position;
  578. target_position_submitted = true;
  579. _request_repath();
  580. }
  581. Vector3 NavigationAgent3D::get_target_position() const {
  582. return target_position;
  583. }
  584. Vector3 NavigationAgent3D::get_next_path_position() {
  585. _update_navigation();
  586. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  587. if (navigation_path.is_empty()) {
  588. ERR_FAIL_NULL_V_MSG(agent_parent, Vector3(), "The agent has no parent.");
  589. return agent_parent->get_global_position();
  590. } else {
  591. return navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
  592. }
  593. }
  594. real_t NavigationAgent3D::distance_to_target() const {
  595. ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");
  596. return agent_parent->get_global_position().distance_to(target_position);
  597. }
  598. bool NavigationAgent3D::is_target_reached() const {
  599. return target_reached;
  600. }
  601. bool NavigationAgent3D::is_target_reachable() {
  602. _update_navigation();
  603. return _is_target_reachable();
  604. }
  605. bool NavigationAgent3D::_is_target_reachable() const {
  606. return target_desired_distance >= _get_final_position().distance_to(target_position);
  607. }
  608. bool NavigationAgent3D::is_navigation_finished() {
  609. _update_navigation();
  610. return navigation_finished;
  611. }
  612. Vector3 NavigationAgent3D::get_final_position() {
  613. _update_navigation();
  614. return _get_final_position();
  615. }
  616. Vector3 NavigationAgent3D::_get_final_position() const {
  617. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  618. if (navigation_path.is_empty()) {
  619. return Vector3();
  620. }
  621. return navigation_path[navigation_path.size() - 1] - Vector3(0, path_height_offset, 0);
  622. }
  623. void NavigationAgent3D::set_velocity_forced(Vector3 p_velocity) {
  624. // Intentionally not checking for equality of the parameter.
  625. // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
  626. // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
  627. velocity_forced = p_velocity;
  628. velocity_forced_submitted = true;
  629. }
  630. void NavigationAgent3D::set_velocity(const Vector3 p_velocity) {
  631. velocity = p_velocity;
  632. velocity_submitted = true;
  633. }
  634. void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
  635. safe_velocity = p_new_velocity;
  636. if (!use_3d_avoidance) {
  637. safe_velocity.y = stored_y_velocity;
  638. }
  639. emit_signal(SNAME("velocity_computed"), safe_velocity);
  640. }
  641. PackedStringArray NavigationAgent3D::get_configuration_warnings() const {
  642. PackedStringArray warnings = Node::get_configuration_warnings();
  643. if (!Object::cast_to<Node3D>(get_parent())) {
  644. warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
  645. }
  646. return warnings;
  647. }
  648. void NavigationAgent3D::_update_navigation() {
  649. if (agent_parent == nullptr) {
  650. return;
  651. }
  652. if (!agent_parent->is_inside_tree()) {
  653. return;
  654. }
  655. if (!target_position_submitted) {
  656. return;
  657. }
  658. Vector3 origin = agent_parent->get_global_position();
  659. bool reload_path = false;
  660. if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
  661. reload_path = true;
  662. } else if (navigation_result->get_path().is_empty()) {
  663. reload_path = true;
  664. } else {
  665. // Check if too far from the navigation path
  666. if (navigation_path_index > 0) {
  667. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  668. Vector3 segment_a = navigation_path[navigation_path_index - 1];
  669. Vector3 segment_b = navigation_path[navigation_path_index];
  670. segment_a.y -= path_height_offset;
  671. segment_b.y -= path_height_offset;
  672. Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment_a, segment_b);
  673. if (origin.distance_to(p) >= path_max_distance) {
  674. // To faraway, reload path
  675. reload_path = true;
  676. }
  677. }
  678. }
  679. if (reload_path) {
  680. navigation_query->set_start_position(origin);
  681. navigation_query->set_target_position(target_position);
  682. navigation_query->set_navigation_layers(navigation_layers);
  683. navigation_query->set_metadata_flags(path_metadata_flags);
  684. if (map_override.is_valid()) {
  685. navigation_query->set_map(map_override);
  686. } else {
  687. navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
  688. }
  689. NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
  690. #ifdef DEBUG_ENABLED
  691. debug_path_dirty = true;
  692. #endif // DEBUG_ENABLED
  693. navigation_finished = false;
  694. last_waypoint_reached = false;
  695. navigation_path_index = 0;
  696. emit_signal(SNAME("path_changed"));
  697. }
  698. if (navigation_result->get_path().is_empty()) {
  699. return;
  700. }
  701. // Check if the navigation has already finished.
  702. if (navigation_finished) {
  703. return;
  704. }
  705. // Check if we reached the target.
  706. if (_is_within_target_distance(origin)) {
  707. // Emit waypoint_reached in case we also moved within distance of a waypoint.
  708. _advance_waypoints(origin);
  709. _transition_to_target_reached();
  710. _transition_to_navigation_finished();
  711. } else {
  712. // Advance waypoints if possible.
  713. _advance_waypoints(origin);
  714. // Keep navigation running even after reaching the last waypoint if the target is reachable.
  715. if (last_waypoint_reached && !_is_target_reachable()) {
  716. _transition_to_navigation_finished();
  717. }
  718. }
  719. }
  720. void NavigationAgent3D::_advance_waypoints(const Vector3 &p_origin) {
  721. if (last_waypoint_reached) {
  722. return;
  723. }
  724. // Advance to the farthest possible waypoint.
  725. while (_is_within_waypoint_distance(p_origin)) {
  726. _trigger_waypoint_reached();
  727. if (_is_last_waypoint()) {
  728. last_waypoint_reached = true;
  729. break;
  730. }
  731. _move_to_next_waypoint();
  732. }
  733. }
  734. void NavigationAgent3D::_request_repath() {
  735. navigation_result->reset();
  736. target_reached = false;
  737. navigation_finished = false;
  738. last_waypoint_reached = false;
  739. }
  740. bool NavigationAgent3D::_is_last_waypoint() const {
  741. return navigation_path_index == navigation_result->get_path().size() - 1;
  742. }
  743. void NavigationAgent3D::_move_to_next_waypoint() {
  744. navigation_path_index += 1;
  745. }
  746. bool NavigationAgent3D::_is_within_waypoint_distance(const Vector3 &p_origin) const {
  747. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  748. Vector3 waypoint = navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
  749. return p_origin.distance_to(waypoint) < path_desired_distance;
  750. }
  751. bool NavigationAgent3D::_is_within_target_distance(const Vector3 &p_origin) const {
  752. return p_origin.distance_to(target_position) < target_desired_distance;
  753. }
  754. void NavigationAgent3D::_trigger_waypoint_reached() {
  755. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  756. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  757. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  758. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  759. Dictionary details;
  760. const Vector3 waypoint = navigation_path[navigation_path_index];
  761. details[CoreStringName(position)] = waypoint;
  762. int waypoint_type = -1;
  763. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  764. const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
  765. details[SNAME("type")] = type;
  766. waypoint_type = type;
  767. }
  768. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  769. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  770. }
  771. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  772. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  773. // Get a reference to the owning object.
  774. Object *owner = nullptr;
  775. if (waypoint_owner_id.is_valid()) {
  776. owner = ObjectDB::get_instance(waypoint_owner_id);
  777. }
  778. details[SNAME("owner")] = owner;
  779. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  780. const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
  781. if (navlink) {
  782. Vector3 link_global_start_position = navlink->get_global_start_position();
  783. Vector3 link_global_end_position = navlink->get_global_end_position();
  784. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  785. details[SNAME("link_entry_position")] = link_global_start_position;
  786. details[SNAME("link_exit_position")] = link_global_end_position;
  787. } else {
  788. details[SNAME("link_entry_position")] = link_global_end_position;
  789. details[SNAME("link_exit_position")] = link_global_start_position;
  790. }
  791. }
  792. }
  793. }
  794. // Emit a signal for the waypoint.
  795. emit_signal(SNAME("waypoint_reached"), details);
  796. // Emit a signal if we've reached a navigation link.
  797. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  798. emit_signal(SNAME("link_reached"), details);
  799. }
  800. }
  801. void NavigationAgent3D::_transition_to_navigation_finished() {
  802. navigation_finished = true;
  803. target_position_submitted = false;
  804. if (avoidance_enabled) {
  805. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  806. NavigationServer3D::get_singleton()->agent_set_velocity(agent, Vector3(0.0, 0.0, 0.0));
  807. NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, Vector3(0.0, 0.0, 0.0));
  808. stored_y_velocity = 0.0;
  809. }
  810. emit_signal(SNAME("navigation_finished"));
  811. }
  812. void NavigationAgent3D::_transition_to_target_reached() {
  813. target_reached = true;
  814. emit_signal(SNAME("target_reached"));
  815. }
  816. void NavigationAgent3D::set_avoidance_layers(uint32_t p_layers) {
  817. avoidance_layers = p_layers;
  818. NavigationServer3D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
  819. }
  820. uint32_t NavigationAgent3D::get_avoidance_layers() const {
  821. return avoidance_layers;
  822. }
  823. void NavigationAgent3D::set_avoidance_mask(uint32_t p_mask) {
  824. avoidance_mask = p_mask;
  825. NavigationServer3D::get_singleton()->agent_set_avoidance_mask(get_rid(), avoidance_mask);
  826. }
  827. uint32_t NavigationAgent3D::get_avoidance_mask() const {
  828. return avoidance_mask;
  829. }
  830. void NavigationAgent3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  831. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  832. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  833. uint32_t avoidance_layers_new = get_avoidance_layers();
  834. if (p_value) {
  835. avoidance_layers_new |= 1 << (p_layer_number - 1);
  836. } else {
  837. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  838. }
  839. set_avoidance_layers(avoidance_layers_new);
  840. }
  841. bool NavigationAgent3D::get_avoidance_layer_value(int p_layer_number) const {
  842. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  843. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  844. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  845. }
  846. void NavigationAgent3D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
  847. ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
  848. ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
  849. uint32_t mask = get_avoidance_mask();
  850. if (p_value) {
  851. mask |= 1 << (p_mask_number - 1);
  852. } else {
  853. mask &= ~(1 << (p_mask_number - 1));
  854. }
  855. set_avoidance_mask(mask);
  856. }
  857. bool NavigationAgent3D::get_avoidance_mask_value(int p_mask_number) const {
  858. ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  859. ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  860. return get_avoidance_mask() & (1 << (p_mask_number - 1));
  861. }
  862. void NavigationAgent3D::set_avoidance_priority(real_t p_priority) {
  863. ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  864. ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  865. avoidance_priority = p_priority;
  866. NavigationServer3D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
  867. }
  868. real_t NavigationAgent3D::get_avoidance_priority() const {
  869. return avoidance_priority;
  870. }
  871. ////////DEBUG////////////////////////////////////////////////////////////
  872. void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
  873. #ifdef DEBUG_ENABLED
  874. if (debug_enabled == p_enabled) {
  875. return;
  876. }
  877. debug_enabled = p_enabled;
  878. debug_path_dirty = true;
  879. #endif // DEBUG_ENABLED
  880. }
  881. bool NavigationAgent3D::get_debug_enabled() const {
  882. return debug_enabled;
  883. }
  884. void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
  885. #ifdef DEBUG_ENABLED
  886. if (debug_use_custom == p_enabled) {
  887. return;
  888. }
  889. debug_use_custom = p_enabled;
  890. debug_path_dirty = true;
  891. #endif // DEBUG_ENABLED
  892. }
  893. bool NavigationAgent3D::get_debug_use_custom() const {
  894. return debug_use_custom;
  895. }
  896. void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
  897. #ifdef DEBUG_ENABLED
  898. if (debug_path_custom_color == p_color) {
  899. return;
  900. }
  901. debug_path_custom_color = p_color;
  902. debug_path_dirty = true;
  903. #endif // DEBUG_ENABLED
  904. }
  905. Color NavigationAgent3D::get_debug_path_custom_color() const {
  906. return debug_path_custom_color;
  907. }
  908. void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
  909. #ifdef DEBUG_ENABLED
  910. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  911. return;
  912. }
  913. debug_path_custom_point_size = MAX(0.0, p_point_size);
  914. debug_path_dirty = true;
  915. #endif // DEBUG_ENABLED
  916. }
  917. float NavigationAgent3D::get_debug_path_custom_point_size() const {
  918. return debug_path_custom_point_size;
  919. }
  920. #ifdef DEBUG_ENABLED
  921. void NavigationAgent3D::_navigation_debug_changed() {
  922. debug_path_dirty = true;
  923. }
  924. void NavigationAgent3D::_update_debug_path() {
  925. if (!debug_path_dirty) {
  926. return;
  927. }
  928. debug_path_dirty = false;
  929. if (!debug_path_instance.is_valid()) {
  930. debug_path_instance = RenderingServer::get_singleton()->instance_create();
  931. }
  932. if (debug_path_mesh.is_null()) {
  933. debug_path_mesh.instantiate();
  934. }
  935. debug_path_mesh->clear_surfaces();
  936. if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  937. return;
  938. }
  939. if (!(agent_parent && agent_parent->is_inside_tree())) {
  940. return;
  941. }
  942. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  943. if (navigation_path.size() <= 1) {
  944. return;
  945. }
  946. Vector<Vector3> debug_path_lines_vertex_array;
  947. for (int i = 0; i < navigation_path.size() - 1; i++) {
  948. debug_path_lines_vertex_array.push_back(navigation_path[i]);
  949. debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
  950. }
  951. Array debug_path_lines_mesh_array;
  952. debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  953. debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
  954. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
  955. Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
  956. if (debug_use_custom) {
  957. if (debug_agent_path_line_custom_material.is_null()) {
  958. debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
  959. }
  960. debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
  961. debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
  962. } else {
  963. debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
  964. }
  965. if (debug_path_custom_point_size > 0.0) {
  966. Vector<Vector3> debug_path_points_vertex_array;
  967. for (int i = 0; i < navigation_path.size(); i++) {
  968. debug_path_points_vertex_array.push_back(navigation_path[i]);
  969. }
  970. Array debug_path_points_mesh_array;
  971. debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
  972. debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
  973. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
  974. Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
  975. if (debug_use_custom) {
  976. if (debug_agent_path_point_custom_material.is_null()) {
  977. debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
  978. }
  979. debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
  980. debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
  981. debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
  982. } else {
  983. debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
  984. }
  985. }
  986. RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
  987. RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
  988. RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  989. }
  990. #endif // DEBUG_ENABLED