Skip to content

Commit f1a6627

Browse files
committed
Improve performance of certain physics queries when using Jolt Physics
1 parent 296de7d commit f1a6627

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

modules/jolt_physics/spaces/jolt_query_collectors.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,26 @@
3434
#include "../jolt_project_settings.h"
3535
#include "jolt_space_3d.h"
3636

37-
#include "core/templates/local_vector.h"
38-
3937
#include "Jolt/Jolt.h"
4038

39+
#include "Jolt/Core/STLLocalAllocator.h"
4140
#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
4241
#include "Jolt/Physics/Collision/Shape/Shape.h"
4342

4443
template <typename TBase, int TDefaultCapacity>
4544
class JoltQueryCollectorAll final : public TBase {
4645
public:
4746
typedef typename TBase::ResultType Hit;
47+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
4848

4949
private:
50-
JPH::Array<Hit> hits;
50+
HitArray hits;
5151

5252
public:
53+
JoltQueryCollectorAll() {
54+
hits.reserve(TDefaultCapacity);
55+
}
56+
5357
bool had_hit() const {
5458
return !hits.is_empty();
5559
}
@@ -109,14 +113,17 @@ template <typename TBase, int TDefaultCapacity>
109113
class JoltQueryCollectorAnyMulti final : public TBase {
110114
public:
111115
typedef typename TBase::ResultType Hit;
116+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
112117

113118
private:
114-
JPH::Array<Hit> hits;
119+
HitArray hits;
115120
int max_hits = 0;
116121

117122
public:
118123
explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :
119-
max_hits(p_max_hits) {}
124+
max_hits(p_max_hits) {
125+
hits.reserve(TDefaultCapacity);
126+
}
120127

121128
bool had_hit() const {
122129
return hits.size() > 0;
@@ -189,14 +196,17 @@ template <typename TBase, int TDefaultCapacity>
189196
class JoltQueryCollectorClosestMulti final : public TBase {
190197
public:
191198
typedef typename TBase::ResultType Hit;
199+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;
192200

193201
private:
194-
JPH::Array<Hit> hits;
202+
HitArray hits;
195203
int max_hits = 0;
196204

197205
public:
198206
explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :
199-
max_hits(p_max_hits) {}
207+
max_hits(p_max_hits) {
208+
hits.reserve(TDefaultCapacity + 1);
209+
}
200210

201211
bool had_hit() const {
202212
return hits.size() > 0;
@@ -220,7 +230,7 @@ class JoltQueryCollectorClosestMulti final : public TBase {
220230
}
221231

222232
virtual void AddHit(const Hit &p_hit) override {
223-
typename JPH::Array<Hit>::const_iterator E = hits.cbegin();
233+
typename HitArray::const_iterator E = hits.cbegin();
224234
for (; E != hits.cend(); ++E) {
225235
if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {
226236
break;

0 commit comments

Comments
 (0)