From a59e1a50bfe9796c505fee06527f7791eb8e9820 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sat, 5 Aug 2017 16:06:15 -0300 Subject: [PATCH] Warn about resizing a rigidbody (2D or 3D), covers the most common cases, closes #7615 (cherry picked from commit de9fb90dbf9473b91e75edbd063fb96f9ea4d262) --- scene/2d/physics_body_2d.cpp | 34 ++++++++++++++++++++++++++++++++++ scene/2d/physics_body_2d.h | 3 +++ scene/3d/physics_body.cpp | 31 +++++++++++++++++++++++++++++++ scene/3d/physics_body.h | 2 ++ 4 files changed, 70 insertions(+) diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index b6f451c3fd2..5290c06b6a3 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -830,6 +830,40 @@ bool RigidBody2D::is_contact_monitor_enabled() const { return contact_monitor != NULL; } +void RigidBody2D::_notification(int p_what) { + +#ifdef TOOLS_ENABLED + if (p_what == NOTIFICATION_ENTER_TREE) { + if (get_tree()->is_editor_hint()) { + set_notify_local_transform(true); //used for warnings and only in editor + } + } + + if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) { + if (get_tree()->is_editor_hint()) { + update_configuration_warning(); + } + } + +#endif +} + +String RigidBody2D::get_configuration_warning() const { + + Matrix32 t = get_transform(); + + String warning = CollisionObject2D::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()) { + warning += "\n"; + } + warning += TTR("Size changes to RigidBody2D (in character or rigid modes) will be overriden by the physics engine when running.\nChange the size in children collision shapes instead."); + } + + return warning; +} + void RigidBody2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_mode", "mode"), &RigidBody2D::set_mode); diff --git a/scene/2d/physics_body_2d.h b/scene/2d/physics_body_2d.h index dc0d45bcf51..cd603c21bcd 100644 --- a/scene/2d/physics_body_2d.h +++ b/scene/2d/physics_body_2d.h @@ -197,6 +197,7 @@ private: bool _test_motion(const Vector2 &p_motion, float p_margin = 0.08, const Ref &p_result = Ref()); protected: + void _notification(int p_what); static void _bind_methods(); public: @@ -265,6 +266,8 @@ public: Array get_colliding_bodies() const; //function for script + virtual String get_configuration_warning() const; + RigidBody2D(); ~RigidBody2D(); }; diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp index a84f98aa412..1c54d47ba04 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -472,6 +472,21 @@ void RigidBody::_direct_state_changed(Object *p_state) { } void RigidBody::_notification(int p_what) { + +#ifdef TOOLS_ENABLED + if (p_what == NOTIFICATION_ENTER_TREE) { + if (get_tree()->is_editor_hint()) { + set_notify_local_transform(true); //used for warnings and only in editor + } + } + + if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) { + if (get_tree()->is_editor_hint()) { + update_configuration_warning(); + } + } + +#endif } void RigidBody::set_mode(Mode p_mode) { @@ -746,6 +761,22 @@ Array RigidBody::get_colliding_bodies() const { return ret; } +String RigidBody::get_configuration_warning() const { + + Transform t = get_transform(); + + String warning = CollisionObject::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(0).length() - 1.0) > 0.05)) { + if (warning != String()) { + warning += "\n"; + } + warning += TTR("Size changes to RigidBody (in character or rigid modes) will be overriden by the physics engine when running.\nChange the size in children collision shapes instead."); + } + + return warning; +} + void RigidBody::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_mode", "mode"), &RigidBody::set_mode); diff --git a/scene/3d/physics_body.h b/scene/3d/physics_body.h index 67529426c97..93e2a076227 100644 --- a/scene/3d/physics_body.h +++ b/scene/3d/physics_body.h @@ -252,6 +252,8 @@ public: void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse); + virtual String get_configuration_warning() const; + RigidBody(); ~RigidBody(); };