navigation_obstacle_2d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/2d/navigation_mesh_source_geometry_data_2d.h"
  33. #include "scene/resources/2d/navigation_polygon.h"
  34. #include "scene/resources/world_2d.h"
  35. #include "servers/navigation_server_2d.h"
  36. Callable NavigationObstacle2D::_navmesh_source_geometry_parsing_callback;
  37. RID NavigationObstacle2D::_navmesh_source_geometry_parser;
  38. void NavigationObstacle2D::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationObstacle2D::get_rid);
  40. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationObstacle2D::set_avoidance_enabled);
  41. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationObstacle2D::get_avoidance_enabled);
  42. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationObstacle2D::set_navigation_map);
  43. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationObstacle2D::get_navigation_map);
  44. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationObstacle2D::set_radius);
  45. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationObstacle2D::get_radius);
  46. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationObstacle2D::set_velocity);
  47. ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationObstacle2D::get_velocity);
  48. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationObstacle2D::set_vertices);
  49. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationObstacle2D::get_vertices);
  50. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationObstacle2D::set_avoidance_layers);
  51. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationObstacle2D::get_avoidance_layers);
  52. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationObstacle2D::set_avoidance_layer_value);
  53. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationObstacle2D::get_avoidance_layer_value);
  54. ClassDB::bind_method(D_METHOD("set_affect_navigation_mesh", "enabled"), &NavigationObstacle2D::set_affect_navigation_mesh);
  55. ClassDB::bind_method(D_METHOD("get_affect_navigation_mesh"), &NavigationObstacle2D::get_affect_navigation_mesh);
  56. ClassDB::bind_method(D_METHOD("set_carve_navigation_mesh", "enabled"), &NavigationObstacle2D::set_carve_navigation_mesh);
  57. ClassDB::bind_method(D_METHOD("get_carve_navigation_mesh"), &NavigationObstacle2D::get_carve_navigation_mesh);
  58. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,500,0.01,suffix:px"), "set_radius", "get_radius");
  59. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "vertices"), "set_vertices", "get_vertices");
  60. ADD_GROUP("NavigationMesh", "");
  61. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "affect_navigation_mesh"), "set_affect_navigation_mesh", "get_affect_navigation_mesh");
  62. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "carve_navigation_mesh"), "set_carve_navigation_mesh", "get_carve_navigation_mesh");
  63. ADD_GROUP("Avoidance", "");
  64. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  65. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  66. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  67. }
  68. void NavigationObstacle2D::_notification(int p_what) {
  69. switch (p_what) {
  70. case NOTIFICATION_POST_ENTER_TREE: {
  71. if (map_override.is_valid()) {
  72. _update_map(map_override);
  73. } else if (is_inside_tree()) {
  74. _update_map(get_world_2d()->get_navigation_map());
  75. } else {
  76. _update_map(RID());
  77. }
  78. previous_transform = get_global_transform();
  79. // need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents
  80. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  81. _update_transform();
  82. set_physics_process_internal(true);
  83. #ifdef DEBUG_ENABLED
  84. RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, get_world_2d()->get_canvas());
  85. #endif // DEBUG_ENABLED
  86. } break;
  87. case NOTIFICATION_EXIT_TREE: {
  88. set_physics_process_internal(false);
  89. _update_map(RID());
  90. #ifdef DEBUG_ENABLED
  91. RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, RID());
  92. #endif // DEBUG_ENABLED
  93. } break;
  94. case NOTIFICATION_SUSPENDED:
  95. case NOTIFICATION_PAUSED: {
  96. if (!can_process()) {
  97. map_before_pause = map_current;
  98. _update_map(RID());
  99. } else if (can_process() && !(map_before_pause == RID())) {
  100. _update_map(map_before_pause);
  101. map_before_pause = RID();
  102. }
  103. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  104. } break;
  105. case NOTIFICATION_UNSUSPENDED: {
  106. if (get_tree()->is_paused()) {
  107. break;
  108. }
  109. [[fallthrough]];
  110. }
  111. case NOTIFICATION_UNPAUSED: {
  112. if (!can_process()) {
  113. map_before_pause = map_current;
  114. _update_map(RID());
  115. } else if (can_process() && !(map_before_pause == RID())) {
  116. _update_map(map_before_pause);
  117. map_before_pause = RID();
  118. }
  119. NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
  120. } break;
  121. case NOTIFICATION_VISIBILITY_CHANGED: {
  122. #ifdef DEBUG_ENABLED
  123. RS::get_singleton()->canvas_item_set_visible(debug_canvas_item, is_visible_in_tree());
  124. #endif // DEBUG_ENABLED
  125. } break;
  126. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  127. if (is_inside_tree()) {
  128. _update_transform();
  129. if (velocity_submitted) {
  130. velocity_submitted = false;
  131. // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same
  132. if (!previous_velocity.is_equal_approx(velocity)) {
  133. NavigationServer2D::get_singleton()->obstacle_set_velocity(obstacle, velocity);
  134. }
  135. previous_velocity = velocity;
  136. }
  137. }
  138. } break;
  139. case NOTIFICATION_DRAW: {
  140. #ifdef DEBUG_ENABLED
  141. if (is_inside_tree()) {
  142. bool is_debug_enabled = false;
  143. if (Engine::get_singleton()->is_editor_hint()) {
  144. is_debug_enabled = true;
  145. } else if (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_avoidance_enabled()) {
  146. is_debug_enabled = true;
  147. }
  148. if (is_debug_enabled) {
  149. RS::get_singleton()->canvas_item_clear(debug_canvas_item);
  150. RS::get_singleton()->canvas_item_set_transform(debug_canvas_item, Transform2D());
  151. _update_fake_agent_radius_debug();
  152. _update_static_obstacle_debug();
  153. }
  154. }
  155. #endif // DEBUG_ENABLED
  156. } break;
  157. }
  158. }
  159. NavigationObstacle2D::NavigationObstacle2D() {
  160. obstacle = NavigationServer2D::get_singleton()->obstacle_create();
  161. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, radius);
  162. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
  163. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  164. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  165. #ifdef DEBUG_ENABLED
  166. debug_canvas_item = RenderingServer::get_singleton()->canvas_item_create();
  167. debug_mesh_rid = RenderingServer::get_singleton()->mesh_create();
  168. #endif // DEBUG_ENABLED
  169. }
  170. NavigationObstacle2D::~NavigationObstacle2D() {
  171. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  172. NavigationServer2D::get_singleton()->free(obstacle);
  173. obstacle = RID();
  174. #ifdef DEBUG_ENABLED
  175. if (debug_mesh_rid.is_valid()) {
  176. RenderingServer::get_singleton()->free(debug_mesh_rid);
  177. debug_mesh_rid = RID();
  178. }
  179. if (debug_canvas_item.is_valid()) {
  180. RenderingServer::get_singleton()->free(debug_canvas_item);
  181. debug_canvas_item = RID();
  182. }
  183. #endif // DEBUG_ENABLED
  184. }
  185. void NavigationObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
  186. vertices = p_vertices;
  187. vertices_are_clockwise = !Geometry2D::is_polygon_clockwise(vertices); // Geometry2D is inverted.
  188. vertices_are_valid = !Geometry2D::triangulate_polygon(vertices).is_empty();
  189. const Transform2D node_transform = is_inside_tree() ? get_global_transform() : Transform2D();
  190. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, node_transform.xform(vertices));
  191. #ifdef DEBUG_ENABLED
  192. queue_redraw();
  193. #endif // DEBUG_ENABLED
  194. }
  195. void NavigationObstacle2D::set_navigation_map(RID p_navigation_map) {
  196. if (map_override == p_navigation_map) {
  197. return;
  198. }
  199. map_override = p_navigation_map;
  200. _update_map(map_override);
  201. }
  202. RID NavigationObstacle2D::get_navigation_map() const {
  203. if (map_override.is_valid()) {
  204. return map_override;
  205. } else if (is_inside_tree()) {
  206. return get_world_2d()->get_navigation_map();
  207. }
  208. return RID();
  209. }
  210. void NavigationObstacle2D::set_radius(real_t p_radius) {
  211. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  212. if (Math::is_equal_approx(radius, p_radius)) {
  213. return;
  214. }
  215. radius = p_radius;
  216. const Vector2 safe_scale = (is_inside_tree() ? get_global_scale() : get_scale()).abs().maxf(0.001);
  217. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, safe_scale[safe_scale.max_axis_index()] * radius);
  218. #ifdef DEBUG_ENABLED
  219. queue_redraw();
  220. #endif // DEBUG_ENABLED
  221. }
  222. void NavigationObstacle2D::set_avoidance_layers(uint32_t p_layers) {
  223. if (avoidance_layers == p_layers) {
  224. return;
  225. }
  226. avoidance_layers = p_layers;
  227. NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
  228. }
  229. uint32_t NavigationObstacle2D::get_avoidance_layers() const {
  230. return avoidance_layers;
  231. }
  232. void NavigationObstacle2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  233. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  234. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  235. uint32_t avoidance_layers_new = get_avoidance_layers();
  236. if (p_value) {
  237. avoidance_layers_new |= 1 << (p_layer_number - 1);
  238. } else {
  239. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  240. }
  241. set_avoidance_layers(avoidance_layers_new);
  242. }
  243. bool NavigationObstacle2D::get_avoidance_layer_value(int p_layer_number) const {
  244. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  245. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  246. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  247. }
  248. void NavigationObstacle2D::set_avoidance_enabled(bool p_enabled) {
  249. if (avoidance_enabled == p_enabled) {
  250. return;
  251. }
  252. avoidance_enabled = p_enabled;
  253. NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
  254. #ifdef DEBUG_ENABLED
  255. queue_redraw();
  256. #endif // DEBUG_ENABLED
  257. }
  258. bool NavigationObstacle2D::get_avoidance_enabled() const {
  259. return avoidance_enabled;
  260. }
  261. void NavigationObstacle2D::set_velocity(const Vector2 p_velocity) {
  262. velocity = p_velocity;
  263. velocity_submitted = true;
  264. }
  265. void NavigationObstacle2D::set_affect_navigation_mesh(bool p_enabled) {
  266. affect_navigation_mesh = p_enabled;
  267. }
  268. bool NavigationObstacle2D::get_affect_navigation_mesh() const {
  269. return affect_navigation_mesh;
  270. }
  271. void NavigationObstacle2D::set_carve_navigation_mesh(bool p_enabled) {
  272. carve_navigation_mesh = p_enabled;
  273. }
  274. bool NavigationObstacle2D::get_carve_navigation_mesh() const {
  275. return carve_navigation_mesh;
  276. }
  277. PackedStringArray NavigationObstacle2D::get_configuration_warnings() const {
  278. PackedStringArray warnings = Node2D::get_configuration_warnings();
  279. const Vector2 global_scale = get_global_scale();
  280. if (global_scale.x < 0.001 || global_scale.y < 0.001) {
  281. warnings.push_back(RTR("NavigationObstacle2D does not support negative or zero scaling."));
  282. }
  283. if (radius > 0.0 && !get_global_transform().is_conformal()) {
  284. warnings.push_back(RTR("The agent radius can only be scaled uniformly. The largest value along the two axes of the global scale will be used to scale the radius. This value may change in unexpected ways when the node is rotated."));
  285. }
  286. if (radius > 0.0 && get_global_skew() != 0.0) {
  287. warnings.push_back(RTR("Skew has no effect on the agent radius."));
  288. }
  289. return warnings;
  290. }
  291. void NavigationObstacle2D::navmesh_parse_init() {
  292. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  293. if (!_navmesh_source_geometry_parser.is_valid()) {
  294. _navmesh_source_geometry_parsing_callback = callable_mp_static(&NavigationObstacle2D::navmesh_parse_source_geometry);
  295. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  296. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  297. }
  298. }
  299. void NavigationObstacle2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  300. NavigationObstacle2D *obstacle = Object::cast_to<NavigationObstacle2D>(p_node);
  301. if (obstacle == nullptr) {
  302. return;
  303. }
  304. if (!obstacle->get_affect_navigation_mesh()) {
  305. return;
  306. }
  307. const Vector2 safe_scale = obstacle->get_global_scale().abs().maxf(0.001);
  308. const float obstacle_radius = obstacle->get_radius();
  309. if (obstacle_radius > 0.0) {
  310. // Radius defined obstacle should be uniformly scaled from obstacle basis max scale axis.
  311. const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
  312. const Vector2 uniform_max_scale = Vector2(scaling_max_value, scaling_max_value);
  313. const Transform2D obstacle_circle_transform = p_source_geometry_data->root_node_transform * Transform2D(obstacle->get_global_rotation(), uniform_max_scale, 0.0, obstacle->get_global_position());
  314. Vector<Vector2> obstruction_circle_vertices;
  315. // The point of this is that the moving obstacle can make a simple hole in the navigation mesh and affect the pathfinding.
  316. // Without, navigation paths can go directly through the middle of the obstacle and conflict with the avoidance to get agents stuck.
  317. // No place for excessive "round" detail here. Every additional edge adds a high cost for something that needs to be quick, not pretty.
  318. static const int circle_points = 12;
  319. obstruction_circle_vertices.resize(circle_points);
  320. Vector2 *circle_vertices_ptrw = obstruction_circle_vertices.ptrw();
  321. const real_t circle_point_step = Math_TAU / circle_points;
  322. for (int i = 0; i < circle_points; i++) {
  323. const float angle = i * circle_point_step;
  324. circle_vertices_ptrw[i] = obstacle_circle_transform.xform(Vector2(Math::cos(angle) * obstacle_radius, Math::sin(angle) * obstacle_radius));
  325. }
  326. p_source_geometry_data->add_projected_obstruction(obstruction_circle_vertices, obstacle->get_carve_navigation_mesh());
  327. }
  328. // Obstacles are projected to the xz-plane, so only rotation around the y-axis can be taken into account.
  329. const Transform2D node_xform = p_source_geometry_data->root_node_transform * obstacle->get_global_transform();
  330. const Vector<Vector2> &obstacle_vertices = obstacle->get_vertices();
  331. if (obstacle_vertices.is_empty()) {
  332. return;
  333. }
  334. Vector<Vector2> obstruction_shape_vertices;
  335. obstruction_shape_vertices.resize(obstacle_vertices.size());
  336. const Vector2 *obstacle_vertices_ptr = obstacle_vertices.ptr();
  337. Vector2 *obstruction_shape_vertices_ptrw = obstruction_shape_vertices.ptrw();
  338. for (int i = 0; i < obstacle_vertices.size(); i++) {
  339. obstruction_shape_vertices_ptrw[i] = node_xform.xform(obstacle_vertices_ptr[i]);
  340. }
  341. p_source_geometry_data->add_projected_obstruction(obstruction_shape_vertices, obstacle->get_carve_navigation_mesh());
  342. }
  343. void NavigationObstacle2D::_update_map(RID p_map) {
  344. map_current = p_map;
  345. NavigationServer2D::get_singleton()->obstacle_set_map(obstacle, p_map);
  346. }
  347. void NavigationObstacle2D::_update_position(const Vector2 p_position) {
  348. NavigationServer2D::get_singleton()->obstacle_set_position(obstacle, p_position);
  349. #ifdef DEBUG_ENABLED
  350. queue_redraw();
  351. #endif // DEBUG_ENABLED
  352. }
  353. void NavigationObstacle2D::_update_transform() {
  354. _update_position(get_global_position());
  355. // Prevent non-positive or non-uniform scaling of dynamic obstacle radius.
  356. const Vector2 safe_scale = get_global_scale().abs().maxf(0.001);
  357. const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
  358. NavigationServer2D::get_singleton()->obstacle_set_radius(obstacle, scaling_max_value * radius);
  359. NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, get_global_transform().translated(-get_global_position()).xform(vertices));
  360. #ifdef DEBUG_ENABLED
  361. queue_redraw();
  362. #endif // DEBUG_ENABLED
  363. }
  364. #ifdef DEBUG_ENABLED
  365. void NavigationObstacle2D::_update_fake_agent_radius_debug() {
  366. if (radius > 0.0 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) {
  367. Color debug_radius_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_color();
  368. // Prevent non-positive scaling.
  369. const Vector2 safe_scale = get_global_scale().abs().maxf(0.001);
  370. // Agent radius is a scalar value and does not support non-uniform scaling, choose the largest axis.
  371. const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
  372. RS::get_singleton()->canvas_item_add_circle(debug_canvas_item, get_global_position(), scaling_max_value * radius, debug_radius_color);
  373. }
  374. }
  375. #endif // DEBUG_ENABLED
  376. #ifdef DEBUG_ENABLED
  377. void NavigationObstacle2D::_update_static_obstacle_debug() {
  378. if (get_vertices().size() < 3) {
  379. return;
  380. }
  381. if (!NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_static()) {
  382. return;
  383. }
  384. RenderingServer *rs = RenderingServer::get_singleton();
  385. rs->mesh_clear(debug_mesh_rid);
  386. const int vertex_count = vertices.size();
  387. Vector<Vector2> edge_vertex_array;
  388. edge_vertex_array.resize(vertex_count * 4);
  389. Vector2 *edge_vertex_array_ptrw = edge_vertex_array.ptrw();
  390. int vertex_index = 0;
  391. for (int i = 0; i < vertex_count; i++) {
  392. Vector2 point = vertices[i];
  393. Vector2 next_point = vertices[(i + 1) % vertex_count];
  394. Vector2 direction = next_point.direction_to(point);
  395. Vector2 arrow_dir = -direction.orthogonal();
  396. Vector2 edge_middle = point + ((next_point - point) * 0.5);
  397. edge_vertex_array_ptrw[vertex_index++] = edge_middle;
  398. edge_vertex_array_ptrw[vertex_index++] = edge_middle + (arrow_dir * 10.0);
  399. edge_vertex_array_ptrw[vertex_index++] = point;
  400. edge_vertex_array_ptrw[vertex_index++] = next_point;
  401. }
  402. Color debug_static_obstacle_edge_color;
  403. if (are_vertices_valid()) {
  404. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_color();
  405. } else {
  406. debug_static_obstacle_edge_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_color();
  407. }
  408. Vector<Color> line_color_array;
  409. line_color_array.resize(edge_vertex_array.size());
  410. line_color_array.fill(debug_static_obstacle_edge_color);
  411. Array edge_mesh_array;
  412. edge_mesh_array.resize(Mesh::ARRAY_MAX);
  413. edge_mesh_array[Mesh::ARRAY_VERTEX] = edge_vertex_array;
  414. edge_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;
  415. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, edge_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  416. rs->canvas_item_add_mesh(debug_canvas_item, debug_mesh_rid, get_global_transform());
  417. }
  418. #endif // DEBUG_ENABLED