Merge pull request #61057 from smix8/navigation_obstacle_transform_error_4.x

This commit is contained in:
Rémi Verschelde 2022-05-16 13:47:28 +02:00 committed by GitHub
commit f95b7412c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

@ -82,7 +82,7 @@ void NavigationObstacle2D::_notification(int p_what) {
} break; } break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (parent_node2d) { if (parent_node2d && parent_node2d->is_inside_tree()) {
NavigationServer2D::get_singleton()->agent_set_position(agent, parent_node2d->get_global_position()); NavigationServer2D::get_singleton()->agent_set_position(agent, parent_node2d->get_global_position());
} }
} break; } break;
@ -130,13 +130,13 @@ void NavigationObstacle2D::reevaluate_agent_radius() {
} }
real_t NavigationObstacle2D::estimate_agent_radius() const { real_t NavigationObstacle2D::estimate_agent_radius() const {
if (parent_node2d) { if (parent_node2d && parent_node2d->is_inside_tree()) {
// Estimate the radius of this physics body // Estimate the radius of this physics body
real_t radius = 0.0; real_t radius = 0.0;
for (int i(0); i < parent_node2d->get_child_count(); i++) { for (int i(0); i < parent_node2d->get_child_count(); i++) {
// For each collision shape // For each collision shape
CollisionShape2D *cs = Object::cast_to<CollisionShape2D>(parent_node2d->get_child(i)); CollisionShape2D *cs = Object::cast_to<CollisionShape2D>(parent_node2d->get_child(i));
if (cs) { if (cs && cs->is_inside_tree()) {
// Take the distance between the Body center to the shape center // Take the distance between the Body center to the shape center
real_t r = cs->get_transform().get_origin().length(); real_t r = cs->get_transform().get_origin().length();
if (cs->get_shape().is_valid()) { if (cs->get_shape().is_valid()) {
@ -147,6 +147,9 @@ real_t NavigationObstacle2D::estimate_agent_radius() const {
r *= MAX(s.x, s.y); r *= MAX(s.x, s.y);
// Takes the biggest radius // Takes the biggest radius
radius = MAX(radius, r); radius = MAX(radius, r);
} else if (cs && !cs->is_inside_tree()) {
WARN_PRINT("A CollisionShape2D of the NavigationObstacle2D parent node was not inside the SceneTree when estimating the obstacle radius."
"\nMove the NavigationObstacle2D to a child position below any CollisionShape2D node of the parent node so the CollisionShape2D is already inside the SceneTree.");
} }
} }
Vector2 s = parent_node2d->get_global_scale(); Vector2 s = parent_node2d->get_global_scale();

View File

@ -81,7 +81,7 @@ void NavigationObstacle3D::_notification(int p_what) {
} break; } break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (parent_node3d) { if (parent_node3d && parent_node3d->is_inside_tree()) {
NavigationServer3D::get_singleton()->agent_set_position(agent, parent_node3d->get_global_transform().origin); NavigationServer3D::get_singleton()->agent_set_position(agent, parent_node3d->get_global_transform().origin);
PhysicsBody3D *rigid = Object::cast_to<PhysicsBody3D>(get_parent()); PhysicsBody3D *rigid = Object::cast_to<PhysicsBody3D>(get_parent());
@ -136,13 +136,13 @@ void NavigationObstacle3D::reevaluate_agent_radius() {
} }
real_t NavigationObstacle3D::estimate_agent_radius() const { real_t NavigationObstacle3D::estimate_agent_radius() const {
if (parent_node3d) { if (parent_node3d && parent_node3d->is_inside_tree()) {
// Estimate the radius of this physics body // Estimate the radius of this physics body
real_t radius = 0.0; real_t radius = 0.0;
for (int i(0); i < parent_node3d->get_child_count(); i++) { for (int i(0); i < parent_node3d->get_child_count(); i++) {
// For each collision shape // For each collision shape
CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(parent_node3d->get_child(i)); CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(parent_node3d->get_child(i));
if (cs) { if (cs && cs->is_inside_tree()) {
// Take the distance between the Body center to the shape center // Take the distance between the Body center to the shape center
real_t r = cs->get_transform().origin.length(); real_t r = cs->get_transform().origin.length();
if (cs->get_shape().is_valid()) { if (cs->get_shape().is_valid()) {
@ -153,6 +153,9 @@ real_t NavigationObstacle3D::estimate_agent_radius() const {
r *= MAX(s.x, MAX(s.y, s.z)); r *= MAX(s.x, MAX(s.y, s.z));
// Takes the biggest radius // Takes the biggest radius
radius = MAX(radius, r); radius = MAX(radius, r);
} else if (cs && !cs->is_inside_tree()) {
WARN_PRINT("A CollisionShape3D of the NavigationObstacle3D parent node was not inside the SceneTree when estimating the obstacle radius."
"\nMove the NavigationObstacle3D to a child position below any CollisionShape3D node of the parent node so the CollisionShape3D is already inside the SceneTree.");
} }
} }