-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTestCollision.h
376 lines (286 loc) · 8.9 KB
/
TestCollision.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#pragma once
#include "iw/physics/Collision/ManifoldPoints.h"
#include "PlaneCollider.h"
#include "SphereCollider.h"
#include "CapsuleCollider.h"
#include "HullCollider.h"
#include "MeshCollider.h"
#include "GJK.h"
#include "iw/math/iwmath.h"
namespace iw {
namespace Physics {
namespace impl {
using namespace glm;
using Test_Collision_func = ManifoldPoints(*)(
ColliderBase*, Transform*,
ColliderBase*, Transform*);
template<
Dimension _d>
struct Test_Collision_funcs
{
impl::Test_Collision_func tests[5][5] = {
{nullptr, impl::Test_Plane_Sphere <_d>, impl::Test_Plane_Capsule <_d>, impl::Test_Plane_Hull_Mesh<_d>, impl::Test_Plane_Hull_Mesh<_d> },
{nullptr, impl::Test_Sphere_Sphere<_d>, impl::Test_Sphere_Capsule <_d>, impl::Test_GJK <_d>, impl::Test_X_Mesh <_d> },
{nullptr, nullptr, impl::Test_Capsule_Capsule<_d>, impl::Test_GJK <_d>, impl::Test_X_Mesh <_d> },
{nullptr, nullptr, nullptr, impl::Test_GJK <_d>, impl::Test_X_Mesh <_d> },
{nullptr, nullptr, nullptr, nullptr, impl::Test_Mesh_Mesh <_d> },
};
};
template<
Dimension _d>
_vec<_d> rot_vec(
_vec<_d> v,
Transform* t)
{
if constexpr (_d == d3) {
v = v * t->WorldRotation();
}
else {
v = (_vec<_d>)(_vec<d3>(v, 0) * t->WorldRotation());
}
return v;
}
template<
Dimension _d>
ManifoldPoints Test_Plane_Sphere(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::PLANE
&& b->Type == ColliderType::SPHERE
&& a->Dim == _d
&& b->Dim == _d);
using Plane = PlaneCollider<_d>;
using Sphere = SphereCollider<_d>;
using vec_t = _vec<_d>;
Plane* A = (Plane*)a;
Sphere* B = (Sphere*)b;
vec_t aCenter = B->Center + (vec_t)bt->WorldPosition();
scalar aRadius = B->Radius * major(bt->WorldScale());
vec_t normal = rot_vec<_d>(normalize(A->Normal), at);
vec_t onPlane = normal * A->Distance + (vec_t)at->WorldPosition();
scalar distance = dot(aCenter - onPlane, normal); // distance from center of sphere to plane surface
if (distance > aRadius) {
return ManifoldPoints();
}
vec_t aDeep = aCenter - normal * aRadius;
vec_t bDeep = aCenter - normal * distance;
return ManifoldPoints(aDeep, bDeep, normal, distance);
}
template<
Dimension _d>
ManifoldPoints Test_Plane_Capsule(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::PLANE
&& b->Type == ColliderType::CAPSULE
&& a->Dim == _d
&& b->Dim == _d);
using Plane = PlaneCollider<_d>;
using Capsule = CapsuleCollider<_d>;
const Plane* A = (Plane*)a;
const Capsule* B = (Capsule*)b;
return ManifoldPoints();
}
template<
Dimension _d>
ManifoldPoints Test_Plane_Hull_Mesh(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::PLANE
&& (b->Type == ColliderType::HULL || b->Type == ColliderType::MESH)
&& a->Dim == _d
&& b->Dim == _d);
using Plane = PlaneCollider<_d>;
using Hull = HullCollider<_d>;
using vec_t = _vec<_d>;
Plane* A = (Plane*)a;
Hull* B = (Hull*)b;
vec_t normal = rot_vec<_d>(normalize(A->Normal), at);
vec_t plane = normal * A->Distance + (vec_t)at->WorldPosition();
vec_t bDeep = B->FindFurthestPoint(bt, -normal);
vec_t ba = plane - bDeep;
float distance = dot(ba, normal);
if (distance < 0) {
return ManifoldPoints();
}
// Might nudge 'plane' twoards bDeep (furthest point of plane in B)
return ManifoldPoints(plane, bDeep, -normal, distance);
}
template<
Dimension _d>
ManifoldPoints Test_Sphere_Sphere(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::SPHERE
&& b->Type == ColliderType::SPHERE
&& a->Dim == _d
&& b->Dim == _d);
using Sphere = SphereCollider<_d>;
using vec_t = _vec<_d>;
Sphere* A = (Sphere*)a;
Sphere* B = (Sphere*)b;
vec_t aCenter = A->Center + (vec_t)at->WorldPosition();
vec_t bCenter = B->Center + (vec_t)bt->WorldPosition();
vec_t ab = bCenter - aCenter;
scalar aRadius = A->Radius * major(at->WorldScale());
scalar bRadius = B->Radius * major(bt->WorldScale());
scalar distance = length(ab);
if ( distance < 0.00001f
|| distance > aRadius + bRadius)
{
return ManifoldPoints();
}
vec_t normal = normalize(ab);
vec_t aDeep = aCenter + normal * aRadius;
vec_t bDeep = bCenter - normal * bRadius;
return ManifoldPoints(aDeep, bDeep);
}
template<
Dimension _d>
ManifoldPoints Test_Sphere_Capsule(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::SPHERE
&& b->Type == ColliderType::CAPSULE
&& a->Dim == _d
&& b->Dim == _d);
using Sphere = SphereCollider<_d>;
using Capsule = CapsuleCollider<_d>;
using vec_t = _vec<_d>;
Sphere* A = (Sphere*)a;
Capsule* B = (Capsule*)b;
vec_t bScale = bt->WorldScale();
scalar bHeightScale = bScale.y;
scalar bRadiusScale = bScale.x;
if constexpr (_d == d3) {
bRadiusScale = iw::max(bRadiusScale, bScale.z);
}
vec_t y(0); y[1] = 1;
vec_t bOffset = rot_vec<_d>(y, bt) * (B->Height * bHeightScale / 2 - B->Radius * bRadiusScale);
vec_t aCenter = A->Center + (vec_t)at->WorldPosition();
vec_t bBottom = B->Center - bOffset + (vec_t)bt->WorldPosition();
vec_t bTop = B->Center + bOffset + (vec_t)bt->WorldPosition();
scalar aRadius = A->Radius * max(bHeightScale, bRadiusScale);
scalar bRadius = B->Radius * bRadiusScale;
vec_t ba = aCenter - bBottom;
vec_t bbt = bTop - bBottom;
vec_t nbbt = normalize(bbt);
vec_t bCenterProj = bBottom + nbbt * iw::clamp(dot(nbbt, ba), 0.0f, length(bbt));
vec_t bp = aCenter - bCenterProj;
scalar distance = length(bp);
if (distance > aRadius + bRadius) {
return ManifoldPoints();
}
vec_t normal = normalize(bp);
vec_t aDeep = aCenter - normal * aRadius;
vec_t bDeep = bCenterProj + normal * bRadius;
return ManifoldPoints(aDeep, bDeep);
}
template<
Dimension _d>
ManifoldPoints Test_Capsule_Capsule(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::CAPSULE
&& b->Type == ColliderType::CAPSULE
&& a->Dim == _d
&& b->Dim == _d);
using Capsule = CapsuleCollider<_d>;
const Capsule* A = (Capsule*)a;
const Capsule* B = (Capsule*)b;
return ManifoldPoints();
}
// For meshes
ManifoldPoints GetMaxPen(
std::vector<ManifoldPoints>& manifolds)
{
if (manifolds.size() == 0) return ManifoldPoints(); // exit if no collision
size_t maxNormalIndex = 0;
float maxNormalDist = FLT_MIN;
for (size_t i = 0; i < manifolds.size(); i++) {
if (manifolds[i].Depth > maxNormalDist) {
maxNormalDist = manifolds[i].Depth;
maxNormalIndex = i;
}
}
return manifolds[maxNormalIndex];
}
template<
Dimension _d>
ManifoldPoints Test_X_Mesh(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type != ColliderType::MESH
&& b->Type == ColliderType::MESH
&& a->Dim == _d
&& b->Dim == _d);
using Collider = Collider<_d>;
using Mesh = MeshCollider<_d>;
Collider* A = (Collider*)a;
Mesh* B = (Mesh*)b;
std::vector<ManifoldPoints> manifolds;
for (Mesh::hull_t& part : B->ConvexParts)
{
if (!part.Bounds().Intersects(bt, A->Bounds(), at)) continue;
auto [collision, simplex] = GJK(A, at , &part, bt);
if (collision) {
manifolds.push_back(EPA(simplex, A, at, &part, bt));
}
}
return GetMaxPen(manifolds);
}
template<
Dimension _d>
ManifoldPoints Test_Mesh_Mesh(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( a->Type == ColliderType::MESH
&& b->Type == ColliderType::MESH
&& a->Dim == _d
&& b->Dim == _d);
using Mesh = MeshCollider<_d>;
Mesh* A = (Mesh*)a;
Mesh* B = (Mesh*)b;
std::vector<ManifoldPoints> manifolds;
for (Mesh::hull_t& aPart : A->ConvexParts)
for (Mesh::hull_t& bPart : B->ConvexParts)
{
// could add each triangle to a broad phase...
if (!aPart.Bounds().Intersects(at, bPart.Bounds(), bt)) continue;
auto [collision, simplex] = GJK(&aPart, at, &bPart, bt);
if (collision) {
manifolds.push_back(EPA(simplex, &aPart, at, &bPart, bt));
}
}
return GetMaxPen(manifolds);
}
template<
Dimension _d>
ManifoldPoints Test_GJK(
ColliderBase* a, Transform* at,
ColliderBase* b, Transform* bt)
{
assert( (a->Type == ColliderType::HULL || b->Type == ColliderType::HULL)
&& (a->Type != ColliderType::MESH && b->Type != ColliderType::MESH)
&& a->Dim == _d
&& b->Dim == _d);
using Collider = Collider<_d>;
Collider* A = (Collider*)a;
Collider* B = (Collider*)b;
auto [collision, simplex] = GJK(A, at, B, bt);
if (collision) {
return EPA(simplex, A, at, B, bt);
}
return ManifoldPoints();
}
}
}
}