navigation_obstacle_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**************************************************************************/
  2. /* navigation_obstacle_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_obstacle_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "scene/resources/world_2d.h"
  33. #include "servers/navigation_server_2d.h"
  34. #include "servers/navigation_server_3d.h"
  35. void NavigationObstacle2D::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationObstacle2D::get_rid);
  37. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationObstacle2D::set_avoidance_enabled);
  38. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationObstacle2D::get_avoidance_enabled);
  39. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationObstacle2D::set_navigation_map);
  40. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationObstacle2D::get_navigation_map);
  41. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationObstacle2D::set_radius);
  42. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationObstacle2D::get_radius);
  43. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationObstacle2D::set_velocity);
  44. ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationObstacle2D::get_velocity);
  45. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationObstacle2D::set_vertices);
  46. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationObstacle2D::get_vertices);
  47. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationObstacle2D::set_avoidance_layers);
  48. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationObstacle2D::get_avoidance_layers);
  49. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationObstacle2D::set_avoidance_layer_value);
  50. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationObstacle2D::get_avoidance_layer_value);
  51. ADD_GROUP("Avoidance", "");
  52. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  53. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  54. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,500,0.01,suffix:px"), "set_radius", "get_radius");
  55. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "vertices"), "set_vertices", "get_vertices");
  56. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  57. }
  58. void NavigationObstacle2D::_notification(int p_what) {
  59. switch (p_what) {
  60. case NOTIFICATION_POST_ENTER_TREE: {
  61. if (map_override.is_valid()) {
  62. _update_map(map_override);
  63. } else if (is_inside_tree()) {
  64. _update_map(get_world_2d()->get_navigation_map());
  65. } else {
  66. _update_map(RID());
  67. }
  68. previous_transform = get_global_transform();
  69. // need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents
  70. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  71. _update_position(get_global_position());
  72. set_physics_process_internal(true);
  73. } break;
  74. case NOTIFICATION_EXIT_TREE: {
  75. set_physics_process_internal(false);
  76. _update_map(RID());
  77. } break;
  78. case NOTIFICATION_PAUSED: {
  79. if (!can_process()) {
  80. map_before_pause = map_current;
  81. _update_map(RID());
  82. } else if (can_process() && !(map_before_pause == RID())) {
  83. _update_map(map_before_pause);
  84. map_before_pause = RID();
  85. }
  86. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  87. } break;
  88. case NOTIFICATION_UNPAUSED: {
  89. if (!can_process()) {
  90. map_before_pause = map_current;
  91. _update_map(RID());
  92. } else if (can_process() && !(map_before_pause == RID())) {
  93. _update_map(map_before_pause);
  94. map_before_pause = RID();
  95. }
  96. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  97. } break;
  98. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  99. if (is_inside_tree()) {
  100. _update_position(get_global_position());
  101. if (velocity_submitted) {
  102. velocity_submitted = false;
  103. // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same
  104. if (!previous_velocity.is_equal_approx(velocity)) {
  105. NavigationServer2D::get_singleton()->obstacle_set_velocity(obstacle, velocity);
  106. }
  107. previous_velocity = velocity;
  108. }
  109. }
  110. } break;
  111. case NOTIFICATION_DRAW: {
  112. #ifdef DEBUG_ENABLED
  113. if (is_inside_tree()) {
  114. bool is_debug_enabled = false;
  115. if (Engine::get_singleton()->is_editor_hint()) {
  116. is_debug_enabled = true;
  117. } else if (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_avoidance_enabled()) {
  118. is_debug_enabled = true;
  119. }
  120. if (is_debug_enabled) {
  121. _update_fake_agent_radius_debug();
  122. _update_static_obstacle_debug();
  123. }
  124. }
  125. #endif // DEBUG_ENABLED
  126. } break;
  127. }
  128. }
  129. NavigationObstacle2D::NavigationObstacle2D() {
  130. obstacle = NavigationServer2D::get_singleton()->obstacle_create();
  131. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  132. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  133. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  134. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  135. }
  136. NavigationObstacle2D::~NavigationObstacle2D() {
  137. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  138. NavigationServer2D::get_singleton()->free(obstacle);
  139. obstacle = RID();
  140. }
  141. void NavigationObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
  142. vertices = p_vertices;
  143. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  144. #ifdef DEBUG_ENABLED
  145. queue_redraw();
  146. #endif // DEBUG_ENABLED
  147. }
  148. void NavigationObstacle2D::set_navigation_map(RID p_navigation_map) {
  149. if (map_override == p_navigation_map) {
  150. return;
  151. }
  152. map_override = p_navigation_map;
  153. _update_map(map_override);
  154. }
  155. RID NavigationObstacle2D::get_navigation_map() const {
  156. if (map_override.is_valid()) {
  157. return map_override;
  158. } else if (is_inside_tree()) {
  159. return get_world_2d()->get_navigation_map();
  160. }
  161. return RID();
  162. }
  163. void NavigationObstacle2D::set_radius(real_t p_radius) {
  164. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  165. if (Math::is_equal_approx(radius, p_radius)) {
  166. return;
  167. }
  168. radius = p_radius;
  169. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  170. #ifdef DEBUG_ENABLED
  171. queue_redraw();
  172. #endif // DEBUG_ENABLED
  173. }
  174. void NavigationObstacle2D::set_avoidance_layers(uint32_t p_layers) {
  175. if (avoidance_layers == p_layers) {
  176. return;
  177. }
  178. avoidance_layers = p_layers;
  179. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  180. }
  181. uint32_t NavigationObstacle2D::get_avoidance_layers() const {
  182. return avoidance_layers;
  183. }
  184. void NavigationObstacle2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  185. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  186. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  187. uint32_t avoidance_layers_new = get_avoidance_layers();
  188. if (p_value) {
  189. avoidance_layers_new |= 1 << (p_layer_number - 1);
  190. } else {
  191. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  192. }
  193. set_avoidance_layers(avoidance_layers_new);
  194. }
  195. bool NavigationObstacle2D::get_avoidance_layer_value(int p_layer_number) const {
  196. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  197. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  198. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  199. }
  200. void NavigationObstacle2D::set_avoidance_enabled(bool p_enabled) {
  201. if (avoidance_enabled == p_enabled) {
  202. return;
  203. }
  204. avoidance_enabled = p_enabled;
  205. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  206. #ifdef DEBUG_ENABLED
  207. queue_redraw();
  208. #endif // DEBUG_ENABLED
  209. }
  210. bool NavigationObstacle2D::get_avoidance_enabled() const {
  211. return avoidance_enabled;
  212. }
  213. void NavigationObstacle2D::set_velocity(const Vector2 p_velocity) {
  214. velocity = p_velocity;
  215. velocity_submitted = true;
  216. }
  217. void NavigationObstacle2D::_update_map(RID p_map) {
  218. map_current = p_map;
  219. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle, p_map);
  220. }
  221. void NavigationObstacle2D::_update_position(const Vector2 p_position) {
  222. NavigationServer2D::get_singleton()->obstacle_set_position(obstacle, p_position);
  223. #ifdef DEBUG_ENABLED
  224. queue_redraw();
  225. #endif // DEBUG_ENABLED
  226. }
  227. #ifdef DEBUG_ENABLED
  228. void NavigationObstacle2D::_update_fake_agent_radius_debug() {
  229. if (radius > 0.0 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) {
  230. Color debug_radius_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_color();
  231. RS::get_singleton()->canvas_item_add_circle(get_canvas_item(), Vector2(), radius, debug_radius_color);
  232. }
  233. }
  234. #endif // DEBUG_ENABLED
  235. #ifdef DEBUG_ENABLED
  236. void NavigationObstacle2D::_update_static_obstacle_debug() {
  237. if (get_vertices().size() > 2 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_static()) {
  238. bool obstacle_pushes_inward = Geometry2D::is_polygon_clockwise(get_vertices());
  239. Color debug_static_obstacle_face_color;
  240. if (obstacle_pushes_inward) {
  241. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_face_color();
  242. } else {
  243. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_face_color();
  244. }
  245. Vector<Vector2> debug_obstacle_polygon_vertices = get_vertices();
  246. Vector<Color> debug_obstacle_polygon_colors;
  247. debug_obstacle_polygon_colors.resize(debug_obstacle_polygon_vertices.size());
  248. debug_obstacle_polygon_colors.fill(debug_static_obstacle_face_color);
  249. RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), debug_obstacle_polygon_vertices, debug_obstacle_polygon_colors);
  250. Color debug_static_obstacle_edge_color;
  251. if (obstacle_pushes_inward) {
  252. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color();
  253. } else {
  254. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color();
  255. }
  256. Vector<Vector2> debug_obstacle_line_vertices = get_vertices();
  257. debug_obstacle_line_vertices.push_back(debug_obstacle_line_vertices[0]);
  258. debug_obstacle_line_vertices.resize(debug_obstacle_line_vertices.size());
  259. Vector<Color> debug_obstacle_line_colors;
  260. debug_obstacle_line_colors.resize(debug_obstacle_line_vertices.size());
  261. debug_obstacle_line_colors.fill(debug_static_obstacle_edge_color);
  262. RS::get_singleton()->canvas_item_add_polyline(get_canvas_item(), debug_obstacle_line_vertices, debug_obstacle_line_colors, 4.0);
  263. }
  264. }
  265. #endif // DEBUG_ENABLED