Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CollisionCollector.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
9class Body;
11
14{
15public:
17 static constexpr float InitialEarlyOutFraction = 1.0f + FLT_EPSILON;
18 static constexpr float ShouldEarlyOutFraction = 0.0f;
19};
20
23{
24public:
26 static constexpr float InitialEarlyOutFraction = 1.0f + FLT_EPSILON;
27 static constexpr float ShouldEarlyOutFraction = -FLT_MAX;
28};
29
32{
33public:
35 static constexpr float InitialEarlyOutFraction = FLT_MAX;
36 static constexpr float ShouldEarlyOutFraction = -FLT_MAX;
37};
38
41
43template <class ResultTypeArg, class TraitsType>
45{
46public:
48 using ResultType = ResultTypeArg;
49
51 CollisionCollector() = default;
52
54 template <class ResultTypeArg2>
55 explicit CollisionCollector(const CollisionCollector<ResultTypeArg2, TraitsType> &inRHS) : mEarlyOutFraction(inRHS.GetEarlyOutFraction()), mContext(inRHS.GetContext()) { }
57
59 virtual ~CollisionCollector() = default;
60
62 virtual void Reset() { mEarlyOutFraction = TraitsType::InitialEarlyOutFraction; }
63
67 virtual void OnBody([[maybe_unused]] const Body &inBody) { /* Collects nothing by default */ }
68
70 void SetContext(const TransformedShape *inContext) { mContext = inContext; }
71 const TransformedShape *GetContext() const { return mContext; }
72
74 virtual void AddHit(const ResultType &inResult) = 0;
75
77 inline void UpdateEarlyOutFraction(float inFraction) { JPH_ASSERT(inFraction <= mEarlyOutFraction); mEarlyOutFraction = inFraction; }
78
80 inline void ResetEarlyOutFraction(float inFraction = TraitsType::InitialEarlyOutFraction) { mEarlyOutFraction = inFraction; }
81
83 inline void ForceEarlyOut() { mEarlyOutFraction = TraitsType::ShouldEarlyOutFraction; }
84
86 inline bool ShouldEarlyOut() const { return mEarlyOutFraction <= TraitsType::ShouldEarlyOutFraction; }
87
89 inline float GetEarlyOutFraction() const { return mEarlyOutFraction; }
90
92 inline float GetPositiveEarlyOutFraction() const { return max(FLT_MIN, mEarlyOutFraction); }
93
94private:
96 float mEarlyOutFraction = TraitsType::InitialEarlyOutFraction;
97
99 const TransformedShape *mContext = nullptr;
100};
101
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
Definition: Body.h:35
Virtual interface that allows collecting multiple collision results.
Definition: CollisionCollector.h:45
void ForceEarlyOut()
Force the collision detection algorithm to terminate as soon as possible. Call this from the AddHit f...
Definition: CollisionCollector.h:83
void ResetEarlyOutFraction(float inFraction=TraitsType::InitialEarlyOutFraction)
Reset the early out fraction to a specific value.
Definition: CollisionCollector.h:80
CollisionCollector()=default
Default constructor.
virtual ~CollisionCollector()=default
Destructor.
bool ShouldEarlyOut() const
When true, the collector will no longer accept any additional hits and the collision detection routin...
Definition: CollisionCollector.h:86
virtual void AddHit(const ResultType &inResult)=0
This function will be called for every hit found, it's up to the application to decide how to store t...
virtual void Reset()
If you want to reuse this collector, call Reset()
Definition: CollisionCollector.h:62
CollisionCollector(const CollisionCollector< ResultTypeArg2, TraitsType > &inRHS)
Constructor to initialize from another collector.
Definition: CollisionCollector.h:55
CollisionCollector(const CollisionCollector< ResultTypeArg, TraitsType > &inRHS)=default
float GetEarlyOutFraction() const
Get the current early out value.
Definition: CollisionCollector.h:89
const TransformedShape * GetContext() const
Definition: CollisionCollector.h:71
virtual void OnBody(const Body &inBody)
Definition: CollisionCollector.h:67
void UpdateEarlyOutFraction(float inFraction)
Update the early out fraction (should be lower than before)
Definition: CollisionCollector.h:77
ResultTypeArg ResultType
Declare ResultType so that derived classes can use it.
Definition: CollisionCollector.h:48
void SetContext(const TransformedShape *inContext)
Set by the collision detection functions to the current TransformedShape that we're colliding against...
Definition: CollisionCollector.h:70
float GetPositiveEarlyOutFraction() const
Get the current early out value but make sure it's bigger than zero, this is used for shape casting a...
Definition: CollisionCollector.h:92
Traits to use for CastRay.
Definition: CollisionCollector.h:14
static constexpr float InitialEarlyOutFraction
For rays the early out fraction is the fraction along the line to order hits.
Definition: CollisionCollector.h:17
static constexpr float ShouldEarlyOutFraction
Closest hit: Fraction is 0.
Definition: CollisionCollector.h:18
Traits to use for CastShape.
Definition: CollisionCollector.h:23
static constexpr float InitialEarlyOutFraction
For rays the early out fraction is the fraction along the line to order hits.
Definition: CollisionCollector.h:26
static constexpr float ShouldEarlyOutFraction
Deepest hit: Penetration is infinite.
Definition: CollisionCollector.h:27
Traits to use for CollideShape.
Definition: CollisionCollector.h:32
static constexpr float InitialEarlyOutFraction
For shape collisions we use -penetration depth to order hits.
Definition: CollisionCollector.h:35
static constexpr float ShouldEarlyOutFraction
Deepest hit: Penetration is infinite.
Definition: CollisionCollector.h:36
Definition: TransformedShape.h:26