navigation_agent_3d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*************************************************************************/
  2. /* navigation_agent_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "servers/navigation_server_3d.h"
  32. void NavigationAgent3D::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent3D::get_rid);
  34. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent3D::set_avoidance_enabled);
  35. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent3D::get_avoidance_enabled);
  36. ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent3D::set_path_desired_distance);
  37. ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent3D::get_path_desired_distance);
  38. ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
  39. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
  40. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
  41. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
  42. ClassDB::bind_method(D_METHOD("set_agent_height_offset", "agent_height_offset"), &NavigationAgent3D::set_agent_height_offset);
  43. ClassDB::bind_method(D_METHOD("get_agent_height_offset"), &NavigationAgent3D::get_agent_height_offset);
  44. ClassDB::bind_method(D_METHOD("set_ignore_y", "ignore"), &NavigationAgent3D::set_ignore_y);
  45. ClassDB::bind_method(D_METHOD("get_ignore_y"), &NavigationAgent3D::get_ignore_y);
  46. ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
  47. ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
  48. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
  49. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
  50. ClassDB::bind_method(D_METHOD("set_time_horizon", "time_horizon"), &NavigationAgent3D::set_time_horizon);
  51. ClassDB::bind_method(D_METHOD("get_time_horizon"), &NavigationAgent3D::get_time_horizon);
  52. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
  53. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
  54. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
  55. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
  56. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
  57. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
  58. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
  59. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
  60. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
  61. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
  62. ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent3D::set_target_location);
  63. ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent3D::get_target_location);
  64. ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent3D::get_next_location);
  65. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
  66. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
  67. ClassDB::bind_method(D_METHOD("get_nav_path"), &NavigationAgent3D::get_nav_path);
  68. ClassDB::bind_method(D_METHOD("get_nav_path_index"), &NavigationAgent3D::get_nav_path_index);
  69. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
  70. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
  71. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
  72. ClassDB::bind_method(D_METHOD("get_final_location"), &NavigationAgent3D::get_final_location);
  73. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
  74. ADD_GROUP("Pathfinding", "");
  75. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,suffix:m"), "set_path_desired_distance", "get_path_desired_distance");
  76. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,suffix:m"), "set_target_desired_distance", "get_target_desired_distance");
  77. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01,suffix:m"), "set_agent_height_offset", "get_agent_height_offset");
  78. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "0.01,100,0.1,suffix:m"), "set_path_max_distance", "get_path_max_distance");
  79. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  80. ADD_GROUP("Avoidance", "");
  81. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  82. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,100,0.01,suffix:m"), "set_radius", "get_radius");
  83. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,10000,0.01,suffix:m"), "set_neighbor_distance", "get_neighbor_distance");
  84. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1"), "set_max_neighbors", "get_max_neighbors");
  85. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon", PROPERTY_HINT_RANGE, "0.01,100,0.01,suffix:s"), "set_time_horizon", "get_time_horizon");
  86. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.1,10000,0.01,suffix:m/s"), "set_max_speed", "get_max_speed");
  87. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_y"), "set_ignore_y", "get_ignore_y");
  88. ADD_SIGNAL(MethodInfo("path_changed"));
  89. ADD_SIGNAL(MethodInfo("target_reached"));
  90. ADD_SIGNAL(MethodInfo("navigation_finished"));
  91. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
  92. }
  93. void NavigationAgent3D::_notification(int p_what) {
  94. switch (p_what) {
  95. case NOTIFICATION_POST_ENTER_TREE: {
  96. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  97. // cannot use READY as ready does not get called if Node is readded to SceneTree
  98. set_agent_parent(get_parent());
  99. set_physics_process_internal(true);
  100. } break;
  101. case NOTIFICATION_PARENTED: {
  102. if (is_inside_tree() && (get_parent() != agent_parent)) {
  103. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  104. // PARENTED notification fires also when Node is added in scripts to a parent
  105. // this would spam transforms fails and world fails while Node is outside SceneTree
  106. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  107. set_agent_parent(get_parent());
  108. set_physics_process_internal(true);
  109. }
  110. } break;
  111. case NOTIFICATION_UNPARENTED: {
  112. // if agent has no parent no point in processing it until reparented
  113. set_agent_parent(nullptr);
  114. set_physics_process_internal(false);
  115. } break;
  116. case NOTIFICATION_EXIT_TREE: {
  117. set_agent_parent(nullptr);
  118. set_physics_process_internal(false);
  119. } break;
  120. case NOTIFICATION_PAUSED: {
  121. if (agent_parent && !agent_parent->can_process()) {
  122. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  123. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  124. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  125. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  126. map_before_pause = RID();
  127. }
  128. } break;
  129. case NOTIFICATION_UNPAUSED: {
  130. if (agent_parent && !agent_parent->can_process()) {
  131. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  132. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  133. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  134. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  135. map_before_pause = RID();
  136. }
  137. } break;
  138. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  139. if (agent_parent) {
  140. if (avoidance_enabled) {
  141. // agent_position on NavigationServer is avoidance only and has nothing to do with pathfinding
  142. // no point in flooding NavigationServer queue with agent position updates that get send to the void if avoidance is not used
  143. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  144. }
  145. _check_distance_to_target();
  146. }
  147. } break;
  148. }
  149. }
  150. NavigationAgent3D::NavigationAgent3D() {
  151. agent = NavigationServer3D::get_singleton()->agent_create();
  152. set_neighbor_distance(50.0);
  153. set_max_neighbors(10);
  154. set_time_horizon(5.0);
  155. set_radius(1.0);
  156. set_max_speed(10.0);
  157. set_ignore_y(true);
  158. }
  159. NavigationAgent3D::~NavigationAgent3D() {
  160. NavigationServer3D::get_singleton()->free(agent);
  161. agent = RID(); // Pointless
  162. }
  163. void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
  164. avoidance_enabled = p_enabled;
  165. if (avoidance_enabled) {
  166. NavigationServer3D::get_singleton()->agent_set_callback(agent, this, "_avoidance_done");
  167. } else {
  168. NavigationServer3D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done");
  169. }
  170. }
  171. bool NavigationAgent3D::get_avoidance_enabled() const {
  172. return avoidance_enabled;
  173. }
  174. void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
  175. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  176. NavigationServer3D::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done");
  177. if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
  178. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  179. agent_parent = Object::cast_to<Node3D>(p_agent_parent);
  180. if (map_override.is_valid()) {
  181. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
  182. } else {
  183. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
  184. }
  185. // create new avoidance callback if enabled
  186. set_avoidance_enabled(avoidance_enabled);
  187. } else {
  188. agent_parent = nullptr;
  189. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  190. }
  191. }
  192. void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
  193. bool navigation_layers_changed = navigation_layers != p_navigation_layers;
  194. navigation_layers = p_navigation_layers;
  195. if (navigation_layers_changed) {
  196. _request_repath();
  197. }
  198. }
  199. uint32_t NavigationAgent3D::get_navigation_layers() const {
  200. return navigation_layers;
  201. }
  202. void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  203. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  204. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  205. uint32_t _navigation_layers = get_navigation_layers();
  206. if (p_value) {
  207. _navigation_layers |= 1 << (p_layer_number - 1);
  208. } else {
  209. _navigation_layers &= ~(1 << (p_layer_number - 1));
  210. }
  211. set_navigation_layers(_navigation_layers);
  212. }
  213. bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
  214. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  215. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  216. return get_navigation_layers() & (1 << (p_layer_number - 1));
  217. }
  218. void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
  219. map_override = p_navigation_map;
  220. NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
  221. _request_repath();
  222. }
  223. RID NavigationAgent3D::get_navigation_map() const {
  224. if (map_override.is_valid()) {
  225. return map_override;
  226. } else if (agent_parent != nullptr) {
  227. return agent_parent->get_world_3d()->get_navigation_map();
  228. }
  229. return RID();
  230. }
  231. void NavigationAgent3D::set_path_desired_distance(real_t p_dd) {
  232. path_desired_distance = p_dd;
  233. }
  234. void NavigationAgent3D::set_target_desired_distance(real_t p_dd) {
  235. target_desired_distance = p_dd;
  236. }
  237. void NavigationAgent3D::set_radius(real_t p_radius) {
  238. radius = p_radius;
  239. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  240. }
  241. void NavigationAgent3D::set_agent_height_offset(real_t p_hh) {
  242. navigation_height_offset = p_hh;
  243. }
  244. void NavigationAgent3D::set_ignore_y(bool p_ignore_y) {
  245. ignore_y = p_ignore_y;
  246. NavigationServer3D::get_singleton()->agent_set_ignore_y(agent, ignore_y);
  247. }
  248. void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
  249. neighbor_distance = p_distance;
  250. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  251. }
  252. void NavigationAgent3D::set_max_neighbors(int p_count) {
  253. max_neighbors = p_count;
  254. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  255. }
  256. void NavigationAgent3D::set_time_horizon(real_t p_time) {
  257. time_horizon = p_time;
  258. NavigationServer3D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  259. }
  260. void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
  261. max_speed = p_max_speed;
  262. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  263. }
  264. void NavigationAgent3D::set_path_max_distance(real_t p_pmd) {
  265. path_max_distance = p_pmd;
  266. }
  267. real_t NavigationAgent3D::get_path_max_distance() {
  268. return path_max_distance;
  269. }
  270. void NavigationAgent3D::set_target_location(Vector3 p_location) {
  271. target_location = p_location;
  272. _request_repath();
  273. }
  274. Vector3 NavigationAgent3D::get_target_location() const {
  275. return target_location;
  276. }
  277. Vector3 NavigationAgent3D::get_next_location() {
  278. update_navigation();
  279. if (navigation_path.size() == 0) {
  280. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector3(), "The agent has no parent.");
  281. return agent_parent->get_global_transform().origin;
  282. } else {
  283. return navigation_path[nav_path_index] - Vector3(0, navigation_height_offset, 0);
  284. }
  285. }
  286. real_t NavigationAgent3D::distance_to_target() const {
  287. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, 0.0, "The agent has no parent.");
  288. return agent_parent->get_global_transform().origin.distance_to(target_location);
  289. }
  290. bool NavigationAgent3D::is_target_reached() const {
  291. return target_reached;
  292. }
  293. bool NavigationAgent3D::is_target_reachable() {
  294. return target_desired_distance >= get_final_location().distance_to(target_location);
  295. }
  296. bool NavigationAgent3D::is_navigation_finished() {
  297. update_navigation();
  298. return navigation_finished;
  299. }
  300. Vector3 NavigationAgent3D::get_final_location() {
  301. update_navigation();
  302. if (navigation_path.size() == 0) {
  303. return Vector3();
  304. }
  305. return navigation_path[navigation_path.size() - 1];
  306. }
  307. void NavigationAgent3D::set_velocity(Vector3 p_velocity) {
  308. target_velocity = p_velocity;
  309. NavigationServer3D::get_singleton()->agent_set_target_velocity(agent, target_velocity);
  310. NavigationServer3D::get_singleton()->agent_set_velocity(agent, prev_safe_velocity);
  311. velocity_submitted = true;
  312. }
  313. void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
  314. prev_safe_velocity = p_new_velocity;
  315. if (!velocity_submitted) {
  316. target_velocity = Vector3();
  317. return;
  318. }
  319. velocity_submitted = false;
  320. emit_signal(SNAME("velocity_computed"), p_new_velocity);
  321. }
  322. TypedArray<String> NavigationAgent3D::get_configuration_warnings() const {
  323. TypedArray<String> warnings = Node::get_configuration_warnings();
  324. if (!Object::cast_to<Node3D>(get_parent())) {
  325. warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
  326. }
  327. return warnings;
  328. }
  329. void NavigationAgent3D::update_navigation() {
  330. if (agent_parent == nullptr) {
  331. return;
  332. }
  333. if (!agent_parent->is_inside_tree()) {
  334. return;
  335. }
  336. if (update_frame_id == Engine::get_singleton()->get_physics_frames()) {
  337. return;
  338. }
  339. update_frame_id = Engine::get_singleton()->get_physics_frames();
  340. Vector3 o = agent_parent->get_global_transform().origin;
  341. bool reload_path = false;
  342. if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
  343. reload_path = true;
  344. } else if (navigation_path.size() == 0) {
  345. reload_path = true;
  346. } else {
  347. // Check if too far from the navigation path
  348. if (nav_path_index > 0) {
  349. Vector3 segment[2];
  350. segment[0] = navigation_path[nav_path_index - 1];
  351. segment[1] = navigation_path[nav_path_index];
  352. segment[0].y -= navigation_height_offset;
  353. segment[1].y -= navigation_height_offset;
  354. Vector3 p = Geometry3D::get_closest_point_to_segment(o, segment);
  355. if (o.distance_to(p) >= path_max_distance) {
  356. // To faraway, reload path
  357. reload_path = true;
  358. }
  359. }
  360. }
  361. if (reload_path) {
  362. if (map_override.is_valid()) {
  363. navigation_path = NavigationServer3D::get_singleton()->map_get_path(map_override, o, target_location, true, navigation_layers);
  364. } else {
  365. navigation_path = NavigationServer3D::get_singleton()->map_get_path(agent_parent->get_world_3d()->get_navigation_map(), o, target_location, true, navigation_layers);
  366. }
  367. navigation_finished = false;
  368. nav_path_index = 0;
  369. emit_signal(SNAME("path_changed"));
  370. }
  371. if (navigation_path.size() == 0) {
  372. return;
  373. }
  374. // Check if we can advance the navigation path
  375. if (navigation_finished == false) {
  376. // Advances to the next far away location.
  377. while (o.distance_to(navigation_path[nav_path_index] - Vector3(0, navigation_height_offset, 0)) < path_desired_distance) {
  378. nav_path_index += 1;
  379. if (nav_path_index == navigation_path.size()) {
  380. _check_distance_to_target();
  381. nav_path_index -= 1;
  382. navigation_finished = true;
  383. emit_signal(SNAME("navigation_finished"));
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. void NavigationAgent3D::_request_repath() {
  390. navigation_path.clear();
  391. target_reached = false;
  392. navigation_finished = false;
  393. update_frame_id = 0;
  394. }
  395. void NavigationAgent3D::_check_distance_to_target() {
  396. if (!target_reached) {
  397. if (distance_to_target() < target_desired_distance) {
  398. emit_signal(SNAME("target_reached"));
  399. target_reached = true;
  400. }
  401. }
  402. }