navigation_obstacle_2d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. ClassDB::bind_method(D_METHOD("set_affect_navigation_mesh", "enabled"), &NavigationObstacle2D::set_affect_navigation_mesh);
  52. ClassDB::bind_method(D_METHOD("get_affect_navigation_mesh"), &NavigationObstacle2D::get_affect_navigation_mesh);
  53. ClassDB::bind_method(D_METHOD("set_carve_navigation_mesh", "enabled"), &NavigationObstacle2D::set_carve_navigation_mesh);
  54. ClassDB::bind_method(D_METHOD("get_carve_navigation_mesh"), &NavigationObstacle2D::get_carve_navigation_mesh);
  55. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,500,0.01,suffix:px"), "set_radius", "get_radius");
  56. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "vertices"), "set_vertices", "get_vertices");
  57. ADD_GROUP("NavigationMesh", "");
  58. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "affect_navigation_mesh"), "set_affect_navigation_mesh", "get_affect_navigation_mesh");
  59. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "carve_navigation_mesh"), "set_carve_navigation_mesh", "get_carve_navigation_mesh");
  60. ADD_GROUP("Avoidance", "");
  61. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  62. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  63. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  64. }
  65. void NavigationObstacle2D::_notification(int p_what) {
  66. switch (p_what) {
  67. case NOTIFICATION_POST_ENTER_TREE: {
  68. if (map_override.is_valid()) {
  69. _update_map(map_override);
  70. } else if (is_inside_tree()) {
  71. _update_map(get_world_2d()->get_navigation_map());
  72. } else {
  73. _update_map(RID());
  74. }
  75. previous_transform = get_global_transform();
  76. // need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents
  77. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  78. _update_position(get_global_position());
  79. set_physics_process_internal(true);
  80. #ifdef DEBUG_ENABLED
  81. RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, get_world_2d()->get_canvas());
  82. #endif // DEBUG_ENABLED
  83. } break;
  84. case NOTIFICATION_EXIT_TREE: {
  85. set_physics_process_internal(false);
  86. _update_map(RID());
  87. #ifdef DEBUG_ENABLED
  88. RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, RID());
  89. #endif // DEBUG_ENABLED
  90. } break;
  91. case NOTIFICATION_SUSPENDED:
  92. case NOTIFICATION_PAUSED: {
  93. if (!can_process()) {
  94. map_before_pause = map_current;
  95. _update_map(RID());
  96. } else if (can_process() && !(map_before_pause == RID())) {
  97. _update_map(map_before_pause);
  98. map_before_pause = RID();
  99. }
  100. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  101. } break;
  102. case NOTIFICATION_UNSUSPENDED: {
  103. if (get_tree()->is_paused()) {
  104. break;
  105. }
  106. [[fallthrough]];
  107. }
  108. case NOTIFICATION_UNPAUSED: {
  109. if (!can_process()) {
  110. map_before_pause = map_current;
  111. _update_map(RID());
  112. } else if (can_process() && !(map_before_pause == RID())) {
  113. _update_map(map_before_pause);
  114. map_before_pause = RID();
  115. }
  116. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  117. } break;
  118. case NOTIFICATION_VISIBILITY_CHANGED: {
  119. #ifdef DEBUG_ENABLED
  120. RS::get_singleton()->canvas_item_set_visible(debug_canvas_item, is_visible_in_tree());
  121. #endif // DEBUG_ENABLED
  122. } break;
  123. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  124. if (is_inside_tree()) {
  125. _update_position(get_global_position());
  126. if (velocity_submitted) {
  127. velocity_submitted = false;
  128. // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same
  129. if (!previous_velocity.is_equal_approx(velocity)) {
  130. NavigationServer2D::get_singleton()->obstacle_set_velocity(obstacle, velocity);
  131. }
  132. previous_velocity = velocity;
  133. }
  134. }
  135. } break;
  136. case NOTIFICATION_DRAW: {
  137. #ifdef DEBUG_ENABLED
  138. if (is_inside_tree()) {
  139. bool is_debug_enabled = false;
  140. if (Engine::get_singleton()->is_editor_hint()) {
  141. is_debug_enabled = true;
  142. } else if (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_avoidance_enabled()) {
  143. is_debug_enabled = true;
  144. }
  145. if (is_debug_enabled) {
  146. RS::get_singleton()->canvas_item_clear(debug_canvas_item);
  147. Transform2D debug_transform = Transform2D(0.0, get_global_position());
  148. RS::get_singleton()->canvas_item_set_transform(debug_canvas_item, debug_transform);
  149. _update_fake_agent_radius_debug();
  150. _update_static_obstacle_debug();
  151. }
  152. }
  153. #endif // DEBUG_ENABLED
  154. } break;
  155. }
  156. }
  157. NavigationObstacle2D::NavigationObstacle2D() {
  158. obstacle = NavigationServer2D::get_singleton()->obstacle_create();
  159. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  160. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  161. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  162. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  163. #ifdef DEBUG_ENABLED
  164. debug_canvas_item = RenderingServer::get_singleton()->canvas_item_create();
  165. #endif // DEBUG_ENABLED
  166. }
  167. NavigationObstacle2D::~NavigationObstacle2D() {
  168. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  169. NavigationServer2D::get_singleton()->free(obstacle);
  170. obstacle = RID();
  171. #ifdef DEBUG_ENABLED
  172. if (debug_canvas_item.is_valid()) {
  173. RenderingServer::get_singleton()->free(debug_canvas_item);
  174. debug_canvas_item = RID();
  175. }
  176. #endif // DEBUG_ENABLED
  177. }
  178. void NavigationObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
  179. vertices = p_vertices;
  180. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  181. #ifdef DEBUG_ENABLED
  182. queue_redraw();
  183. #endif // DEBUG_ENABLED
  184. }
  185. void NavigationObstacle2D::set_navigation_map(RID p_navigation_map) {
  186. if (map_override == p_navigation_map) {
  187. return;
  188. }
  189. map_override = p_navigation_map;
  190. _update_map(map_override);
  191. }
  192. RID NavigationObstacle2D::get_navigation_map() const {
  193. if (map_override.is_valid()) {
  194. return map_override;
  195. } else if (is_inside_tree()) {
  196. return get_world_2d()->get_navigation_map();
  197. }
  198. return RID();
  199. }
  200. void NavigationObstacle2D::set_radius(real_t p_radius) {
  201. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  202. if (Math::is_equal_approx(radius, p_radius)) {
  203. return;
  204. }
  205. radius = p_radius;
  206. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  207. #ifdef DEBUG_ENABLED
  208. queue_redraw();
  209. #endif // DEBUG_ENABLED
  210. }
  211. void NavigationObstacle2D::set_avoidance_layers(uint32_t p_layers) {
  212. if (avoidance_layers == p_layers) {
  213. return;
  214. }
  215. avoidance_layers = p_layers;
  216. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  217. }
  218. uint32_t NavigationObstacle2D::get_avoidance_layers() const {
  219. return avoidance_layers;
  220. }
  221. void NavigationObstacle2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  222. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  223. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  224. uint32_t avoidance_layers_new = get_avoidance_layers();
  225. if (p_value) {
  226. avoidance_layers_new |= 1 << (p_layer_number - 1);
  227. } else {
  228. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  229. }
  230. set_avoidance_layers(avoidance_layers_new);
  231. }
  232. bool NavigationObstacle2D::get_avoidance_layer_value(int p_layer_number) const {
  233. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  234. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  235. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  236. }
  237. void NavigationObstacle2D::set_avoidance_enabled(bool p_enabled) {
  238. if (avoidance_enabled == p_enabled) {
  239. return;
  240. }
  241. avoidance_enabled = p_enabled;
  242. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  243. #ifdef DEBUG_ENABLED
  244. queue_redraw();
  245. #endif // DEBUG_ENABLED
  246. }
  247. bool NavigationObstacle2D::get_avoidance_enabled() const {
  248. return avoidance_enabled;
  249. }
  250. void NavigationObstacle2D::set_velocity(const Vector2 p_velocity) {
  251. velocity = p_velocity;
  252. velocity_submitted = true;
  253. }
  254. void NavigationObstacle2D::set_affect_navigation_mesh(bool p_enabled) {
  255. affect_navigation_mesh = p_enabled;
  256. }
  257. bool NavigationObstacle2D::get_affect_navigation_mesh() const {
  258. return affect_navigation_mesh;
  259. }
  260. void NavigationObstacle2D::set_carve_navigation_mesh(bool p_enabled) {
  261. carve_navigation_mesh = p_enabled;
  262. }
  263. bool NavigationObstacle2D::get_carve_navigation_mesh() const {
  264. return carve_navigation_mesh;
  265. }
  266. void NavigationObstacle2D::_update_map(RID p_map) {
  267. map_current = p_map;
  268. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle, p_map);
  269. }
  270. void NavigationObstacle2D::_update_position(const Vector2 p_position) {
  271. NavigationServer2D::get_singleton()->obstacle_set_position(obstacle, p_position);
  272. #ifdef DEBUG_ENABLED
  273. queue_redraw();
  274. #endif // DEBUG_ENABLED
  275. }
  276. #ifdef DEBUG_ENABLED
  277. void NavigationObstacle2D::_update_fake_agent_radius_debug() {
  278. if (radius > 0.0 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) {
  279. Color debug_radius_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_color();
  280. RS::get_singleton()->canvas_item_add_circle(debug_canvas_item, Vector2(), radius, debug_radius_color);
  281. }
  282. }
  283. #endif // DEBUG_ENABLED
  284. #ifdef DEBUG_ENABLED
  285. void NavigationObstacle2D::_update_static_obstacle_debug() {
  286. if (get_vertices().size() > 2 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_static()) {
  287. bool obstacle_pushes_inward = Geometry2D::is_polygon_clockwise(get_vertices());
  288. Color debug_static_obstacle_face_color;
  289. if (obstacle_pushes_inward) {
  290. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_face_color();
  291. } else {
  292. debug_static_obstacle_face_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_face_color();
  293. }
  294. Vector<Vector2> debug_obstacle_polygon_vertices = get_vertices();
  295. Vector<Color> debug_obstacle_polygon_colors;
  296. debug_obstacle_polygon_colors.resize(debug_obstacle_polygon_vertices.size());
  297. debug_obstacle_polygon_colors.fill(debug_static_obstacle_face_color);
  298. RS::get_singleton()->canvas_item_add_polygon(debug_canvas_item, debug_obstacle_polygon_vertices, debug_obstacle_polygon_colors);
  299. Color debug_static_obstacle_edge_color;
  300. if (obstacle_pushes_inward) {
  301. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color();
  302. } else {
  303. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color();
  304. }
  305. Vector<Vector2> debug_obstacle_line_vertices = get_vertices();
  306. debug_obstacle_line_vertices.push_back(debug_obstacle_line_vertices[0]);
  307. debug_obstacle_line_vertices.resize(debug_obstacle_line_vertices.size());
  308. Vector<Color> debug_obstacle_line_colors;
  309. debug_obstacle_line_colors.resize(debug_obstacle_line_vertices.size());
  310. debug_obstacle_line_colors.fill(debug_static_obstacle_edge_color);
  311. RS::get_singleton()->canvas_item_add_polyline(debug_canvas_item, debug_obstacle_line_vertices, debug_obstacle_line_colors, 4.0);
  312. }
  313. }
  314. #endif // DEBUG_ENABLED