Skip to content

Commit

Permalink
Improve performance of certain physics queries when using Jolt Physics
Browse files Browse the repository at this point in the history
(cherry picked from commit 5d2a54e)
  • Loading branch information
mihe authored and Spartan322 committed Feb 12, 2025
1 parent b7cddae commit 25af5e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool JoltPhysicsDirectSpaceState3D::_cast_motion_impl(const JPH::Shape &p_jolt_s
aabb_translated.Translate(motion);
aabb.Encapsulate(aabb_translated);

JoltQueryCollectorAnyMulti<JPH::CollideShapeBodyCollector, 2048> aabb_collector;
JoltQueryCollectorAnyMulti<JPH::CollideShapeBodyCollector, 1024> aabb_collector;
space->get_broad_phase_query().CollideAABox(aabb, aabb_collector, p_broad_phase_layer_filter, p_object_layer_filter);

if (!aabb_collector.had_hit()) {
Expand Down
26 changes: 18 additions & 8 deletions modules/jolt_physics/spaces/jolt_query_collectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@
#include "../jolt_project_settings.h"
#include "jolt_space_3d.h"

#include "core/templates/local_vector.h"

#include "Jolt/Jolt.h"

#include "Jolt/Core/STLLocalAllocator.h"
#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
#include "Jolt/Physics/Collision/Shape/Shape.h"

template <typename TBase, int TDefaultCapacity>
class JoltQueryCollectorAll final : public TBase {
public:
typedef typename TBase::ResultType Hit;
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;

private:
JPH::Array<Hit> hits;
HitArray hits;

public:
JoltQueryCollectorAll() {
hits.reserve(TDefaultCapacity);
}

bool had_hit() const {
return !hits.is_empty();
}
Expand Down Expand Up @@ -111,14 +115,17 @@ template <typename TBase, int TDefaultCapacity>
class JoltQueryCollectorAnyMulti final : public TBase {
public:
typedef typename TBase::ResultType Hit;
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;

private:
JPH::Array<Hit> hits;
HitArray hits;
int max_hits = 0;

public:
explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :
max_hits(p_max_hits) {}
max_hits(p_max_hits) {
hits.reserve(TDefaultCapacity);
}

bool had_hit() const {
return hits.size() > 0;
Expand Down Expand Up @@ -191,14 +198,17 @@ template <typename TBase, int TDefaultCapacity>
class JoltQueryCollectorClosestMulti final : public TBase {
public:
typedef typename TBase::ResultType Hit;
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;

private:
JPH::Array<Hit> hits;
HitArray hits;
int max_hits = 0;

public:
explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :
max_hits(p_max_hits) {}
max_hits(p_max_hits) {
hits.reserve(TDefaultCapacity + 1);
}

bool had_hit() const {
return hits.size() > 0;
Expand All @@ -222,7 +232,7 @@ class JoltQueryCollectorClosestMulti final : public TBase {
}

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

0 comments on commit 25af5e7

Please sign in to comment.