navigation_agent_2d.cpp 13 KB

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