Skip to content

Commit

Permalink
Merge pull request #88895 from smix8/navobstacle2d_debug_transform
Browse files Browse the repository at this point in the history
Fix NavigationObstacle2D debug being affected by Node2D transform
  • Loading branch information
akien-mga committed Feb 27, 2024
2 parents 10c3b00 + b044905 commit a586e86
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
33 changes: 30 additions & 3 deletions scene/2d/navigation_obstacle_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ void NavigationObstacle2D::_notification(int p_what) {
NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
_update_position(get_global_position());
set_physics_process_internal(true);
#ifdef DEBUG_ENABLED
RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, get_world_2d()->get_canvas());
#endif // DEBUG_ENABLED
} break;

case NOTIFICATION_EXIT_TREE: {
set_physics_process_internal(false);
_update_map(RID());
#ifdef DEBUG_ENABLED
RS::get_singleton()->canvas_item_set_parent(debug_canvas_item, RID());
#endif // DEBUG_ENABLED
} break;

case NOTIFICATION_PAUSED: {
Expand All @@ -110,6 +116,12 @@ void NavigationObstacle2D::_notification(int p_what) {
NavigationServer2D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
} break;

case NOTIFICATION_VISIBILITY_CHANGED: {
#ifdef DEBUG_ENABLED
RS::get_singleton()->canvas_item_set_visible(debug_canvas_item, is_visible_in_tree());
#endif // DEBUG_ENABLED
} break;

case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (is_inside_tree()) {
_update_position(get_global_position());
Expand All @@ -136,6 +148,9 @@ void NavigationObstacle2D::_notification(int p_what) {
}

if (is_debug_enabled) {
RS::get_singleton()->canvas_item_clear(debug_canvas_item);
Transform2D debug_transform = Transform2D(0.0, get_global_position());
RS::get_singleton()->canvas_item_set_transform(debug_canvas_item, debug_transform);
_update_fake_agent_radius_debug();
_update_static_obstacle_debug();
}
Expand All @@ -152,13 +167,24 @@ NavigationObstacle2D::NavigationObstacle2D() {
NavigationServer2D::get_singleton()->obstacle_set_vertices(obstacle, vertices);
NavigationServer2D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
NavigationServer2D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);

#ifdef DEBUG_ENABLED
debug_canvas_item = RenderingServer::get_singleton()->canvas_item_create();
#endif // DEBUG_ENABLED
}

NavigationObstacle2D::~NavigationObstacle2D() {
ERR_FAIL_NULL(NavigationServer2D::get_singleton());

NavigationServer2D::get_singleton()->free(obstacle);
obstacle = RID();

#ifdef DEBUG_ENABLED
if (debug_canvas_item.is_valid()) {
RenderingServer::get_singleton()->free(debug_canvas_item);
debug_canvas_item = RID();
}
#endif // DEBUG_ENABLED
}

void NavigationObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
Expand Down Expand Up @@ -267,7 +293,8 @@ void NavigationObstacle2D::_update_position(const Vector2 p_position) {
void NavigationObstacle2D::_update_fake_agent_radius_debug() {
if (radius > 0.0 && NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) {
Color debug_radius_color = NavigationServer2D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_color();
RS::get_singleton()->canvas_item_add_circle(get_canvas_item(), Vector2(), radius, debug_radius_color);

RS::get_singleton()->canvas_item_add_circle(debug_canvas_item, Vector2(), radius, debug_radius_color);
}
}
#endif // DEBUG_ENABLED
Expand All @@ -291,7 +318,7 @@ void NavigationObstacle2D::_update_static_obstacle_debug() {
debug_obstacle_polygon_colors.resize(debug_obstacle_polygon_vertices.size());
debug_obstacle_polygon_colors.fill(debug_static_obstacle_face_color);

RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), debug_obstacle_polygon_vertices, debug_obstacle_polygon_colors);
RS::get_singleton()->canvas_item_add_polygon(debug_canvas_item, debug_obstacle_polygon_vertices, debug_obstacle_polygon_colors);

Color debug_static_obstacle_edge_color;

Expand All @@ -309,7 +336,7 @@ void NavigationObstacle2D::_update_static_obstacle_debug() {
debug_obstacle_line_colors.resize(debug_obstacle_line_vertices.size());
debug_obstacle_line_colors.fill(debug_static_obstacle_edge_color);

RS::get_singleton()->canvas_item_add_polyline(get_canvas_item(), debug_obstacle_line_vertices, debug_obstacle_line_colors, 4.0);
RS::get_singleton()->canvas_item_add_polyline(debug_canvas_item, debug_obstacle_line_vertices, debug_obstacle_line_colors, 4.0);
}
}
#endif // DEBUG_ENABLED
1 change: 1 addition & 0 deletions scene/2d/navigation_obstacle_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class NavigationObstacle2D : public Node2D {

#ifdef DEBUG_ENABLED
private:
RID debug_canvas_item;
void _update_fake_agent_radius_debug();
void _update_static_obstacle_debug();
#endif // DEBUG_ENABLED
Expand Down

0 comments on commit a586e86

Please sign in to comment.