navigation_agent_3d.cpp 13 KB

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