Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state sync after call to _integrate_forces in _body_state_changed #79977

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,7 @@ struct _RigidBody2DInOut {
int local_shape = 0;
};

void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
lock_callback();

set_block_transform_notify(true); // don't want notify (would feedback loop)
void RigidBody2D::_sync_body_state(PhysicsDirectBodyState2D *p_state) {
if (!freeze || freeze_mode != FREEZE_MODE_KINEMATIC) {
set_global_transform(p_state->get_transform());
}
Expand All @@ -444,9 +441,17 @@ void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
sleeping = p_state->is_sleeping();
emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
}
}

void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
lock_callback();

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

GDVIRTUAL_CALL(_integrate_forces, p_state);

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

if (contact_monitor) {
Expand Down
2 changes: 2 additions & 0 deletions scene/2d/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class RigidBody2D : public PhysicsBody2D {
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState2D *p_state);
void _body_state_changed(PhysicsDirectBodyState2D *p_state);

void _sync_body_state(PhysicsDirectBodyState2D *p_state);

protected:
void _notification(int p_what);
static void _bind_methods();
Expand Down
32 changes: 20 additions & 12 deletions scene/3d/physics_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,7 @@ struct _RigidBodyInOut {
int local_shape = 0;
};

void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
lock_callback();

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

linear_velocity = p_state->get_linear_velocity();
Expand All @@ -499,9 +496,17 @@ void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
sleeping = p_state->is_sleeping();
emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
}
}

void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
lock_callback();

set_ignore_transform_notification(true);
_sync_body_state(p_state);

GDVIRTUAL_CALL(_integrate_forces, p_state);

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

Expand Down Expand Up @@ -2915,25 +2920,28 @@ void PhysicalBone3D::_notification(int p_what) {
}
}

void PhysicalBone3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
set_global_transform(p_state->get_transform());
linear_velocity = p_state->get_linear_velocity();
angular_velocity = p_state->get_angular_velocity();
}

void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
if (!simulate_physics || !_internal_simulate_physics) {
return;
}

linear_velocity = p_state->get_linear_velocity();
angular_velocity = p_state->get_angular_velocity();
set_ignore_transform_notification(true);
_sync_body_state(p_state);

GDVIRTUAL_CALL(_integrate_forces, p_state);

/// Update bone transform.

Transform3D global_transform(p_state->get_transform());

set_ignore_transform_notification(true);
set_global_transform(global_transform);
_sync_body_state(p_state);
set_ignore_transform_notification(false);
_on_transform_changed();

Transform3D global_transform(p_state->get_transform());

// Update skeleton
if (parent_skeleton) {
if (-1 != bone_id) {
Expand Down
3 changes: 3 additions & 0 deletions scene/3d/physics_body_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class RigidBody3D : public PhysicsBody3D {
void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);

void _sync_body_state(PhysicsDirectBodyState3D *p_state);

protected:
void _notification(int p_what);
static void _bind_methods();
Expand Down Expand Up @@ -692,6 +694,7 @@ class PhysicalBone3D : public PhysicsBody3D {
static void _bind_methods();

private:
void _sync_body_state(PhysicsDirectBodyState3D *p_state);
static Skeleton3D *find_skeleton_parent(Node *p_parent);

void _update_joint_offset();
Expand Down