Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CollisionCollectorImpl.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
9
11
13template <class CollectorType>
14class AllHitCollisionCollector : public CollectorType
15{
16public:
18 using ResultType = typename CollectorType::ResultType;
19
20 // See: CollectorType::Reset
21 virtual void Reset() override
22 {
23 CollectorType::Reset();
24
25 mHits.clear();
26 }
27
28 // See: CollectorType::AddHit
29 virtual void AddHit(const ResultType &inResult) override
30 {
31 mHits.push_back(inResult);
32 }
33
35 void Sort()
36 {
37 QuickSort(mHits.begin(), mHits.end(), [](const ResultType &inLHS, const ResultType &inRHS) { return inLHS.GetEarlyOutFraction() < inRHS.GetEarlyOutFraction(); });
38 }
39
41 inline bool HadHit() const
42 {
43 return !mHits.empty();
44 }
45
47};
48
50template <class CollectorType>
51class ClosestHitCollisionCollector : public CollectorType
52{
53public:
55 using ResultType = typename CollectorType::ResultType;
56
57 // See: CollectorType::Reset
58 virtual void Reset() override
59 {
60 CollectorType::Reset();
61
62 mHadHit = false;
63 }
64
65 // See: CollectorType::AddHit
66 virtual void AddHit(const ResultType &inResult) override
67 {
68 float early_out = inResult.GetEarlyOutFraction();
69 if (!mHadHit || early_out < mHit.GetEarlyOutFraction())
70 {
71 // Update early out fraction
72 CollectorType::UpdateEarlyOutFraction(early_out);
73
74 // Store hit
75 mHit = inResult;
76 mHadHit = true;
77 }
78 }
79
81 inline bool HadHit() const
82 {
83 return mHadHit;
84 }
85
87
88private:
89 bool mHadHit = false;
90};
91
93template <class CollectorType>
94class AnyHitCollisionCollector : public CollectorType
95{
96public:
98 using ResultType = typename CollectorType::ResultType;
99
100 // See: CollectorType::Reset
101 virtual void Reset() override
102 {
103 CollectorType::Reset();
104
105 mHadHit = false;
106 }
107
108 // See: CollectorType::AddHit
109 virtual void AddHit(const ResultType &inResult) override
110 {
111 // Test that the collector is not collecting more hits after forcing an early out
112 JPH_ASSERT(!mHadHit);
113
114 // Abort any further testing
115 CollectorType::ForceEarlyOut();
116
117 // Store hit
118 mHit = inResult;
119 mHadHit = true;
120 }
121
123 inline bool HadHit() const
124 {
125 return mHadHit;
126 }
127
129
130private:
131 bool mHadHit = false;
132};
133
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
void QuickSort(Iterator inBegin, Iterator inEnd, Compare inCompare)
Implementation of the quick sort algorithm. The STL version implementation is not consistent across p...
Definition: QuickSort.h:53
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
Simple implementation that collects all hits and optionally sorts them on distance.
Definition: CollisionCollectorImpl.h:15
void Sort()
Order hits on closest first.
Definition: CollisionCollectorImpl.h:35
bool HadHit() const
Check if any hits were collected.
Definition: CollisionCollectorImpl.h:41
typename CollectorType::ResultType ResultType
Redeclare ResultType.
Definition: CollisionCollectorImpl.h:18
Array< ResultType > mHits
Definition: CollisionCollectorImpl.h:46
virtual void AddHit(const ResultType &inResult) override
Definition: CollisionCollectorImpl.h:29
virtual void Reset() override
Definition: CollisionCollectorImpl.h:21
Simple implementation that collects any hit.
Definition: CollisionCollectorImpl.h:95
virtual void Reset() override
Definition: CollisionCollectorImpl.h:101
virtual void AddHit(const ResultType &inResult) override
Definition: CollisionCollectorImpl.h:109
typename CollectorType::ResultType ResultType
Redeclare ResultType.
Definition: CollisionCollectorImpl.h:98
bool HadHit() const
Check if this collector has had a hit.
Definition: CollisionCollectorImpl.h:123
ResultType mHit
Definition: CollisionCollectorImpl.h:128
Simple implementation that collects the closest / deepest hit.
Definition: CollisionCollectorImpl.h:52
virtual void Reset() override
Definition: CollisionCollectorImpl.h:58
virtual void AddHit(const ResultType &inResult) override
Definition: CollisionCollectorImpl.h:66
ResultType mHit
Definition: CollisionCollectorImpl.h:86
bool HadHit() const
Check if this collector has had a hit.
Definition: CollisionCollectorImpl.h:81
typename CollectorType::ResultType ResultType
Redeclare ResultType.
Definition: CollisionCollectorImpl.h:55