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

Improve performance of certain physics queries when using Jolt Physics #101071

Merged
merged 1 commit into from
Feb 11, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 @@ -34,22 +34,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 @@ -109,14 +113,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 @@ -189,14 +196,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 @@ -220,7 +230,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