Skip to content

Commit c225686

Browse files
committed
Merge pull request #106490 from mihe/jolt/non-monitoring-area-performance
Improve performance with non-monitoring areas when using Jolt Physics
2 parents 176a965 + 617a39d commit c225686

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

modules/jolt_physics/objects/jolt_area_3d.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void JoltArea3D::_add_to_space() {
8383

8484
jolt_settings->SetShape(jolt_shape);
8585

86-
JPH::Body *new_jolt_body = space->add_rigid_body(*this, *jolt_settings);
86+
JPH::Body *new_jolt_body = space->add_rigid_body(*this, *jolt_settings, _should_sleep());
8787
if (new_jolt_body == nullptr) {
8888
return;
8989
}
@@ -275,6 +275,18 @@ void JoltArea3D::_force_areas_exited(bool p_remove) {
275275
}
276276
}
277277

278+
void JoltArea3D::_update_sleeping() {
279+
if (space == nullptr) {
280+
return;
281+
}
282+
283+
if (_should_sleep()) {
284+
space->get_body_iface().DeactivateBody(jolt_body->GetID());
285+
} else {
286+
space->get_body_iface().ActivateBody(jolt_body->GetID());
287+
}
288+
}
289+
278290
void JoltArea3D::_update_group_filter() {
279291
if (!in_space()) {
280292
return;
@@ -316,19 +328,23 @@ void JoltArea3D::_events_changed() {
316328
}
317329

318330
void JoltArea3D::_body_monitoring_changed() {
319-
if (has_body_monitor_callback()) {
331+
if (is_monitoring_bodies()) {
320332
_force_bodies_entered();
321333
} else {
322334
_force_bodies_exited(false);
323335
}
336+
337+
_update_sleeping();
324338
}
325339

326340
void JoltArea3D::_area_monitoring_changed() {
327-
if (has_area_monitor_callback()) {
341+
if (is_monitoring_areas()) {
328342
_force_areas_entered();
329343
} else {
330344
_force_areas_exited(false);
331345
}
346+
347+
_update_sleeping();
332348
}
333349

334350
void JoltArea3D::_monitorable_changed() {
@@ -513,15 +529,15 @@ void JoltArea3D::set_monitorable(bool p_monitorable) {
513529
}
514530

515531
bool JoltArea3D::can_monitor(const JoltBody3D &p_other) const {
516-
return (collision_mask & p_other.get_collision_layer()) != 0;
532+
return is_monitoring_bodies() && (collision_mask & p_other.get_collision_layer()) != 0;
517533
}
518534

519535
bool JoltArea3D::can_monitor(const JoltSoftBody3D &p_other) const {
520536
return false;
521537
}
522538

523539
bool JoltArea3D::can_monitor(const JoltArea3D &p_other) const {
524-
return p_other.is_monitorable() && (collision_mask & p_other.get_collision_layer()) != 0;
540+
return is_monitoring_areas() && p_other.is_monitorable() && (collision_mask & p_other.get_collision_layer()) != 0;
525541
}
526542

527543
bool JoltArea3D::can_interact_with(const JoltBody3D &p_other) const {

modules/jolt_physics/objects/jolt_area_3d.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class JoltArea3D final : public JoltShapedObject3D {
116116

117117
virtual JPH::EMotionType _get_motion_type() const override { return JPH::EMotionType::Kinematic; }
118118

119+
bool _should_sleep() const { return !is_monitoring(); }
120+
119121
virtual void _add_to_space() override;
120122

121123
void _enqueue_call_queries();
@@ -137,6 +139,7 @@ class JoltArea3D final : public JoltShapedObject3D {
137139
void _force_areas_entered();
138140
void _force_areas_exited(bool p_remove);
139141

142+
void _update_sleeping();
140143
void _update_group_filter();
141144
void _update_default_gravity();
142145

@@ -165,6 +168,10 @@ class JoltArea3D final : public JoltShapedObject3D {
165168
bool has_area_monitor_callback() const { return area_monitor_callback.is_valid(); }
166169
void set_area_monitor_callback(const Callable &p_callback);
167170

171+
bool is_monitoring_bodies() const { return has_body_monitor_callback(); }
172+
bool is_monitoring_areas() const { return has_area_monitor_callback(); }
173+
bool is_monitoring() const { return is_monitoring_bodies() || is_monitoring_areas(); }
174+
168175
bool is_monitorable() const { return monitorable; }
169176
void set_monitorable(bool p_monitorable);
170177

0 commit comments

Comments
 (0)