Rework migration of animate_physical_bones to compatibility

This commit is contained in:
Silc Lizard (Tokage) Renew 2024-06-23 16:05:04 +09:00
parent 6b281c0c07
commit 793f3832e5
5 changed files with 14 additions and 9 deletions

View File

@ -5,6 +5,7 @@
</brief_description> </brief_description>
<description> <description>
The [PhysicalBone3D] node is a physics body that can be used to make bones in a [Skeleton3D] react to physics. The [PhysicalBone3D] node is a physics body that can be used to make bones in a [Skeleton3D] react to physics.
[b]Note:[/b] In order to detect physical bones with raycasts, the [member SkeletonModifier3D.active] property of the parent [PhysicalBoneSimulator3D] must be [code]true[/code] and the [Skeleton3D]'s bone must be assigned to [PhysicalBone3D] correctly; it means that [method get_bone_id] should return a valid id ([code]&gt;= 0[/code]).
</description> </description>
<tutorials> <tutorials>
</tutorials> </tutorials>

View File

@ -243,6 +243,8 @@
<return type="void" /> <return type="void" />
<param index="0" name="enabled" type="bool" /> <param index="0" name="enabled" type="bool" />
<description> <description>
This method exists for compatibility with old structures in which the [Skeleton3D] does not have a [PhysicalBoneSimulator3D] as a child, but directly has [PhysicalBone3D]s as children.
In case you need to raycast to it without running [method physical_bones_start_simulation], call this method with [code]enabled == true[/code].
</description> </description>
</method> </method>
<method name="set_bone_enabled"> <method name="set_bone_enabled">

View File

@ -764,7 +764,7 @@ void PhysicalBone3D::_notification(int p_what) {
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
PhysicalBoneSimulator3D *simulator = get_simulator(); PhysicalBoneSimulator3D *simulator = get_simulator();
if (simulator) { if (simulator) {
if (-1 != bone_id) { if (bone_id != -1) {
simulator->unbind_physical_bone_from_bone(bone_id); simulator->unbind_physical_bone_from_bone(bone_id);
bone_id = -1; bone_id = -1;
} }
@ -816,7 +816,7 @@ void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
PhysicalBoneSimulator3D *simulator = get_simulator(); PhysicalBoneSimulator3D *simulator = get_simulator();
Skeleton3D *skeleton = get_skeleton(); Skeleton3D *skeleton = get_skeleton();
if (simulator && skeleton) { if (simulator && skeleton) {
if (-1 != bone_id) { if (bone_id != -1) {
simulator->set_bone_global_pose(bone_id, skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse)); simulator->set_bone_global_pose(bone_id, skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse));
} }
} }
@ -1293,7 +1293,7 @@ void PhysicalBone3D::update_bone_id() {
const int new_bone_id = simulator->find_bone(bone_name); const int new_bone_id = simulator->find_bone(bone_name);
if (new_bone_id != bone_id) { if (new_bone_id != bone_id) {
if (-1 != bone_id) { if (bone_id != -1) {
// Assert the unbind from old node // Assert the unbind from old node
simulator->unbind_physical_bone_from_bone(bone_id); simulator->unbind_physical_bone_from_bone(bone_id);
} }
@ -1313,7 +1313,7 @@ void PhysicalBone3D::update_offset() {
Skeleton3D *skeleton = get_skeleton(); Skeleton3D *skeleton = get_skeleton();
if (simulator && skeleton) { if (simulator && skeleton) {
Transform3D bone_transform(skeleton->get_global_transform()); Transform3D bone_transform(skeleton->get_global_transform());
if (-1 != bone_id) { if (bone_id != -1) {
bone_transform *= simulator->get_bone_global_pose(bone_id); bone_transform *= simulator->get_bone_global_pose(bone_id);
} }
@ -1328,7 +1328,7 @@ void PhysicalBone3D::update_offset() {
} }
void PhysicalBone3D::_start_physics_simulation() { void PhysicalBone3D::_start_physics_simulation() {
if (_internal_simulate_physics || !simulator_id.is_valid()) { if (_internal_simulate_physics || !simulator_id.is_valid() || bone_id == -1) {
return; return;
} }
reset_to_rest_position(); reset_to_rest_position();
@ -1344,7 +1344,7 @@ void PhysicalBone3D::_start_physics_simulation() {
void PhysicalBone3D::_stop_physics_simulation() { void PhysicalBone3D::_stop_physics_simulation() {
PhysicalBoneSimulator3D *simulator = get_simulator(); PhysicalBoneSimulator3D *simulator = get_simulator();
if (simulator) { if (simulator) {
if (simulator->is_simulating_physics()) { if (simulator->is_active() && bone_id != -1) {
set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC); set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer()); PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask()); PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());

View File

@ -1140,7 +1140,8 @@ void Skeleton3D::set_animate_physical_bones(bool p_enabled) {
if (!sim) { if (!sim) {
return; return;
} }
sim->set_active(p_enabled); animate_physical_bones = p_enabled;
sim->set_active(animate_physical_bones || sim->is_simulating_physics());
} }
bool Skeleton3D::get_animate_physical_bones() const { bool Skeleton3D::get_animate_physical_bones() const {
@ -1148,7 +1149,7 @@ bool Skeleton3D::get_animate_physical_bones() const {
if (!sim) { if (!sim) {
return false; return false;
} }
return sim->is_active(); return animate_physical_bones;
} }
void Skeleton3D::physical_bones_stop_simulation() { void Skeleton3D::physical_bones_stop_simulation() {
@ -1157,7 +1158,7 @@ void Skeleton3D::physical_bones_stop_simulation() {
return; return;
} }
sim->physical_bones_stop_simulation(); sim->physical_bones_stop_simulation();
sim->set_active(false); sim->set_active(animate_physical_bones || sim->is_simulating_physics());
} }
void Skeleton3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) { void Skeleton3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) {

View File

@ -67,6 +67,7 @@ class Skeleton3D : public Node3D {
GDCLASS(Skeleton3D, Node3D); GDCLASS(Skeleton3D, Node3D);
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
bool animate_physical_bones = false;
Node *simulator = nullptr; Node *simulator = nullptr;
void setup_simulator(); void setup_simulator();
#endif // _DISABLE_DEPRECATED #endif // _DISABLE_DEPRECATED