Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix PhysicalBone3D joint constrains revert

kobewic449246e06a0
diff --git a/scene/3d/physics/physical_bone_3d.cpp b/scene/3d/physics/physical_bone_3d.cpp
index 47572ec3b4d..6d79c17752c 100644
--- a/scene/3d/physics/physical_bone_3d.cpp
+++ b/scene/3d/physics/physical_bone_3d.cpp
@@ -927,6 +927,16 @@ void PhysicalBone3D::_bind_methods() {
BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
}
+template <typename T>
+void PhysicalBone3D::_make_joint_data() {
+ joint_data = memnew(T);
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ joint_defaults = memnew(T);
+ }
+#endif
+}
+
void PhysicalBone3D::_update_joint_offset() {
_fix_joint_offset();
@@ -1101,21 +1111,26 @@ void PhysicalBone3D::set_joint_type(JointType p_joint_type) {
memdelete(joint_data);
joint_data = nullptr;
+#ifdef TOOLS_ENABLED
+ memdelete(joint_defaults);
+ joint_defaults = nullptr;
+#endif
+
switch (p_joint_type) {
case JOINT_TYPE_PIN:
- joint_data = memnew(PinJointData);
+ _make_joint_data<PinJointData>();
break;
case JOINT_TYPE_CONE:
- joint_data = memnew(ConeJointData);
+ _make_joint_data<ConeJointData>();
break;
case JOINT_TYPE_HINGE:
- joint_data = memnew(HingeJointData);
+ _make_joint_data<HingeJointData>();
break;
case JOINT_TYPE_SLIDER:
- joint_data = memnew(SliderJointData);
+ _make_joint_data<SliderJointData>();
break;
case JOINT_TYPE_6DOF:
- joint_data = memnew(SixDOFJointData);
+ _make_joint_data<SixDOFJointData>();
break;
case JOINT_TYPE_NONE:
break;
@@ -1291,6 +1306,9 @@ PhysicalBone3D::PhysicalBone3D() :
PhysicalBone3D::~PhysicalBone3D() {
memdelete(joint_data);
+#ifdef TOOLS_ENABLED
+ memdelete(joint_defaults);
+#endif
ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
PhysicsServer3D::get_singleton()->free_rid(joint);
}
@@ -1373,3 +1391,20 @@ void PhysicalBone3D::_stop_physics_simulation() {
_internal_simulate_physics = false;
}
}
+
+#ifdef TOOLS_ENABLED
+bool PhysicalBone3D::_property_can_revert(const StringName &p_name) const {
+ if (joint_defaults) {
+ Variant ret;
+ return joint_defaults->_get(p_name, ret);
+ }
+ return false;
+}
+
+bool PhysicalBone3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
+ if (joint_defaults) {
+ return joint_defaults->_get(p_name, r_property);
+ }
+ return false;
+}
+#endif
diff --git a/scene/3d/physics/physical_bone_3d.h b/scene/3d/physics/physical_bone_3d.h
index 48f992e5fdd..c2518229e0c 100644
--- a/scene/3d/physics/physical_bone_3d.h
+++ b/scene/3d/physics/physical_bone_3d.h
@@ -162,6 +162,7 @@ private:
#ifdef TOOLS_ENABLED
// if false gizmo move body
bool gizmo_move_joint = false;
+ JointData *joint_defaults = nullptr;
#endif
JointData *joint_data = nullptr;
@@ -196,6 +197,10 @@ protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
+#ifdef TOOLS_ENABLED
+ bool _property_can_revert(const StringName &p_name) const;
+ bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
+#endif
void _notification(int p_what);
GDVIRTUAL1(_integrate_forces, RequiredParam<PhysicsDirectBodyState3D>)
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
@@ -206,6 +211,8 @@ protected:
private:
void _sync_body_state(PhysicsDirectBodyState3D *p_state);
+ template <typename T>
+ void _make_joint_data();
void _update_joint_offset();
void _fix_joint_offset();
void _reload_joint();