2
0

navigation_agent_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
  35. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
  36. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
  37. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
  38. ClassDB::bind_method(D_METHOD("set_agent_height_offset", "agent_height_offset"), &NavigationAgent3D::set_agent_height_offset);
  39. ClassDB::bind_method(D_METHOD("get_agent_height_offset"), &NavigationAgent3D::get_agent_height_offset);
  40. ClassDB::bind_method(D_METHOD("set_ignore_y", "ignore"), &NavigationAgent3D::set_ignore_y);
  41. ClassDB::bind_method(D_METHOD("get_ignore_y"), &NavigationAgent3D::get_ignore_y);
  42. ClassDB::bind_method(D_METHOD("set_neighbor_dist", "neighbor_dist"), &NavigationAgent3D::set_neighbor_dist);
  43. ClassDB::bind_method(D_METHOD("get_neighbor_dist"), &NavigationAgent3D::get_neighbor_dist);
  44. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
  45. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
  46. ClassDB::bind_method(D_METHOD("set_time_horizon", "time_horizon"), &NavigationAgent3D::set_time_horizon);
  47. ClassDB::bind_method(D_METHOD("get_time_horizon"), &NavigationAgent3D::get_time_horizon);
  48. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
  49. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
  50. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
  51. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
  52. ClassDB::bind_method(D_METHOD("set_navigable_layers", "navigable_layers"), &NavigationAgent3D::set_navigable_layers);
  53. ClassDB::bind_method(D_METHOD("get_navigable_layers"), &NavigationAgent3D::get_navigable_layers);
  54. ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent3D::set_target_location);
  55. ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent3D::get_target_location);
  56. ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent3D::get_next_location);
  57. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
  58. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
  59. ClassDB::bind_method(D_METHOD("get_nav_path"), &NavigationAgent3D::get_nav_path);
  60. ClassDB::bind_method(D_METHOD("get_nav_path_index"), &NavigationAgent3D::get_nav_path_index);
  61. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
  62. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
  63. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
  64. ClassDB::bind_method(D_METHOD("get_final_location"), &NavigationAgent3D::get_final_location);
  65. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
  66. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance");
  67. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_radius", "get_radius");
  68. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01"), "set_agent_height_offset", "get_agent_height_offset");
  69. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_dist", PROPERTY_HINT_RANGE, "0.1,10000,0.01"), "set_neighbor_dist", "get_neighbor_dist");
  70. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1"), "set_max_neighbors", "get_max_neighbors");
  71. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon", PROPERTY_HINT_RANGE, "0.01,100,0.01"), "set_time_horizon", "get_time_horizon");
  72. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.1,10000,0.01"), "set_max_speed", "get_max_speed");
  73. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "0.01,100,0.1"), "set_path_max_distance", "get_path_max_distance");
  74. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_y"), "set_ignore_y", "get_ignore_y");
  75. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigable_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigable_layers", "get_navigable_layers");
  76. ADD_SIGNAL(MethodInfo("path_changed"));
  77. ADD_SIGNAL(MethodInfo("target_reached"));
  78. ADD_SIGNAL(MethodInfo("navigation_finished"));
  79. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
  80. }
  81. void NavigationAgent3D::_notification(int p_what) {
  82. switch (p_what) {
  83. case NOTIFICATION_READY: {
  84. agent_parent = Object::cast_to<Node3D>(get_parent());
  85. if (agent_parent != nullptr) {
  86. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  87. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
  88. NavigationServer3D::get_singleton()->agent_set_callback(agent, this, "_avoidance_done");
  89. }
  90. set_physics_process_internal(true);
  91. } break;
  92. case NOTIFICATION_EXIT_TREE: {
  93. agent_parent = nullptr;
  94. set_physics_process_internal(false);
  95. } break;
  96. case NOTIFICATION_PAUSED: {
  97. if (agent_parent && !agent_parent->can_process()) {
  98. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  99. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  100. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  101. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  102. map_before_pause = RID();
  103. }
  104. } break;
  105. case NOTIFICATION_UNPAUSED: {
  106. if (agent_parent && !agent_parent->can_process()) {
  107. map_before_pause = NavigationServer3D::get_singleton()->agent_get_map(get_rid());
  108. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  109. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  110. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  111. map_before_pause = RID();
  112. }
  113. } break;
  114. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  115. if (agent_parent) {
  116. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  117. _check_distance_to_target();
  118. }
  119. } break;
  120. }
  121. }
  122. NavigationAgent3D::NavigationAgent3D() {
  123. agent = NavigationServer3D::get_singleton()->agent_create();
  124. set_neighbor_dist(50.0);
  125. set_max_neighbors(10);
  126. set_time_horizon(5.0);
  127. set_radius(1.0);
  128. set_max_speed(10.0);
  129. set_ignore_y(true);
  130. }
  131. NavigationAgent3D::~NavigationAgent3D() {
  132. NavigationServer3D::get_singleton()->free(agent);
  133. agent = RID(); // Pointless
  134. }
  135. void NavigationAgent3D::set_navigable_layers(uint32_t p_layers) {
  136. bool layers_changed = navigable_layers != p_layers;
  137. navigable_layers = p_layers;
  138. if (layers_changed) {
  139. _request_repath();
  140. }
  141. }
  142. uint32_t NavigationAgent3D::get_navigable_layers() const {
  143. return navigable_layers;
  144. }
  145. void NavigationAgent3D::set_target_desired_distance(real_t p_dd) {
  146. target_desired_distance = p_dd;
  147. }
  148. void NavigationAgent3D::set_radius(real_t p_radius) {
  149. radius = p_radius;
  150. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  151. }
  152. void NavigationAgent3D::set_agent_height_offset(real_t p_hh) {
  153. navigation_height_offset = p_hh;
  154. }
  155. void NavigationAgent3D::set_ignore_y(bool p_ignore_y) {
  156. ignore_y = p_ignore_y;
  157. NavigationServer3D::get_singleton()->agent_set_ignore_y(agent, ignore_y);
  158. }
  159. void NavigationAgent3D::set_neighbor_dist(real_t p_dist) {
  160. neighbor_dist = p_dist;
  161. NavigationServer3D::get_singleton()->agent_set_neighbor_dist(agent, neighbor_dist);
  162. }
  163. void NavigationAgent3D::set_max_neighbors(int p_count) {
  164. max_neighbors = p_count;
  165. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  166. }
  167. void NavigationAgent3D::set_time_horizon(real_t p_time) {
  168. time_horizon = p_time;
  169. NavigationServer3D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  170. }
  171. void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
  172. max_speed = p_max_speed;
  173. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  174. }
  175. void NavigationAgent3D::set_path_max_distance(real_t p_pmd) {
  176. path_max_distance = p_pmd;
  177. }
  178. real_t NavigationAgent3D::get_path_max_distance() {
  179. return path_max_distance;
  180. }
  181. void NavigationAgent3D::set_target_location(Vector3 p_location) {
  182. target_location = p_location;
  183. _request_repath();
  184. }
  185. Vector3 NavigationAgent3D::get_target_location() const {
  186. return target_location;
  187. }
  188. Vector3 NavigationAgent3D::get_next_location() {
  189. update_navigation();
  190. if (navigation_path.size() == 0) {
  191. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector3(), "The agent has no parent.");
  192. return agent_parent->get_global_transform().origin;
  193. } else {
  194. return navigation_path[nav_path_index] - Vector3(0, navigation_height_offset, 0);
  195. }
  196. }
  197. real_t NavigationAgent3D::distance_to_target() const {
  198. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, 0.0, "The agent has no parent.");
  199. return agent_parent->get_global_transform().origin.distance_to(target_location);
  200. }
  201. bool NavigationAgent3D::is_target_reached() const {
  202. return target_reached;
  203. }
  204. bool NavigationAgent3D::is_target_reachable() {
  205. return target_desired_distance >= get_final_location().distance_to(target_location);
  206. }
  207. bool NavigationAgent3D::is_navigation_finished() {
  208. update_navigation();
  209. return navigation_finished;
  210. }
  211. Vector3 NavigationAgent3D::get_final_location() {
  212. update_navigation();
  213. if (navigation_path.size() == 0) {
  214. return Vector3();
  215. }
  216. return navigation_path[navigation_path.size() - 1];
  217. }
  218. void NavigationAgent3D::set_velocity(Vector3 p_velocity) {
  219. target_velocity = p_velocity;
  220. NavigationServer3D::get_singleton()->agent_set_target_velocity(agent, target_velocity);
  221. NavigationServer3D::get_singleton()->agent_set_velocity(agent, prev_safe_velocity);
  222. velocity_submitted = true;
  223. }
  224. void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
  225. prev_safe_velocity = p_new_velocity;
  226. if (!velocity_submitted) {
  227. target_velocity = Vector3();
  228. return;
  229. }
  230. velocity_submitted = false;
  231. emit_signal(SNAME("velocity_computed"), p_new_velocity);
  232. }
  233. TypedArray<String> NavigationAgent3D::get_configuration_warnings() const {
  234. TypedArray<String> warnings = Node::get_configuration_warnings();
  235. if (!Object::cast_to<Node3D>(get_parent())) {
  236. warnings.push_back(RTR("The NavigationAgent3D can be used only under a spatial node."));
  237. }
  238. return warnings;
  239. }
  240. void NavigationAgent3D::update_navigation() {
  241. if (agent_parent == nullptr) {
  242. return;
  243. }
  244. if (!agent_parent->is_inside_tree()) {
  245. return;
  246. }
  247. if (update_frame_id == Engine::get_singleton()->get_physics_frames()) {
  248. return;
  249. }
  250. update_frame_id = Engine::get_singleton()->get_physics_frames();
  251. Vector3 o = agent_parent->get_global_transform().origin;
  252. bool reload_path = false;
  253. if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
  254. reload_path = true;
  255. } else if (navigation_path.size() == 0) {
  256. reload_path = true;
  257. } else {
  258. // Check if too far from the navigation path
  259. if (nav_path_index > 0) {
  260. Vector3 segment[2];
  261. segment[0] = navigation_path[nav_path_index - 1];
  262. segment[1] = navigation_path[nav_path_index];
  263. segment[0].y -= navigation_height_offset;
  264. segment[1].y -= navigation_height_offset;
  265. Vector3 p = Geometry3D::get_closest_point_to_segment(o, segment);
  266. if (o.distance_to(p) >= path_max_distance) {
  267. // To faraway, reload path
  268. reload_path = true;
  269. }
  270. }
  271. }
  272. if (reload_path) {
  273. navigation_path = NavigationServer3D::get_singleton()->map_get_path(agent_parent->get_world_3d()->get_navigation_map(), o, target_location, true, navigable_layers);
  274. navigation_finished = false;
  275. nav_path_index = 0;
  276. emit_signal(SNAME("path_changed"));
  277. }
  278. if (navigation_path.size() == 0) {
  279. return;
  280. }
  281. // Check if we can advance the navigation path
  282. if (navigation_finished == false) {
  283. // Advances to the next far away location.
  284. while (o.distance_to(navigation_path[nav_path_index] - Vector3(0, navigation_height_offset, 0)) < target_desired_distance) {
  285. nav_path_index += 1;
  286. if (nav_path_index == navigation_path.size()) {
  287. _check_distance_to_target();
  288. nav_path_index -= 1;
  289. navigation_finished = true;
  290. emit_signal(SNAME("navigation_finished"));
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. void NavigationAgent3D::_request_repath() {
  297. navigation_path.clear();
  298. target_reached = false;
  299. navigation_finished = false;
  300. update_frame_id = 0;
  301. }
  302. void NavigationAgent3D::_check_distance_to_target() {
  303. if (!target_reached) {
  304. if (distance_to_target() < target_desired_distance) {
  305. emit_signal(SNAME("target_reached"));
  306. target_reached = true;
  307. }
  308. }
  309. }