navigation_agent_3d.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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_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_agent_height_offset", "agent_height_offset"), &NavigationAgent3D::set_agent_height_offset);
  44. ClassDB::bind_method(D_METHOD("get_agent_height_offset"), &NavigationAgent3D::get_agent_height_offset);
  45. ClassDB::bind_method(D_METHOD("set_ignore_y", "ignore"), &NavigationAgent3D::set_ignore_y);
  46. ClassDB::bind_method(D_METHOD("get_ignore_y"), &NavigationAgent3D::get_ignore_y);
  47. ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
  48. ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
  49. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
  50. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
  51. ClassDB::bind_method(D_METHOD("set_time_horizon", "time_horizon"), &NavigationAgent3D::set_time_horizon);
  52. ClassDB::bind_method(D_METHOD("get_time_horizon"), &NavigationAgent3D::get_time_horizon);
  53. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
  54. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
  55. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
  56. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
  57. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
  58. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
  59. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
  60. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
  61. ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent3D::set_pathfinding_algorithm);
  62. ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent3D::get_pathfinding_algorithm);
  63. ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent3D::set_path_postprocessing);
  64. ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent3D::get_path_postprocessing);
  65. ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent3D::set_path_metadata_flags);
  66. ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent3D::get_path_metadata_flags);
  67. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
  68. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
  69. ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent3D::set_target_position);
  70. ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent3D::get_target_position);
  71. ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent3D::get_next_path_position);
  72. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
  73. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
  74. ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent3D::get_current_navigation_result);
  75. ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent3D::get_current_navigation_path);
  76. ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent3D::get_current_navigation_path_index);
  77. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
  78. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
  79. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
  80. ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent3D::get_final_position);
  81. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
  82. ADD_GROUP("Pathfinding", "");
  83. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
  84. 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");
  85. 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");
  86. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01,or_greater,suffix:m"), "set_agent_height_offset", "get_agent_height_offset");
  87. 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");
  88. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  89. ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
  90. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered"), "set_path_postprocessing", "get_path_postprocessing");
  91. 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");
  92. ADD_GROUP("Avoidance", "");
  93. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  94. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
  95. 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");
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
  97. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater,suffix:s"), "set_time_horizon", "get_time_horizon");
  98. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.1,1000,0.01,or_greater,suffix:m/s"), "set_max_speed", "get_max_speed");
  99. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_y"), "set_ignore_y", "get_ignore_y");
  100. ADD_SIGNAL(MethodInfo("path_changed"));
  101. ADD_SIGNAL(MethodInfo("target_reached"));
  102. ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  103. ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  104. ADD_SIGNAL(MethodInfo("navigation_finished"));
  105. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
  106. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent3D::set_debug_enabled);
  107. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent3D::get_debug_enabled);
  108. ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent3D::set_debug_use_custom);
  109. ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent3D::get_debug_use_custom);
  110. ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent3D::set_debug_path_custom_color);
  111. ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent3D::get_debug_path_custom_color);
  112. ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent3D::set_debug_path_custom_point_size);
  113. ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent3D::get_debug_path_custom_point_size);
  114. ADD_GROUP("Debug", "debug_");
  115. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
  116. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
  117. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
  118. 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");
  119. }
  120. void NavigationAgent3D::_notification(int p_what) {
  121. switch (p_what) {
  122. case NOTIFICATION_POST_ENTER_TREE: {
  123. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  124. // cannot use READY as ready does not get called if Node is re-added to SceneTree
  125. set_agent_parent(get_parent());
  126. set_physics_process_internal(true);
  127. #ifdef DEBUG_ENABLED
  128. if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
  129. debug_path_dirty = true;
  130. }
  131. #endif // DEBUG_ENABLED
  132. } break;
  133. case NOTIFICATION_PARENTED: {
  134. if (is_inside_tree() && (get_parent() != agent_parent)) {
  135. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  136. // PARENTED notification fires also when Node is added in scripts to a parent
  137. // this would spam transforms fails and world fails while Node is outside SceneTree
  138. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  139. set_agent_parent(get_parent());
  140. set_physics_process_internal(true);
  141. }
  142. } break;
  143. case NOTIFICATION_UNPARENTED: {
  144. // if agent has no parent no point in processing it until reparented
  145. set_agent_parent(nullptr);
  146. set_physics_process_internal(false);
  147. } break;
  148. case NOTIFICATION_EXIT_TREE: {
  149. set_agent_parent(nullptr);
  150. set_physics_process_internal(false);
  151. #ifdef DEBUG_ENABLED
  152. if (debug_path_instance.is_valid()) {
  153. RS::get_singleton()->instance_set_visible(debug_path_instance, false);
  154. }
  155. #endif // DEBUG_ENABLED
  156. } break;
  157. case NOTIFICATION_PAUSED: {
  158. if (agent_parent && !agent_parent->can_process()) {
  159. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  160. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  161. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  162. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  163. map_before_pause = RID();
  164. }
  165. } break;
  166. case NOTIFICATION_UNPAUSED: {
  167. if (agent_parent && !agent_parent->can_process()) {
  168. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  169. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  170. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  171. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  172. map_before_pause = RID();
  173. }
  174. } break;
  175. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  176. if (agent_parent && target_position_submitted) {
  177. if (avoidance_enabled) {
  178. // agent_position on NavigationServer is avoidance only and has nothing to do with pathfinding
  179. // no point in flooding NavigationServer queue with agent position updates that get send to the void if avoidance is not used
  180. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  181. }
  182. _check_distance_to_target();
  183. }
  184. #ifdef DEBUG_ENABLED
  185. if (debug_path_dirty) {
  186. _update_debug_path();
  187. }
  188. #endif // DEBUG_ENABLED
  189. } break;
  190. }
  191. }
  192. NavigationAgent3D::NavigationAgent3D() {
  193. agent = NavigationServer3D::get_singleton()->agent_create();
  194. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  195. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  196. NavigationServer3D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  197. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  198. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  199. NavigationServer3D::get_singleton()->agent_set_ignore_y(agent, ignore_y);
  200. // Preallocate query and result objects to improve performance.
  201. navigation_query = Ref<NavigationPathQueryParameters3D>();
  202. navigation_query.instantiate();
  203. navigation_result = Ref<NavigationPathQueryResult3D>();
  204. navigation_result.instantiate();
  205. #ifdef DEBUG_ENABLED
  206. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  207. #endif // DEBUG_ENABLED
  208. }
  209. NavigationAgent3D::~NavigationAgent3D() {
  210. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  211. NavigationServer3D::get_singleton()->free(agent);
  212. agent = RID(); // Pointless
  213. #ifdef DEBUG_ENABLED
  214. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  215. ERR_FAIL_NULL(RenderingServer::get_singleton());
  216. if (debug_path_instance.is_valid()) {
  217. RenderingServer::get_singleton()->free(debug_path_instance);
  218. }
  219. if (debug_path_mesh.is_valid()) {
  220. RenderingServer::get_singleton()->free(debug_path_mesh->get_rid());
  221. }
  222. #endif // DEBUG_ENABLED
  223. }
  224. void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
  225. if (avoidance_enabled == p_enabled) {
  226. return;
  227. }
  228. avoidance_enabled = p_enabled;
  229. if (avoidance_enabled) {
  230. NavigationServer3D::get_singleton()->agent_set_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  231. } else {
  232. NavigationServer3D::get_singleton()->agent_set_callback(agent, Callable());
  233. }
  234. }
  235. bool NavigationAgent3D::get_avoidance_enabled() const {
  236. return avoidance_enabled;
  237. }
  238. void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
  239. if (agent_parent == p_agent_parent) {
  240. return;
  241. }
  242. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  243. NavigationServer3D::get_singleton()->agent_set_callback(agent, Callable());
  244. if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
  245. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  246. agent_parent = Object::cast_to<Node3D>(p_agent_parent);
  247. if (map_override.is_valid()) {
  248. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
  249. } else {
  250. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
  251. }
  252. // create new avoidance callback if enabled
  253. if (avoidance_enabled) {
  254. NavigationServer3D::get_singleton()->agent_set_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  255. }
  256. } else {
  257. agent_parent = nullptr;
  258. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  259. }
  260. }
  261. void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
  262. if (navigation_layers == p_navigation_layers) {
  263. return;
  264. }
  265. navigation_layers = p_navigation_layers;
  266. _request_repath();
  267. }
  268. uint32_t NavigationAgent3D::get_navigation_layers() const {
  269. return navigation_layers;
  270. }
  271. void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  272. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  273. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  274. uint32_t _navigation_layers = get_navigation_layers();
  275. if (p_value) {
  276. _navigation_layers |= 1 << (p_layer_number - 1);
  277. } else {
  278. _navigation_layers &= ~(1 << (p_layer_number - 1));
  279. }
  280. set_navigation_layers(_navigation_layers);
  281. }
  282. bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
  283. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  284. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  285. return get_navigation_layers() & (1 << (p_layer_number - 1));
  286. }
  287. void NavigationAgent3D::set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm) {
  288. if (pathfinding_algorithm == p_pathfinding_algorithm) {
  289. return;
  290. }
  291. pathfinding_algorithm = p_pathfinding_algorithm;
  292. navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
  293. }
  294. void NavigationAgent3D::set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing) {
  295. if (path_postprocessing == p_path_postprocessing) {
  296. return;
  297. }
  298. path_postprocessing = p_path_postprocessing;
  299. navigation_query->set_path_postprocessing(path_postprocessing);
  300. }
  301. void NavigationAgent3D::set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_path_metadata_flags) {
  302. if (path_metadata_flags == p_path_metadata_flags) {
  303. return;
  304. }
  305. path_metadata_flags = p_path_metadata_flags;
  306. }
  307. void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
  308. if (map_override == p_navigation_map) {
  309. return;
  310. }
  311. map_override = p_navigation_map;
  312. NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
  313. _request_repath();
  314. }
  315. RID NavigationAgent3D::get_navigation_map() const {
  316. if (map_override.is_valid()) {
  317. return map_override;
  318. } else if (agent_parent != nullptr) {
  319. return agent_parent->get_world_3d()->get_navigation_map();
  320. }
  321. return RID();
  322. }
  323. void NavigationAgent3D::set_path_desired_distance(real_t p_path_desired_distance) {
  324. if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
  325. return;
  326. }
  327. path_desired_distance = p_path_desired_distance;
  328. }
  329. void NavigationAgent3D::set_target_desired_distance(real_t p_target_desired_distance) {
  330. if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
  331. return;
  332. }
  333. target_desired_distance = p_target_desired_distance;
  334. }
  335. void NavigationAgent3D::set_radius(real_t p_radius) {
  336. if (Math::is_equal_approx(radius, p_radius)) {
  337. return;
  338. }
  339. radius = p_radius;
  340. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  341. }
  342. void NavigationAgent3D::set_agent_height_offset(real_t p_agent_height_offset) {
  343. if (Math::is_equal_approx(navigation_height_offset, p_agent_height_offset)) {
  344. return;
  345. }
  346. navigation_height_offset = p_agent_height_offset;
  347. }
  348. void NavigationAgent3D::set_ignore_y(bool p_ignore_y) {
  349. if (ignore_y == p_ignore_y) {
  350. return;
  351. }
  352. ignore_y = p_ignore_y;
  353. NavigationServer3D::get_singleton()->agent_set_ignore_y(agent, ignore_y);
  354. }
  355. void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
  356. if (Math::is_equal_approx(neighbor_distance, p_distance)) {
  357. return;
  358. }
  359. neighbor_distance = p_distance;
  360. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  361. }
  362. void NavigationAgent3D::set_max_neighbors(int p_count) {
  363. if (max_neighbors == p_count) {
  364. return;
  365. }
  366. max_neighbors = p_count;
  367. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  368. }
  369. void NavigationAgent3D::set_time_horizon(real_t p_time) {
  370. if (Math::is_equal_approx(time_horizon, p_time)) {
  371. return;
  372. }
  373. time_horizon = p_time;
  374. NavigationServer3D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  375. }
  376. void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
  377. if (Math::is_equal_approx(max_speed, p_max_speed)) {
  378. return;
  379. }
  380. max_speed = p_max_speed;
  381. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  382. }
  383. void NavigationAgent3D::set_path_max_distance(real_t p_path_max_distance) {
  384. if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
  385. return;
  386. }
  387. path_max_distance = p_path_max_distance;
  388. }
  389. real_t NavigationAgent3D::get_path_max_distance() {
  390. return path_max_distance;
  391. }
  392. void NavigationAgent3D::set_target_position(Vector3 p_position) {
  393. // 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.
  394. // Revisit later when the navigation server can update the path without requesting a new path.
  395. target_position = p_position;
  396. target_position_submitted = true;
  397. _request_repath();
  398. }
  399. Vector3 NavigationAgent3D::get_target_position() const {
  400. return target_position;
  401. }
  402. Vector3 NavigationAgent3D::get_next_path_position() {
  403. update_navigation();
  404. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  405. if (navigation_path.size() == 0) {
  406. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector3(), "The agent has no parent.");
  407. return agent_parent->get_global_position();
  408. } else {
  409. return navigation_path[navigation_path_index] - Vector3(0, navigation_height_offset, 0);
  410. }
  411. }
  412. real_t NavigationAgent3D::distance_to_target() const {
  413. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, 0.0, "The agent has no parent.");
  414. return agent_parent->get_global_position().distance_to(target_position);
  415. }
  416. bool NavigationAgent3D::is_target_reached() const {
  417. return target_reached;
  418. }
  419. bool NavigationAgent3D::is_target_reachable() {
  420. return target_desired_distance >= get_final_position().distance_to(target_position);
  421. }
  422. bool NavigationAgent3D::is_navigation_finished() {
  423. update_navigation();
  424. return navigation_finished;
  425. }
  426. Vector3 NavigationAgent3D::get_final_position() {
  427. update_navigation();
  428. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  429. if (navigation_path.size() == 0) {
  430. return Vector3();
  431. }
  432. return navigation_path[navigation_path.size() - 1];
  433. }
  434. void NavigationAgent3D::set_velocity(Vector3 p_velocity) {
  435. // Intentionally not checking for equality of the parameter.
  436. // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
  437. // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
  438. target_velocity = p_velocity;
  439. velocity_submitted = true;
  440. NavigationServer3D::get_singleton()->agent_set_target_velocity(agent, target_velocity);
  441. NavigationServer3D::get_singleton()->agent_set_velocity(agent, prev_safe_velocity);
  442. }
  443. void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
  444. prev_safe_velocity = p_new_velocity;
  445. if (!velocity_submitted) {
  446. target_velocity = Vector3();
  447. return;
  448. }
  449. velocity_submitted = false;
  450. emit_signal(SNAME("velocity_computed"), p_new_velocity);
  451. }
  452. PackedStringArray NavigationAgent3D::get_configuration_warnings() const {
  453. PackedStringArray warnings = Node::get_configuration_warnings();
  454. if (!Object::cast_to<Node3D>(get_parent())) {
  455. warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
  456. }
  457. return warnings;
  458. }
  459. void NavigationAgent3D::update_navigation() {
  460. if (agent_parent == nullptr) {
  461. return;
  462. }
  463. if (!agent_parent->is_inside_tree()) {
  464. return;
  465. }
  466. if (!target_position_submitted) {
  467. return;
  468. }
  469. if (update_frame_id == Engine::get_singleton()->get_physics_frames()) {
  470. return;
  471. }
  472. update_frame_id = Engine::get_singleton()->get_physics_frames();
  473. Vector3 origin = agent_parent->get_global_position();
  474. bool reload_path = false;
  475. if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
  476. reload_path = true;
  477. } else if (navigation_result->get_path().size() == 0) {
  478. reload_path = true;
  479. } else {
  480. // Check if too far from the navigation path
  481. if (navigation_path_index > 0) {
  482. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  483. Vector3 segment[2];
  484. segment[0] = navigation_path[navigation_path_index - 1];
  485. segment[1] = navigation_path[navigation_path_index];
  486. segment[0].y -= navigation_height_offset;
  487. segment[1].y -= navigation_height_offset;
  488. Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment);
  489. if (origin.distance_to(p) >= path_max_distance) {
  490. // To faraway, reload path
  491. reload_path = true;
  492. }
  493. }
  494. }
  495. if (reload_path) {
  496. navigation_query->set_start_position(origin);
  497. navigation_query->set_target_position(target_position);
  498. navigation_query->set_navigation_layers(navigation_layers);
  499. navigation_query->set_metadata_flags(path_metadata_flags);
  500. if (map_override.is_valid()) {
  501. navigation_query->set_map(map_override);
  502. } else {
  503. navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
  504. }
  505. NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
  506. #ifdef DEBUG_ENABLED
  507. debug_path_dirty = true;
  508. #endif // DEBUG_ENABLED
  509. navigation_finished = false;
  510. navigation_path_index = 0;
  511. emit_signal(SNAME("path_changed"));
  512. }
  513. if (navigation_result->get_path().size() == 0) {
  514. return;
  515. }
  516. // Check if we can advance the navigation path
  517. if (navigation_finished == false) {
  518. // Advances to the next far away position.
  519. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  520. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  521. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  522. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  523. while (origin.distance_to(navigation_path[navigation_path_index] - Vector3(0, navigation_height_offset, 0)) < path_desired_distance) {
  524. Dictionary details;
  525. const Vector3 waypoint = navigation_path[navigation_path_index];
  526. details[SNAME("position")] = waypoint;
  527. int waypoint_type = -1;
  528. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  529. const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
  530. details[SNAME("type")] = type;
  531. waypoint_type = type;
  532. }
  533. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  534. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  535. }
  536. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  537. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  538. // Get a reference to the owning object.
  539. Object *owner = nullptr;
  540. if (waypoint_owner_id.is_valid()) {
  541. owner = ObjectDB::get_instance(waypoint_owner_id);
  542. }
  543. details[SNAME("owner")] = owner;
  544. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  545. const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
  546. if (navlink) {
  547. Vector3 link_global_start_position = navlink->get_global_start_position();
  548. Vector3 link_global_end_position = navlink->get_global_end_position();
  549. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  550. details[SNAME("link_entry_position")] = link_global_start_position;
  551. details[SNAME("link_exit_position")] = link_global_end_position;
  552. } else {
  553. details[SNAME("link_entry_position")] = link_global_end_position;
  554. details[SNAME("link_exit_position")] = link_global_start_position;
  555. }
  556. }
  557. }
  558. }
  559. // Emit a signal for the waypoint
  560. emit_signal(SNAME("waypoint_reached"), details);
  561. // Emit a signal if we've reached a navigation link
  562. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  563. emit_signal(SNAME("link_reached"), details);
  564. }
  565. // Move to the next waypoint on the list
  566. navigation_path_index += 1;
  567. // Check to see if we've finished our route
  568. if (navigation_path_index == navigation_path.size()) {
  569. _check_distance_to_target();
  570. navigation_path_index -= 1;
  571. navigation_finished = true;
  572. target_position_submitted = false;
  573. emit_signal(SNAME("navigation_finished"));
  574. break;
  575. }
  576. }
  577. }
  578. }
  579. void NavigationAgent3D::_request_repath() {
  580. navigation_result->reset();
  581. target_reached = false;
  582. navigation_finished = false;
  583. update_frame_id = 0;
  584. }
  585. void NavigationAgent3D::_check_distance_to_target() {
  586. if (!target_reached) {
  587. if (distance_to_target() < target_desired_distance) {
  588. target_reached = true;
  589. emit_signal(SNAME("target_reached"));
  590. }
  591. }
  592. }
  593. ////////DEBUG////////////////////////////////////////////////////////////
  594. void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
  595. #ifdef DEBUG_ENABLED
  596. if (debug_enabled == p_enabled) {
  597. return;
  598. }
  599. debug_enabled = p_enabled;
  600. debug_path_dirty = true;
  601. #endif // DEBUG_ENABLED
  602. }
  603. bool NavigationAgent3D::get_debug_enabled() const {
  604. return debug_enabled;
  605. }
  606. void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
  607. #ifdef DEBUG_ENABLED
  608. if (debug_use_custom == p_enabled) {
  609. return;
  610. }
  611. debug_use_custom = p_enabled;
  612. debug_path_dirty = true;
  613. #endif // DEBUG_ENABLED
  614. }
  615. bool NavigationAgent3D::get_debug_use_custom() const {
  616. return debug_use_custom;
  617. }
  618. void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
  619. #ifdef DEBUG_ENABLED
  620. if (debug_path_custom_color == p_color) {
  621. return;
  622. }
  623. debug_path_custom_color = p_color;
  624. debug_path_dirty = true;
  625. #endif // DEBUG_ENABLED
  626. }
  627. Color NavigationAgent3D::get_debug_path_custom_color() const {
  628. return debug_path_custom_color;
  629. }
  630. void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
  631. #ifdef DEBUG_ENABLED
  632. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  633. return;
  634. }
  635. debug_path_custom_point_size = MAX(0.0, p_point_size);
  636. debug_path_dirty = true;
  637. #endif // DEBUG_ENABLED
  638. }
  639. float NavigationAgent3D::get_debug_path_custom_point_size() const {
  640. return debug_path_custom_point_size;
  641. }
  642. #ifdef DEBUG_ENABLED
  643. void NavigationAgent3D::_navigation_debug_changed() {
  644. debug_path_dirty = true;
  645. }
  646. void NavigationAgent3D::_update_debug_path() {
  647. if (!debug_path_dirty) {
  648. return;
  649. }
  650. debug_path_dirty = false;
  651. if (!debug_path_instance.is_valid()) {
  652. debug_path_instance = RenderingServer::get_singleton()->instance_create();
  653. }
  654. if (!debug_path_mesh.is_valid()) {
  655. debug_path_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  656. }
  657. debug_path_mesh->clear_surfaces();
  658. if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  659. return;
  660. }
  661. if (!(agent_parent && agent_parent->is_inside_tree())) {
  662. return;
  663. }
  664. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  665. if (navigation_path.size() <= 1) {
  666. return;
  667. }
  668. Vector<Vector3> debug_path_lines_vertex_array;
  669. for (int i = 0; i < navigation_path.size() - 1; i++) {
  670. debug_path_lines_vertex_array.push_back(navigation_path[i]);
  671. debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
  672. }
  673. Array debug_path_lines_mesh_array;
  674. debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  675. debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
  676. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
  677. Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
  678. if (debug_use_custom) {
  679. if (!debug_agent_path_line_custom_material.is_valid()) {
  680. debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
  681. }
  682. debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
  683. debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
  684. } else {
  685. debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
  686. }
  687. if (debug_path_custom_point_size > 0.0) {
  688. Vector<Vector3> debug_path_points_vertex_array;
  689. for (int i = 0; i < navigation_path.size(); i++) {
  690. debug_path_points_vertex_array.push_back(navigation_path[i]);
  691. }
  692. Array debug_path_points_mesh_array;
  693. debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
  694. debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
  695. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
  696. Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
  697. if (debug_use_custom) {
  698. if (!debug_agent_path_point_custom_material.is_valid()) {
  699. debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
  700. }
  701. debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
  702. debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
  703. debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
  704. } else {
  705. debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
  706. }
  707. }
  708. RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
  709. RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
  710. RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  711. }
  712. #endif // DEBUG_ENABLED