navigation_obstacle_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_transform().get_origin());
  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. } break;
  87. case NOTIFICATION_UNPAUSED: {
  88. if (!can_process()) {
  89. map_before_pause = map_current;
  90. _update_map(RID());
  91. } else if (can_process() && !(map_before_pause == RID())) {
  92. _update_map(map_before_pause);
  93. map_before_pause = RID();
  94. }
  95. } break;
  96. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  97. if (is_inside_tree()) {
  98. _update_position(get_global_transform().get_origin());
  99. if (velocity_submitted) {
  100. velocity_submitted = false;
  101. // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same
  102. if (!previous_velocity.is_equal_approx(velocity)) {
  103. NavigationServer2D::get_singleton()->obstacle_set_velocity(obstacle, velocity);
  104. }
  105. previous_velocity = velocity;
  106. }
  107. }
  108. } break;
  109. case NOTIFICATION_DRAW: {
  110. #ifdef DEBUG_ENABLED
  111. if (is_inside_tree()) {
  112. bool is_debug_enabled = false;
  113. if (Engine::get_singleton()->is_editor_hint()) {
  114. is_debug_enabled = true;
  115. } else if (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_avoidance_enabled()) {
  116. is_debug_enabled = true;
  117. }
  118. if (is_debug_enabled) {
  119. _update_fake_agent_radius_debug();
  120. _update_static_obstacle_debug();
  121. }
  122. }
  123. #endif // DEBUG_ENABLED
  124. } break;
  125. }
  126. }
  127. NavigationObstacle2D::NavigationObstacle2D() {
  128. obstacle = NavigationServer2D::get_singleton()->obstacle_create();
  129. set_radius(radius);
  130. set_vertices(vertices);
  131. set_avoidance_layers(avoidance_layers);
  132. set_avoidance_enabled(avoidance_enabled);
  133. }
  134. NavigationObstacle2D::~NavigationObstacle2D() {
  135. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  136. NavigationServer2D::get_singleton()->free(obstacle);
  137. obstacle = RID();
  138. }
  139. void NavigationObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
  140. vertices = p_vertices;
  141. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  142. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  143. queue_redraw();
  144. }
  145. }
  146. void NavigationObstacle2D::set_navigation_map(RID p_navigation_map) {
  147. if (map_override == p_navigation_map) {
  148. return;
  149. }
  150. map_override = p_navigation_map;
  151. _update_map(map_override);
  152. }
  153. RID NavigationObstacle2D::get_navigation_map() const {
  154. if (map_override.is_valid()) {
  155. return map_override;
  156. } else if (is_inside_tree()) {
  157. return get_world_2d()->get_navigation_map();
  158. }
  159. return RID();
  160. }
  161. void NavigationObstacle2D::set_radius(real_t p_radius) {
  162. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  163. if (Math::is_equal_approx(radius, p_radius)) {
  164. return;
  165. }
  166. radius = p_radius;
  167. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  168. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  169. queue_redraw();
  170. }
  171. }
  172. void NavigationObstacle2D::set_avoidance_layers(uint32_t p_layers) {
  173. if (avoidance_layers == p_layers) {
  174. return;
  175. }
  176. avoidance_layers = p_layers;
  177. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  178. }
  179. uint32_t NavigationObstacle2D::get_avoidance_layers() const {
  180. return avoidance_layers;
  181. }
  182. void NavigationObstacle2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  183. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  184. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  185. uint32_t avoidance_layers_new = get_avoidance_layers();
  186. if (p_value) {
  187. avoidance_layers_new |= 1 << (p_layer_number - 1);
  188. } else {
  189. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  190. }
  191. set_avoidance_layers(avoidance_layers_new);
  192. }
  193. bool NavigationObstacle2D::get_avoidance_layer_value(int p_layer_number) const {
  194. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  195. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  196. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  197. }
  198. void NavigationObstacle2D::set_avoidance_enabled(bool p_enabled) {
  199. if (avoidance_enabled == p_enabled) {
  200. return;
  201. }
  202. avoidance_enabled = p_enabled;
  203. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  204. }
  205. bool NavigationObstacle2D::get_avoidance_enabled() const {
  206. return avoidance_enabled;
  207. }
  208. void NavigationObstacle2D::set_velocity(const Vector2 p_velocity) {
  209. velocity = p_velocity;
  210. velocity_submitted = true;
  211. }
  212. void NavigationObstacle2D::_update_map(RID p_map) {
  213. map_current = p_map;
  214. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle, p_map);
  215. }
  216. void NavigationObstacle2D::_update_position(const Vector2 p_position) {
  217. NavigationServer2D::get_singleton()->obstacle_set_position(obstacle, p_position);
  218. }
  219. #ifdef DEBUG_ENABLED
  220. void NavigationObstacle2D::_update_fake_agent_radius_debug() {
  221. if (radius > 0.0 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) {
  222. Color debug_radius_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_color();
  223. draw_circle(get_global_transform().get_origin(), radius, debug_radius_color);
  224. }
  225. }
  226. #endif // DEBUG_ENABLED
  227. #ifdef DEBUG_ENABLED
  228. void NavigationObstacle2D::_update_static_obstacle_debug() {
  229. if (get_vertices().size() > 2 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_static()) {
  230. bool obstacle_pushes_inward = Geometry2D::is_polygon_clockwise(get_vertices());
  231. Color debug_static_obstacle_face_color;
  232. if (obstacle_pushes_inward) {
  233. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_face_color();
  234. } else {
  235. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_face_color();
  236. }
  237. Vector<Vector2> debug_obstacle_polygon_vertices = get_vertices();
  238. Vector<Color> debug_obstacle_polygon_colors;
  239. debug_obstacle_polygon_colors.resize(debug_obstacle_polygon_vertices.size());
  240. debug_obstacle_polygon_colors.fill(debug_static_obstacle_face_color);
  241. RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), debug_obstacle_polygon_vertices, debug_obstacle_polygon_colors);
  242. Color debug_static_obstacle_edge_color;
  243. if (obstacle_pushes_inward) {
  244. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color();
  245. } else {
  246. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color();
  247. }
  248. Vector<Vector2> debug_obstacle_line_vertices = get_vertices();
  249. debug_obstacle_line_vertices.push_back(debug_obstacle_line_vertices[0]);
  250. debug_obstacle_line_vertices.resize(debug_obstacle_line_vertices.size());
  251. Vector<Color> debug_obstacle_line_colors;
  252. debug_obstacle_line_colors.resize(debug_obstacle_line_vertices.size());
  253. debug_obstacle_line_colors.fill(debug_static_obstacle_edge_color);
  254. RS::get_singleton()->canvas_item_add_polyline(get_canvas_item(), debug_obstacle_line_vertices, debug_obstacle_line_colors, 4.0);
  255. }
  256. }
  257. #endif // DEBUG_ENABLED