Skip to content

Commit a30410b

Browse files
committed
Refactor post-step operations in Jolt module to be done as needed
1 parent 7b1ed52 commit a30410b

8 files changed

+88
-59
lines changed

modules/jolt_physics/objects/jolt_area_3d.cpp

+37-27
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,6 @@ JPH::ObjectLayer JoltArea3D::_get_object_layer() const {
5959
return space->map_to_object_layer(_get_broad_phase_layer(), collision_layer, collision_mask);
6060
}
6161

62-
bool JoltArea3D::_has_pending_events() const {
63-
if (body_monitor_callback.is_valid()) {
64-
for (const KeyValue<JPH::BodyID, Overlap> &E : bodies_by_id) {
65-
if (!E.value.pending_added.is_empty() || !E.value.pending_removed.is_empty()) {
66-
return true;
67-
}
68-
}
69-
}
70-
71-
if (area_monitor_callback.is_valid()) {
72-
for (const KeyValue<JPH::BodyID, Overlap> &E : areas_by_id) {
73-
if (!E.value.pending_added.is_empty() || !E.value.pending_removed.is_empty()) {
74-
return true;
75-
}
76-
}
77-
}
78-
79-
return false;
80-
}
81-
8262
void JoltArea3D::_add_to_space() {
8363
jolt_shape = build_shapes(true);
8464

@@ -136,6 +116,8 @@ void JoltArea3D::_add_shape_pair(Overlap &p_overlap, const JPH::BodyID &p_body_i
136116
shape_indices.self = find_shape_index(p_self_shape_id);
137117

138118
p_overlap.pending_added.push_back(shape_indices);
119+
120+
_events_changed();
139121
}
140122

141123
bool JoltArea3D::_remove_shape_pair(Overlap &p_overlap, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
@@ -148,6 +130,8 @@ bool JoltArea3D::_remove_shape_pair(Overlap &p_overlap, const JPH::SubShapeID &p
148130
p_overlap.pending_removed.push_back(shape_pair->value);
149131
p_overlap.shape_pairs.remove(shape_pair);
150132

133+
_events_changed();
134+
151135
return true;
152136
}
153137

@@ -224,10 +208,16 @@ void JoltArea3D::_force_bodies_entered() {
224208
for (KeyValue<JPH::BodyID, Overlap> &E : bodies_by_id) {
225209
Overlap &body = E.value;
226210

211+
if (unlikely(body.shape_pairs.is_empty())) {
212+
continue;
213+
}
214+
227215
for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : body.shape_pairs) {
228216
body.pending_removed.erase(P.value);
229217
body.pending_added.push_back(P.value);
230218
}
219+
220+
_events_changed();
231221
}
232222
}
233223

@@ -236,11 +226,17 @@ void JoltArea3D::_force_bodies_exited(bool p_remove) {
236226
const JPH::BodyID &id = E.key;
237227
Overlap &body = E.value;
238228

229+
if (unlikely(body.shape_pairs.is_empty())) {
230+
continue;
231+
}
232+
239233
for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : body.shape_pairs) {
240234
body.pending_added.erase(P.value);
241235
body.pending_removed.push_back(P.value);
242236
}
243237

238+
_events_changed();
239+
244240
if (p_remove) {
245241
body.shape_pairs.clear();
246242
_notify_body_exited(id);
@@ -252,22 +248,34 @@ void JoltArea3D::_force_areas_entered() {
252248
for (KeyValue<JPH::BodyID, Overlap> &E : areas_by_id) {
253249
Overlap &area = E.value;
254250

251+
if (unlikely(area.shape_pairs.is_empty())) {
252+
continue;
253+
}
254+
255255
for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : area.shape_pairs) {
256256
area.pending_removed.erase(P.value);
257257
area.pending_added.push_back(P.value);
258258
}
259+
260+
_events_changed();
259261
}
260262
}
261263

262264
void JoltArea3D::_force_areas_exited(bool p_remove) {
263265
for (KeyValue<JPH::BodyID, Overlap> &E : areas_by_id) {
264266
Overlap &area = E.value;
265267

268+
if (unlikely(area.shape_pairs.is_empty())) {
269+
continue;
270+
}
271+
266272
for (const KeyValue<ShapeIDPair, ShapeIndexPair> &P : area.shape_pairs) {
267273
area.pending_added.erase(P.value);
268274
area.pending_removed.push_back(P.value);
269275
}
270276

277+
_events_changed();
278+
271279
if (p_remove) {
272280
area.shape_pairs.clear();
273281
}
@@ -313,6 +321,10 @@ void JoltArea3D::_space_changed() {
313321
_update_default_gravity();
314322
}
315323

324+
void JoltArea3D::_events_changed() {
325+
_enqueue_call_queries();
326+
}
327+
316328
void JoltArea3D::_body_monitoring_changed() {
317329
if (has_body_monitor_callback()) {
318330
_force_bodies_entered();
@@ -664,11 +676,13 @@ void JoltArea3D::body_exited(const JPH::BodyID &p_body_id, bool p_notify) {
664676
return;
665677
}
666678

667-
for (KeyValue<ShapeIDPair, ShapeIndexPair> &E : overlap->shape_pairs) {
679+
for (const KeyValue<ShapeIDPair, ShapeIndexPair> &E : overlap->shape_pairs) {
668680
overlap->pending_added.erase(E.value);
669681
overlap->pending_removed.push_back(E.value);
670682
}
671683

684+
_events_changed();
685+
672686
overlap->shape_pairs.clear();
673687

674688
if (p_notify) {
@@ -691,16 +705,12 @@ void JoltArea3D::area_exited(const JPH::BodyID &p_body_id) {
691705
overlap->pending_removed.push_back(E.value);
692706
}
693707

708+
_events_changed();
709+
694710
overlap->shape_pairs.clear();
695711
}
696712

697713
void JoltArea3D::call_queries() {
698714
_flush_events(bodies_by_id, body_monitor_callback);
699715
_flush_events(areas_by_id, area_monitor_callback);
700716
}
701-
702-
void JoltArea3D::post_step(float p_step, JPH::Body &p_jolt_body) {
703-
if (_has_pending_events()) {
704-
_enqueue_call_queries();
705-
}
706-
}

modules/jolt_physics/objects/jolt_area_3d.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ class JoltArea3D final : public JoltShapedObject3D {
117117

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

120-
bool _has_pending_events() const;
121-
122120
virtual void _add_to_space() override;
123121

124122
void _enqueue_call_queries();
@@ -145,6 +143,7 @@ class JoltArea3D final : public JoltShapedObject3D {
145143

146144
virtual void _space_changing() override;
147145
virtual void _space_changed() override;
146+
void _events_changed();
148147
void _body_monitoring_changed();
149148
void _area_monitoring_changed();
150149
void _monitorable_changed();
@@ -228,8 +227,6 @@ class JoltArea3D final : public JoltShapedObject3D {
228227

229228
virtual bool has_custom_center_of_mass() const override { return false; }
230229
virtual Vector3 get_center_of_mass_custom() const override { return Vector3(); }
231-
232-
virtual void post_step(float p_step, JPH::Body &p_jolt_body) override;
233230
};
234231

235232
#endif // JOLT_AREA_3D_H

modules/jolt_physics/objects/jolt_object_3d.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ bool JoltObject3D::can_interact_with(const JoltObject3D &p_other) const {
136136
}
137137
}
138138

139-
void JoltObject3D::pre_step(float p_step, JPH::Body &p_jolt_body) {
140-
}
141-
142-
void JoltObject3D::post_step(float p_step, JPH::Body &p_jolt_body) {
143-
}
144-
145139
String JoltObject3D::to_string() const {
146140
Object *instance = get_instance();
147141
return instance != nullptr ? instance->to_string() : "<unknown>";

modules/jolt_physics/objects/jolt_object_3d.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ class JoltObject3D {
148148

149149
virtual bool reports_contacts() const = 0;
150150

151-
virtual void pre_step(float p_step, JPH::Body &p_jolt_body);
152-
virtual void post_step(float p_step, JPH::Body &p_jolt_body);
151+
virtual void pre_step(float p_step, JPH::Body &p_jolt_body) {}
153152

154153
String to_string() const;
155154
};

modules/jolt_physics/objects/jolt_shaped_object_3d.cpp

+24-8
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,26 @@ JPH::ShapeRefC JoltShapedObject3D::_try_build_compound_shape(bool p_optimize) {
140140
return shape_result.Get();
141141
}
142142

143+
void JoltShapedObject3D::_enqueue_shapes_changed() {
144+
if (space != nullptr) {
145+
space->enqueue_shapes_changed(&shapes_changed_element);
146+
}
147+
}
148+
149+
void JoltShapedObject3D::_dequeue_shapes_changed() {
150+
if (space != nullptr) {
151+
space->dequeue_shapes_changed(&shapes_changed_element);
152+
}
153+
}
154+
143155
void JoltShapedObject3D::_enqueue_needs_optimization() {
144-
if (!needs_optimization_element.in_list()) {
156+
if (space != nullptr) {
145157
space->enqueue_needs_optimization(&needs_optimization_element);
146158
}
147159
}
148160

149161
void JoltShapedObject3D::_dequeue_needs_optimization() {
150-
if (needs_optimization_element.in_list()) {
162+
if (space != nullptr) {
151163
space->dequeue_needs_optimization(&needs_optimization_element);
152164
}
153165
}
@@ -160,8 +172,11 @@ void JoltShapedObject3D::_shapes_changed() {
160172
void JoltShapedObject3D::_space_changing() {
161173
JoltObject3D::_space_changing();
162174

175+
_dequeue_shapes_changed();
163176
_dequeue_needs_optimization();
164177

178+
previous_jolt_shape = nullptr;
179+
165180
if (space != nullptr) {
166181
const JoltWritableBody3D body = space->write_body(jolt_id);
167182
ERR_FAIL_COND(body.is_invalid());
@@ -172,6 +187,7 @@ void JoltShapedObject3D::_space_changing() {
172187

173188
JoltShapedObject3D::JoltShapedObject3D(ObjectType p_object_type) :
174189
JoltObject3D(p_object_type),
190+
shapes_changed_element(this),
175191
needs_optimization_element(this) {
176192
jolt_settings->mAllowSleeping = true;
177193
jolt_settings->mFriction = 1.0f;
@@ -317,6 +333,8 @@ void JoltShapedObject3D::commit_shapes(bool p_optimize_compound) {
317333

318334
space->get_body_iface().SetShape(jolt_id, jolt_shape, false, JPH::EActivation::DontActivate);
319335

336+
_enqueue_shapes_changed();
337+
320338
if (!p_optimize_compound && jolt_shape->GetType() == JPH::EShapeType::Compound) {
321339
_enqueue_needs_optimization();
322340
} else {
@@ -369,6 +387,10 @@ void JoltShapedObject3D::clear_shapes() {
369387
_shapes_changed();
370388
}
371389

390+
void JoltShapedObject3D::clear_previous_shape() {
391+
previous_jolt_shape = nullptr;
392+
}
393+
372394
int JoltShapedObject3D::find_shape_index(uint32_t p_shape_instance_id) const {
373395
for (int i = 0; i < (int)shapes.size(); ++i) {
374396
if (shapes[i].get_id() == p_shape_instance_id) {
@@ -450,9 +472,3 @@ void JoltShapedObject3D::set_shape_disabled(int p_index, bool p_disabled) {
450472

451473
_shapes_changed();
452474
}
453-
454-
void JoltShapedObject3D::post_step(float p_step, JPH::Body &p_jolt_body) {
455-
JoltObject3D::post_step(p_step, p_jolt_body);
456-
457-
previous_jolt_shape = nullptr;
458-
}

modules/jolt_physics/objects/jolt_shaped_object_3d.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class JoltShapedObject3D : public JoltObject3D {
4444
friend class JoltShape3D;
4545

4646
protected:
47+
SelfList<JoltShapedObject3D> shapes_changed_element;
4748
SelfList<JoltShapedObject3D> needs_optimization_element;
4849

4950
Vector3 scale = Vector3(1, 1, 1);
@@ -61,6 +62,9 @@ class JoltShapedObject3D : public JoltObject3D {
6162
JPH::ShapeRefC _try_build_single_shape();
6263
JPH::ShapeRefC _try_build_compound_shape(bool p_optimize);
6364

65+
void _enqueue_shapes_changed();
66+
void _dequeue_shapes_changed();
67+
6468
void _enqueue_needs_optimization();
6569
void _dequeue_needs_optimization();
6670

@@ -106,6 +110,7 @@ class JoltShapedObject3D : public JoltObject3D {
106110
void set_shape(int p_index, JoltShape3D *p_shape);
107111

108112
void clear_shapes();
113+
void clear_previous_shape();
109114

110115
int get_shape_count() const { return shapes.size(); }
111116

@@ -123,8 +128,6 @@ class JoltShapedObject3D : public JoltObject3D {
123128

124129
bool is_shape_disabled(int p_index) const;
125130
void set_shape_disabled(int p_index, bool p_disabled);
126-
127-
virtual void post_step(float p_step, JPH::Body &p_jolt_body) override;
128131
};
129132

130133
#endif // JOLT_SHAPED_OBJECT_3D_H

modules/jolt_physics/spaces/jolt_space_3d.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,10 @@ void JoltSpace3D::_pre_step(float p_step) {
8585
void JoltSpace3D::_post_step(float p_step) {
8686
contact_listener->post_step();
8787

88-
// WARNING: The list of active bodies may have changed between `pre_step` and `post_step`.
89-
90-
const JPH::BodyLockInterface &lock_iface = get_lock_iface();
91-
const JPH::BodyID *active_rigid_bodies = physics_system->GetActiveBodiesUnsafe(JPH::EBodyType::RigidBody);
92-
const JPH::uint32 active_rigid_body_count = physics_system->GetNumActiveBodies(JPH::EBodyType::RigidBody);
93-
94-
for (JPH::uint32 i = 0; i < active_rigid_body_count; i++) {
95-
JPH::Body *jolt_body = lock_iface.TryGetBody(active_rigid_bodies[i]);
96-
JoltObject3D *object = reinterpret_cast<JoltObject3D *>(jolt_body->GetUserData());
97-
object->post_step(p_step, *jolt_body);
88+
while (shapes_changed_list.first()) {
89+
JoltShapedObject3D *object = shapes_changed_list.first()->self();
90+
shapes_changed_list.remove(shapes_changed_list.first());
91+
object->clear_previous_shape();
9892
}
9993
}
10094

@@ -433,6 +427,18 @@ void JoltSpace3D::dequeue_call_queries(SelfList<JoltArea3D> *p_area) {
433427
}
434428
}
435429

430+
void JoltSpace3D::enqueue_shapes_changed(SelfList<JoltShapedObject3D> *p_object) {
431+
if (!p_object->in_list()) {
432+
shapes_changed_list.add(p_object);
433+
}
434+
}
435+
436+
void JoltSpace3D::dequeue_shapes_changed(SelfList<JoltShapedObject3D> *p_object) {
437+
if (p_object->in_list()) {
438+
shapes_changed_list.remove(p_object);
439+
}
440+
}
441+
436442
void JoltSpace3D::enqueue_needs_optimization(SelfList<JoltShapedObject3D> *p_object) {
437443
if (!p_object->in_list()) {
438444
needs_optimization_list.add(p_object);

modules/jolt_physics/spaces/jolt_space_3d.h

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class JoltShapedObject3D;
5959
class JoltSpace3D {
6060
SelfList<JoltBody3D>::List body_call_queries_list;
6161
SelfList<JoltArea3D>::List area_call_queries_list;
62+
SelfList<JoltShapedObject3D>::List shapes_changed_list;
6263
SelfList<JoltShapedObject3D>::List needs_optimization_list;
6364

6465
RID rid;
@@ -142,6 +143,9 @@ class JoltSpace3D {
142143
void dequeue_call_queries(SelfList<JoltBody3D> *p_body);
143144
void dequeue_call_queries(SelfList<JoltArea3D> *p_area);
144145

146+
void enqueue_shapes_changed(SelfList<JoltShapedObject3D> *p_object);
147+
void dequeue_shapes_changed(SelfList<JoltShapedObject3D> *p_object);
148+
145149
void enqueue_needs_optimization(SelfList<JoltShapedObject3D> *p_object);
146150
void dequeue_needs_optimization(SelfList<JoltShapedObject3D> *p_object);
147151

0 commit comments

Comments
 (0)