Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/spaces/jolt_query_collectors.h
10278 views
1
/**************************************************************************/
2
/* jolt_query_collectors.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "../jolt_project_settings.h"
34
#include "jolt_space_3d.h"
35
36
#include "Jolt/Jolt.h"
37
38
#include "Jolt/Core/STLLocalAllocator.h"
39
#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
40
#include "Jolt/Physics/Collision/Shape/Shape.h"
41
42
template <typename TBase, int TDefaultCapacity>
43
class JoltQueryCollectorAll final : public TBase {
44
public:
45
typedef typename TBase::ResultType Hit;
46
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
47
48
private:
49
HitArray hits;
50
51
public:
52
JoltQueryCollectorAll() {
53
hits.reserve(TDefaultCapacity);
54
}
55
56
bool had_hit() const {
57
return !hits.is_empty();
58
}
59
60
int get_hit_count() const {
61
return hits.size();
62
}
63
64
const Hit &get_hit(int p_index) const {
65
return hits[p_index];
66
}
67
68
void reset() { Reset(); }
69
70
virtual void Reset() override {
71
TBase::Reset();
72
hits.clear();
73
}
74
75
virtual void AddHit(const Hit &p_hit) override {
76
hits.push_back(p_hit);
77
}
78
};
79
80
template <typename TBase>
81
class JoltQueryCollectorAny final : public TBase {
82
public:
83
typedef typename TBase::ResultType Hit;
84
85
private:
86
Hit hit;
87
bool valid = false;
88
89
public:
90
bool had_hit() const { return valid; }
91
92
const Hit &get_hit() const { return hit; }
93
94
void reset() {
95
Reset();
96
}
97
98
virtual void Reset() override {
99
TBase::Reset();
100
valid = false;
101
}
102
103
virtual void AddHit(const Hit &p_hit) override {
104
hit = p_hit;
105
valid = true;
106
107
TBase::ForceEarlyOut();
108
}
109
};
110
111
template <typename TBase, int TDefaultCapacity>
112
class JoltQueryCollectorAnyMulti final : public TBase {
113
public:
114
typedef typename TBase::ResultType Hit;
115
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
116
117
private:
118
HitArray hits;
119
int max_hits = 0;
120
121
public:
122
explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :
123
max_hits(p_max_hits) {
124
hits.reserve(TDefaultCapacity);
125
}
126
127
bool had_hit() const {
128
return hits.size() > 0;
129
}
130
131
int get_hit_count() const {
132
return hits.size();
133
}
134
135
const Hit &get_hit(int p_index) const {
136
return hits[p_index];
137
}
138
139
void reset() {
140
Reset();
141
}
142
143
virtual void Reset() override {
144
TBase::Reset();
145
hits.clear();
146
}
147
148
virtual void AddHit(const Hit &p_hit) override {
149
if ((int)hits.size() < max_hits) {
150
hits.push_back(p_hit);
151
}
152
153
if ((int)hits.size() == max_hits) {
154
TBase::ForceEarlyOut();
155
}
156
}
157
};
158
159
template <typename TBase>
160
class JoltQueryCollectorClosest final : public TBase {
161
public:
162
typedef typename TBase::ResultType Hit;
163
164
private:
165
Hit hit;
166
bool valid = false;
167
168
public:
169
bool had_hit() const { return valid; }
170
171
const Hit &get_hit() const { return hit; }
172
173
void reset() {
174
Reset();
175
}
176
177
virtual void Reset() override {
178
TBase::Reset();
179
valid = false;
180
}
181
182
virtual void AddHit(const Hit &p_hit) override {
183
const float early_out = p_hit.GetEarlyOutFraction();
184
185
if (!valid || early_out < hit.GetEarlyOutFraction()) {
186
TBase::UpdateEarlyOutFraction(early_out);
187
188
hit = p_hit;
189
valid = true;
190
}
191
}
192
};
193
194
template <typename TBase, int TDefaultCapacity>
195
class JoltQueryCollectorClosestMulti final : public TBase {
196
public:
197
typedef typename TBase::ResultType Hit;
198
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;
199
200
private:
201
HitArray hits;
202
int max_hits = 0;
203
204
public:
205
explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :
206
max_hits(p_max_hits) {
207
hits.reserve(TDefaultCapacity + 1);
208
}
209
210
bool had_hit() const {
211
return hits.size() > 0;
212
}
213
214
int get_hit_count() const {
215
return hits.size();
216
}
217
218
const Hit &get_hit(int p_index) const {
219
return hits[p_index];
220
}
221
222
void reset() {
223
Reset();
224
}
225
226
virtual void Reset() override {
227
TBase::Reset();
228
hits.clear();
229
}
230
231
virtual void AddHit(const Hit &p_hit) override {
232
typename HitArray::const_iterator E = hits.cbegin();
233
for (; E != hits.cend(); ++E) {
234
if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {
235
break;
236
}
237
}
238
239
hits.insert(E, p_hit);
240
241
if ((int)hits.size() > max_hits) {
242
hits.resize(max_hits);
243
}
244
}
245
};
246
247