Skip to content

Commit

Permalink
Merge pull request #84799 from mihe/fix-83412
Browse files Browse the repository at this point in the history
Fix transform changes in `_integrate_forces` being overwritten
  • Loading branch information
akien-mga committed Nov 12, 2023
2 parents 4123f13 + 21a3ed1 commit 6415006
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 5 additions & 3 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ struct _RigidBody2DInOut {

void RigidBody2D::_sync_body_state(PhysicsDirectBodyState2D *p_state) {
if (!freeze || freeze_mode != FREEZE_MODE_KINEMATIC) {
set_block_transform_notify(true);
set_global_transform(p_state->get_transform());
set_block_transform_notify(false);
}

linear_velocity = p_state->get_linear_velocity();
Expand All @@ -446,16 +448,16 @@ void RigidBody2D::_sync_body_state(PhysicsDirectBodyState2D *p_state) {
void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
lock_callback();

set_block_transform_notify(true); // don't want notify (would feedback loop)

if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
_sync_body_state(p_state);

GDVIRTUAL_CALL(_integrate_forces, p_state);

// Update the physics server with any new transform, to prevent it from being overwritten at the sync below.
force_update_transform();
}

_sync_body_state(p_state);
set_block_transform_notify(false); // want it back

if (contact_monitor) {
contact_monitor->locked = true;
Expand Down
17 changes: 11 additions & 6 deletions scene/3d/physics_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ struct _RigidBodyInOut {
};

void RigidBody3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
set_ignore_transform_notification(true);
set_global_transform(p_state->get_transform());
set_ignore_transform_notification(false);

linear_velocity = p_state->get_linear_velocity();
angular_velocity = p_state->get_angular_velocity();
Expand All @@ -501,16 +503,16 @@ void RigidBody3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
lock_callback();

set_ignore_transform_notification(true);

if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
_sync_body_state(p_state);

GDVIRTUAL_CALL(_integrate_forces, p_state);

// Update the physics server with any new transform, to prevent it from being overwritten at the sync below.
force_update_transform();
}

_sync_body_state(p_state);
set_ignore_transform_notification(false);
_on_transform_changed();

if (contact_monitor) {
Expand Down Expand Up @@ -2927,7 +2929,10 @@ void PhysicalBone3D::_notification(int p_what) {
}

void PhysicalBone3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
set_ignore_transform_notification(true);
set_global_transform(p_state->get_transform());
set_ignore_transform_notification(false);

linear_velocity = p_state->get_linear_velocity();
angular_velocity = p_state->get_angular_velocity();
}
Expand All @@ -2937,16 +2942,16 @@ void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
return;
}

set_ignore_transform_notification(true);

if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
_sync_body_state(p_state);

GDVIRTUAL_CALL(_integrate_forces, p_state);

// Update the physics server with any new transform, to prevent it from being overwritten at the sync below.
force_update_transform();
}

_sync_body_state(p_state);
set_ignore_transform_notification(false);
_on_transform_changed();

Transform3D global_transform(p_state->get_transform());
Expand Down

0 comments on commit 6415006

Please sign in to comment.