From 9ad73062e85962c10965fefa8493dc7f4702eafc Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Thu, 27 Feb 2025 10:38:06 +0000 Subject: [PATCH] Physics Interpolation - Add editor warning for non-interpolated physics body --- scene/2d/physics_body_2d.cpp | 15 ++++++++++++++- scene/2d/physics_body_2d.h | 2 ++ scene/3d/physics_body.cpp | 15 ++++++++++++++- scene/3d/physics_body.h | 2 ++ scene/main/node.cpp | 2 ++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index f05792c3cd8..32f61124c84 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -60,6 +60,19 @@ void PhysicsBody2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", 0), "_set_layers", "_get_layers"); //for backwards compat } +String PhysicsBody2D::get_configuration_warning() const { + String warning = CollisionObject2D::get_configuration_warning(); + + if (!is_physics_interpolated()) { + if (!warning.empty()) { + warning += "\n\n"; + } + warning += TTR("PhysicsBody2D will not work correctly on a non-interpolated branch of the SceneTree.\nCheck the node's inherited physics_interpolation_mode."); + } + + return warning; +} + PhysicsBody2D::PhysicsBody2D(Physics2DServer::BodyMode p_mode) : CollisionObject2D(RID_PRIME(Physics2DServer::get_singleton()->body_create()), false) { Physics2DServer::get_singleton()->body_set_mode(get_rid(), p_mode); @@ -808,7 +821,7 @@ void RigidBody2D::_notification(int p_what) { String RigidBody2D::get_configuration_warning() const { Transform2D t = get_transform(); - String warning = CollisionObject2D::get_configuration_warning(); + String warning = PhysicsBody2D::get_configuration_warning(); if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.elements[0].length() - 1.0) > 0.05 || ABS(t.elements[1].length() - 1.0) > 0.05)) { if (warning != String()) { diff --git a/scene/2d/physics_body_2d.h b/scene/2d/physics_body_2d.h index a7aee5f01c9..95b8be34cb1 100644 --- a/scene/2d/physics_body_2d.h +++ b/scene/2d/physics_body_2d.h @@ -51,6 +51,8 @@ protected: static void _bind_methods(); public: + virtual String get_configuration_warning() const; + Array get_collision_exceptions(); void add_collision_exception_with(Node *p_node); //must be physicsbody void remove_collision_exception_with(Node *p_node); diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp index 1a5f642f5a6..7fb917a7e3a 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -98,6 +98,19 @@ void PhysicsBody::_bind_methods() { ClassDB::bind_method(D_METHOD("_get_layers"), &PhysicsBody::_get_layers); } +String PhysicsBody::get_configuration_warning() const { + String warning = CollisionObject::get_configuration_warning(); + + if (!is_physics_interpolated()) { + if (!warning.empty()) { + warning += "\n\n"; + } + warning += TTR("PhysicsBody will not work correctly on a non-interpolated branch of the SceneTree.\nCheck the node's inherited physics_interpolation_mode."); + } + + return warning; +} + PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode) : CollisionObject(RID_PRIME(PhysicsServer::get_singleton()->body_create(p_mode)), false) { } @@ -783,7 +796,7 @@ Array RigidBody::get_colliding_bodies() const { String RigidBody::get_configuration_warning() const { Transform t = get_transform(); - String warning = CollisionObject::get_configuration_warning(); + String warning = PhysicsBody::get_configuration_warning(); if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(2).length() - 1.0) > 0.05)) { if (warning != String()) { diff --git a/scene/3d/physics_body.h b/scene/3d/physics_body.h index cf6ef2debe5..d1662c3d881 100644 --- a/scene/3d/physics_body.h +++ b/scene/3d/physics_body.h @@ -49,6 +49,8 @@ protected: PhysicsBody(PhysicsServer::BodyMode p_mode); public: + virtual String get_configuration_warning() const; + virtual Vector3 get_linear_velocity() const; virtual Vector3 get_angular_velocity() const; virtual float get_inverse_mass() const; diff --git a/scene/main/node.cpp b/scene/main/node.cpp index b13e61ddf2f..91cc9d7f6dd 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -219,6 +219,8 @@ void Node::_propagate_physics_interpolated(bool p_interpolated) { // allow a call to the VisualServer etc in derived classes _physics_interpolated_changed(); + update_configuration_warning(); + data.blocked++; for (int i = 0; i < data.children.size(); i++) { data.children[i]->_propagate_physics_interpolated(p_interpolated);