navigation_agent_3d.cpp 36 KB

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