navigation_agent_2d.cpp 14 KB

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