Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_3d/godot_collision_solver_3d_sat.cpp
10278 views
1
/**************************************************************************/
2
/* godot_collision_solver_3d_sat.cpp */
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
#include "godot_collision_solver_3d_sat.h"
32
33
#include "gjk_epa.h"
34
35
#include "core/math/geometry_3d.h"
36
37
#define fallback_collision_solver gjk_epa_calculate_penetration
38
39
#define _BACKFACE_NORMAL_THRESHOLD -0.0002
40
41
// Cylinder SAT analytic methods and face-circle contact points for cylinder-trimesh and cylinder-box collision are based on ODE colliders.
42
43
/*
44
* Cylinder-trimesh and Cylinder-box colliders by Alen Ladavac
45
* Ported to ODE by Nguyen Binh
46
*/
47
48
/*************************************************************************
49
* *
50
* Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
51
* All rights reserved. Email: [email protected] Web: www.q12.org *
52
* *
53
* This library is free software; you can redistribute it and/or *
54
* modify it under the terms of EITHER: *
55
* (1) The GNU Lesser General Public License as published by the Free *
56
* Software Foundation; either version 2.1 of the License, or (at *
57
* your option) any later version. The text of the GNU Lesser *
58
* General Public License is included with this library in the *
59
* file LICENSE.TXT. *
60
* (2) The BSD-style license that is included with this library in *
61
* the file LICENSE-BSD.TXT. *
62
* *
63
* This library is distributed in the hope that it will be useful, *
64
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
65
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
66
* LICENSE.TXT and LICENSE-BSD.TXT for more details. *
67
* *
68
*************************************************************************/
69
70
struct _CollectorCallback {
71
GodotCollisionSolver3D::CallbackResult callback = nullptr;
72
void *userdata = nullptr;
73
bool swap = false;
74
bool collided = false;
75
Vector3 normal;
76
Vector3 *prev_axis = nullptr;
77
78
_FORCE_INLINE_ void call(const Vector3 &p_point_A, const Vector3 &p_point_B, Vector3 p_normal) {
79
if (p_normal.dot(p_point_B - p_point_A) < 0) {
80
p_normal = -p_normal;
81
}
82
if (swap) {
83
callback(p_point_B, 0, p_point_A, 0, -p_normal, userdata);
84
} else {
85
callback(p_point_A, 0, p_point_B, 0, p_normal, userdata);
86
}
87
}
88
};
89
90
typedef void (*GenerateContactsFunc)(const Vector3 *, int, const Vector3 *, int, _CollectorCallback *);
91
92
static void _generate_contacts_point_point(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
93
#ifdef DEBUG_ENABLED
94
ERR_FAIL_COND(p_point_count_A != 1);
95
ERR_FAIL_COND(p_point_count_B != 1);
96
#endif
97
98
p_callback->call(*p_points_A, *p_points_B, p_callback->normal);
99
}
100
101
static void _generate_contacts_point_edge(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
102
#ifdef DEBUG_ENABLED
103
ERR_FAIL_COND(p_point_count_A != 1);
104
ERR_FAIL_COND(p_point_count_B != 2);
105
#endif
106
107
Vector3 closest_B = Geometry3D::get_closest_point_to_segment_uncapped(*p_points_A, p_points_B[0], p_points_B[1]);
108
p_callback->call(*p_points_A, closest_B, p_callback->normal);
109
}
110
111
static void _generate_contacts_point_face(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
112
#ifdef DEBUG_ENABLED
113
ERR_FAIL_COND(p_point_count_A != 1);
114
ERR_FAIL_COND(p_point_count_B < 3);
115
#endif
116
117
Plane plane(p_points_B[0], p_points_B[1], p_points_B[2]);
118
Vector3 closest_B = plane.project(*p_points_A);
119
p_callback->call(*p_points_A, closest_B, plane.get_normal());
120
}
121
122
static void _generate_contacts_point_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
123
#ifdef DEBUG_ENABLED
124
ERR_FAIL_COND(p_point_count_A != 1);
125
ERR_FAIL_COND(p_point_count_B != 3);
126
#endif
127
128
Plane plane(p_points_B[0], p_points_B[1], p_points_B[2]);
129
Vector3 closest_B = plane.project(*p_points_A);
130
p_callback->call(*p_points_A, closest_B, plane.get_normal());
131
}
132
133
static void _generate_contacts_edge_edge(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
134
#ifdef DEBUG_ENABLED
135
ERR_FAIL_COND(p_point_count_A != 2);
136
ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix
137
#endif
138
139
Vector3 rel_A = p_points_A[1] - p_points_A[0];
140
Vector3 rel_B = p_points_B[1] - p_points_B[0];
141
142
Vector3 c = rel_A.cross(rel_B).cross(rel_B);
143
144
if (Math::is_zero_approx(rel_A.dot(c))) {
145
// should handle somehow..
146
//ERR_PRINT("TODO FIX");
147
//return;
148
149
Vector3 axis = rel_A.normalized(); //make an axis
150
Vector3 base_A = p_points_A[0] - axis * axis.dot(p_points_A[0]);
151
Vector3 base_B = p_points_B[0] - axis * axis.dot(p_points_B[0]);
152
153
//sort all 4 points in axis
154
real_t dvec[4] = { axis.dot(p_points_A[0]), axis.dot(p_points_A[1]), axis.dot(p_points_B[0]), axis.dot(p_points_B[1]) };
155
156
SortArray<real_t> sa;
157
sa.sort(dvec, 4);
158
159
//use the middle ones as contacts
160
p_callback->call(base_A + axis * dvec[1], base_B + axis * dvec[1], p_callback->normal);
161
p_callback->call(base_A + axis * dvec[2], base_B + axis * dvec[2], p_callback->normal);
162
163
return;
164
}
165
166
real_t d = (c.dot(p_points_B[0]) - p_points_A[0].dot(c)) / rel_A.dot(c);
167
168
if (d < 0.0) {
169
d = 0.0;
170
} else if (d > 1.0) {
171
d = 1.0;
172
}
173
174
const Vector3 closest_A = p_points_A[0] + rel_A * d;
175
const Vector3 closest_B = Geometry3D::get_closest_point_to_segment_uncapped(closest_A, p_points_B[0], p_points_B[1]);
176
// The normal should be perpendicular to both edges.
177
Vector3 normal = rel_A.cross(rel_B);
178
real_t normal_len = normal.length();
179
if (normal_len > 1e-3) {
180
normal /= normal_len;
181
} else {
182
normal = p_callback->normal;
183
}
184
p_callback->call(closest_A, closest_B, normal);
185
}
186
187
static void _generate_contacts_edge_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
188
#ifdef DEBUG_ENABLED
189
ERR_FAIL_COND(p_point_count_A != 2);
190
ERR_FAIL_COND(p_point_count_B != 3);
191
#endif
192
193
const Vector3 &circle_B_pos = p_points_B[0];
194
Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
195
Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
196
197
real_t circle_B_radius = circle_B_line_1.length();
198
Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
199
200
Plane circle_plane(circle_B_normal, circle_B_pos);
201
202
static const int max_clip = 2;
203
Vector3 contact_points[max_clip];
204
int num_points = 0;
205
206
// Project edge point in circle plane.
207
const Vector3 &edge_A_1 = p_points_A[0];
208
Vector3 proj_point_1 = circle_plane.project(edge_A_1);
209
210
Vector3 dist_vec = proj_point_1 - circle_B_pos;
211
real_t dist_sq = dist_vec.length_squared();
212
213
// Point 1 is inside disk, add as contact point.
214
if (dist_sq <= circle_B_radius * circle_B_radius) {
215
contact_points[num_points] = edge_A_1;
216
++num_points;
217
}
218
219
const Vector3 &edge_A_2 = p_points_A[1];
220
Vector3 proj_point_2 = circle_plane.project(edge_A_2);
221
222
Vector3 dist_vec_2 = proj_point_2 - circle_B_pos;
223
real_t dist_sq_2 = dist_vec_2.length_squared();
224
225
// Point 2 is inside disk, add as contact point.
226
if (dist_sq_2 <= circle_B_radius * circle_B_radius) {
227
contact_points[num_points] = edge_A_2;
228
++num_points;
229
}
230
231
if (num_points < 2) {
232
Vector3 line_vec = proj_point_2 - proj_point_1;
233
real_t line_length_sq = line_vec.length_squared();
234
235
// Create a quadratic formula of the form ax^2 + bx + c = 0
236
real_t a, b, c;
237
238
a = line_length_sq;
239
b = 2.0 * dist_vec.dot(line_vec);
240
c = dist_sq - circle_B_radius * circle_B_radius;
241
242
// Solve for t.
243
real_t sqrtterm = b * b - 4.0 * a * c;
244
245
// If the term we intend to square root is less than 0 then the answer won't be real,
246
// so the line doesn't intersect.
247
if (sqrtterm >= 0) {
248
sqrtterm = Math::sqrt(sqrtterm);
249
250
Vector3 edge_dir = edge_A_2 - edge_A_1;
251
252
real_t fraction_1 = (-b - sqrtterm) / (2.0 * a);
253
if ((fraction_1 > 0.0) && (fraction_1 < 1.0)) {
254
Vector3 face_point_1 = edge_A_1 + fraction_1 * edge_dir;
255
ERR_FAIL_COND(num_points >= max_clip);
256
contact_points[num_points] = face_point_1;
257
++num_points;
258
}
259
260
real_t fraction_2 = (-b + sqrtterm) / (2.0 * a);
261
if ((fraction_2 > 0.0) && (fraction_2 < 1.0) && !Math::is_equal_approx(fraction_1, fraction_2)) {
262
Vector3 face_point_2 = edge_A_1 + fraction_2 * edge_dir;
263
ERR_FAIL_COND(num_points >= max_clip);
264
contact_points[num_points] = face_point_2;
265
++num_points;
266
}
267
}
268
}
269
270
// Generate contact points.
271
for (int i = 0; i < num_points; i++) {
272
const Vector3 &contact_point_A = contact_points[i];
273
274
real_t d = circle_plane.distance_to(contact_point_A);
275
Vector3 closest_B = contact_point_A - circle_plane.normal * d;
276
277
if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
278
continue;
279
}
280
281
p_callback->call(contact_point_A, closest_B, circle_plane.get_normal());
282
}
283
}
284
285
static void _generate_contacts_face_face(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
286
#ifdef DEBUG_ENABLED
287
ERR_FAIL_COND(p_point_count_A < 2);
288
ERR_FAIL_COND(p_point_count_B < 3);
289
#endif
290
291
static const int max_clip = 32;
292
293
Vector3 _clipbuf1[max_clip];
294
Vector3 _clipbuf2[max_clip];
295
Vector3 *clipbuf_src = _clipbuf1;
296
Vector3 *clipbuf_dst = _clipbuf2;
297
int clipbuf_len = p_point_count_A;
298
299
// copy A points to clipbuf_src
300
for (int i = 0; i < p_point_count_A; i++) {
301
clipbuf_src[i] = p_points_A[i];
302
}
303
304
Plane plane_B(p_points_B[0], p_points_B[1], p_points_B[2]);
305
306
// go through all of B points
307
for (int i = 0; i < p_point_count_B; i++) {
308
int i_n = (i + 1) % p_point_count_B;
309
310
Vector3 edge0_B = p_points_B[i];
311
Vector3 edge1_B = p_points_B[i_n];
312
313
Vector3 clip_normal = (edge0_B - edge1_B).cross(plane_B.normal).normalized();
314
// make a clip plane
315
316
Plane clip(clip_normal, edge0_B);
317
// avoid double clip if A is edge
318
int dst_idx = 0;
319
bool edge = clipbuf_len == 2;
320
for (int j = 0; j < clipbuf_len; j++) {
321
int j_n = (j + 1) % clipbuf_len;
322
323
Vector3 edge0_A = clipbuf_src[j];
324
Vector3 edge1_A = clipbuf_src[j_n];
325
326
real_t dist0 = clip.distance_to(edge0_A);
327
real_t dist1 = clip.distance_to(edge1_A);
328
329
if (dist0 <= 0) { // behind plane
330
331
ERR_FAIL_COND(dst_idx >= max_clip);
332
clipbuf_dst[dst_idx++] = clipbuf_src[j];
333
}
334
335
// check for different sides and non coplanar
336
//if ( (dist0*dist1) < -CMP_EPSILON && !(edge && j)) {
337
if ((dist0 * dist1) < 0 && !(edge && j)) {
338
// calculate intersection
339
Vector3 rel = edge1_A - edge0_A;
340
real_t den = clip.normal.dot(rel);
341
real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
342
Vector3 inters = edge0_A + rel * dist;
343
344
ERR_FAIL_COND(dst_idx >= max_clip);
345
clipbuf_dst[dst_idx] = inters;
346
dst_idx++;
347
}
348
}
349
350
clipbuf_len = dst_idx;
351
SWAP(clipbuf_src, clipbuf_dst);
352
}
353
354
// generate contacts
355
//Plane plane_A(p_points_A[0],p_points_A[1],p_points_A[2]);
356
357
for (int i = 0; i < clipbuf_len; i++) {
358
real_t d = plane_B.distance_to(clipbuf_src[i]);
359
360
Vector3 closest_B = clipbuf_src[i] - plane_B.normal * d;
361
362
if (p_callback->normal.dot(clipbuf_src[i]) >= p_callback->normal.dot(closest_B)) {
363
continue;
364
}
365
366
p_callback->call(clipbuf_src[i], closest_B, plane_B.get_normal());
367
}
368
}
369
370
static void _generate_contacts_face_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
371
#ifdef DEBUG_ENABLED
372
ERR_FAIL_COND(p_point_count_A < 3);
373
ERR_FAIL_COND(p_point_count_B != 3);
374
#endif
375
376
const Vector3 &circle_B_pos = p_points_B[0];
377
Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
378
Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
379
380
// Clip face with circle segments.
381
static const int circle_segments = 8;
382
Vector3 circle_points[circle_segments];
383
384
real_t angle_delta = 2.0 * Math::PI / circle_segments;
385
386
for (int i = 0; i < circle_segments; ++i) {
387
Vector3 point_pos = circle_B_pos;
388
point_pos += circle_B_line_1 * Math::cos(i * angle_delta);
389
point_pos += circle_B_line_2 * Math::sin(i * angle_delta);
390
circle_points[i] = point_pos;
391
}
392
393
_generate_contacts_face_face(p_points_A, p_point_count_A, circle_points, circle_segments, p_callback);
394
395
// Clip face with circle plane.
396
Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
397
398
Plane circle_plane(circle_B_normal, circle_B_pos);
399
400
static const int max_clip = 32;
401
Vector3 contact_points[max_clip];
402
int num_points = 0;
403
404
for (int i = 0; i < p_point_count_A; i++) {
405
int i_n = (i + 1) % p_point_count_A;
406
407
const Vector3 &edge0_A = p_points_A[i];
408
const Vector3 &edge1_A = p_points_A[i_n];
409
410
real_t dist0 = circle_plane.distance_to(edge0_A);
411
real_t dist1 = circle_plane.distance_to(edge1_A);
412
413
// First point in front of plane, generate contact point.
414
if (dist0 * circle_plane.d >= 0) {
415
ERR_FAIL_COND(num_points >= max_clip);
416
contact_points[num_points] = edge0_A;
417
++num_points;
418
}
419
420
// Points on different sides, generate contact point.
421
if (dist0 * dist1 < 0) {
422
// calculate intersection
423
Vector3 rel = edge1_A - edge0_A;
424
real_t den = circle_plane.normal.dot(rel);
425
real_t dist = -(circle_plane.normal.dot(edge0_A) - circle_plane.d) / den;
426
Vector3 inters = edge0_A + rel * dist;
427
428
ERR_FAIL_COND(num_points >= max_clip);
429
contact_points[num_points] = inters;
430
++num_points;
431
}
432
}
433
434
// Generate contact points.
435
for (int i = 0; i < num_points; i++) {
436
const Vector3 &contact_point_A = contact_points[i];
437
438
real_t d = circle_plane.distance_to(contact_point_A);
439
Vector3 closest_B = contact_point_A - circle_plane.normal * d;
440
441
if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
442
continue;
443
}
444
445
p_callback->call(contact_point_A, closest_B, circle_plane.get_normal());
446
}
447
}
448
449
static void _generate_contacts_circle_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
450
#ifdef DEBUG_ENABLED
451
ERR_FAIL_COND(p_point_count_A != 3);
452
ERR_FAIL_COND(p_point_count_B != 3);
453
#endif
454
455
const Vector3 &circle_A_pos = p_points_A[0];
456
Vector3 circle_A_line_1 = p_points_A[1] - circle_A_pos;
457
Vector3 circle_A_line_2 = p_points_A[2] - circle_A_pos;
458
459
real_t circle_A_radius = circle_A_line_1.length();
460
Vector3 circle_A_normal = circle_A_line_1.cross(circle_A_line_2).normalized();
461
462
const Vector3 &circle_B_pos = p_points_B[0];
463
Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
464
Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
465
466
real_t circle_B_radius = circle_B_line_1.length();
467
Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
468
469
static const int max_clip = 4;
470
Vector3 contact_points[max_clip];
471
int num_points = 0;
472
473
Vector3 centers_diff = circle_B_pos - circle_A_pos;
474
Vector3 norm_proj = circle_A_normal.dot(centers_diff) * circle_A_normal;
475
Vector3 comp_proj = centers_diff - norm_proj;
476
real_t proj_dist = comp_proj.length();
477
if (!Math::is_zero_approx(proj_dist)) {
478
comp_proj /= proj_dist;
479
if ((proj_dist > circle_A_radius - circle_B_radius) && (proj_dist > circle_B_radius - circle_A_radius)) {
480
// Circles are overlapping, use the 2 points of intersection as contacts.
481
real_t radius_a_sqr = circle_A_radius * circle_A_radius;
482
real_t radius_b_sqr = circle_B_radius * circle_B_radius;
483
real_t d_sqr = proj_dist * proj_dist;
484
real_t s = (1.0 + (radius_a_sqr - radius_b_sqr) / d_sqr) * 0.5;
485
real_t h = Math::sqrt(MAX(radius_a_sqr - d_sqr * s * s, 0.0));
486
Vector3 midpoint = circle_A_pos + s * comp_proj * proj_dist;
487
Vector3 h_vec = h * circle_A_normal.cross(comp_proj);
488
489
Vector3 point_A = midpoint + h_vec;
490
contact_points[num_points] = point_A;
491
++num_points;
492
493
point_A = midpoint - h_vec;
494
contact_points[num_points] = point_A;
495
++num_points;
496
497
// Add 2 points from circle A and B along the line between the centers.
498
point_A = circle_A_pos + comp_proj * circle_A_radius;
499
contact_points[num_points] = point_A;
500
++num_points;
501
502
point_A = circle_B_pos - comp_proj * circle_B_radius - norm_proj;
503
contact_points[num_points] = point_A;
504
++num_points;
505
} // Otherwise one circle is inside the other one, use 3 arbitrary equidistant points.
506
} // Otherwise circles are concentric, use 3 arbitrary equidistant points.
507
508
if (num_points == 0) {
509
// Generate equidistant points.
510
if (circle_A_radius < circle_B_radius) {
511
// Circle A inside circle B.
512
for (int i = 0; i < 3; ++i) {
513
Vector3 circle_A_point = circle_A_pos;
514
circle_A_point += circle_A_line_1 * Math::cos(2.0 * Math::PI * i / 3.0);
515
circle_A_point += circle_A_line_2 * Math::sin(2.0 * Math::PI * i / 3.0);
516
517
contact_points[num_points] = circle_A_point;
518
++num_points;
519
}
520
} else {
521
// Circle B inside circle A.
522
for (int i = 0; i < 3; ++i) {
523
Vector3 circle_B_point = circle_B_pos;
524
circle_B_point += circle_B_line_1 * Math::cos(2.0 * Math::PI * i / 3.0);
525
circle_B_point += circle_B_line_2 * Math::sin(2.0 * Math::PI * i / 3.0);
526
527
Vector3 circle_A_point = circle_B_point - norm_proj;
528
529
contact_points[num_points] = circle_A_point;
530
++num_points;
531
}
532
}
533
}
534
535
Plane circle_B_plane(circle_B_normal, circle_B_pos);
536
537
// Generate contact points.
538
for (int i = 0; i < num_points; i++) {
539
const Vector3 &contact_point_A = contact_points[i];
540
541
real_t d = circle_B_plane.distance_to(contact_point_A);
542
Vector3 closest_B = contact_point_A - circle_B_plane.normal * d;
543
544
if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
545
continue;
546
}
547
548
p_callback->call(contact_point_A, closest_B, circle_B_plane.get_normal());
549
}
550
}
551
552
static void _generate_contacts_from_supports(const Vector3 *p_points_A, int p_point_count_A, GodotShape3D::FeatureType p_feature_type_A, const Vector3 *p_points_B, int p_point_count_B, GodotShape3D::FeatureType p_feature_type_B, _CollectorCallback *p_callback) {
553
#ifdef DEBUG_ENABLED
554
ERR_FAIL_COND(p_point_count_A < 1);
555
ERR_FAIL_COND(p_point_count_B < 1);
556
#endif
557
558
static const GenerateContactsFunc generate_contacts_func_table[4][4] = {
559
{
560
_generate_contacts_point_point,
561
_generate_contacts_point_edge,
562
_generate_contacts_point_face,
563
_generate_contacts_point_circle,
564
},
565
{
566
nullptr,
567
_generate_contacts_edge_edge,
568
_generate_contacts_face_face,
569
_generate_contacts_edge_circle,
570
},
571
{
572
nullptr,
573
nullptr,
574
_generate_contacts_face_face,
575
_generate_contacts_face_circle,
576
},
577
{
578
nullptr,
579
nullptr,
580
nullptr,
581
_generate_contacts_circle_circle,
582
},
583
};
584
585
int pointcount_B;
586
int pointcount_A;
587
const Vector3 *points_A;
588
const Vector3 *points_B;
589
int version_A;
590
int version_B;
591
592
if (p_feature_type_A > p_feature_type_B) {
593
//swap
594
p_callback->swap = !p_callback->swap;
595
p_callback->normal = -p_callback->normal;
596
597
pointcount_B = p_point_count_A;
598
pointcount_A = p_point_count_B;
599
points_A = p_points_B;
600
points_B = p_points_A;
601
version_A = p_feature_type_B;
602
version_B = p_feature_type_A;
603
} else {
604
pointcount_B = p_point_count_B;
605
pointcount_A = p_point_count_A;
606
points_A = p_points_A;
607
points_B = p_points_B;
608
version_A = p_feature_type_A;
609
version_B = p_feature_type_B;
610
}
611
612
GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B];
613
ERR_FAIL_NULL(contacts_func);
614
contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_callback);
615
}
616
617
template <typename ShapeA, typename ShapeB, bool withMargin = false>
618
class SeparatorAxisTest {
619
const ShapeA *shape_A = nullptr;
620
const ShapeB *shape_B = nullptr;
621
const Transform3D *transform_A = nullptr;
622
const Transform3D *transform_B = nullptr;
623
real_t best_depth = 1e15;
624
_CollectorCallback *callback = nullptr;
625
real_t margin_A = 0.0;
626
real_t margin_B = 0.0;
627
Vector3 separator_axis;
628
629
public:
630
Vector3 best_axis;
631
632
_FORCE_INLINE_ bool test_previous_axis() {
633
if (callback && callback->prev_axis && *callback->prev_axis != Vector3()) {
634
return test_axis(*callback->prev_axis);
635
} else {
636
return true;
637
}
638
}
639
640
_FORCE_INLINE_ bool test_axis(const Vector3 &p_axis) {
641
Vector3 axis = p_axis;
642
643
if (axis.is_zero_approx()) {
644
// strange case, try an upwards separator
645
axis = Vector3(0.0, 1.0, 0.0);
646
}
647
648
real_t min_A = 0.0, max_A = 0.0, min_B = 0.0, max_B = 0.0;
649
650
shape_A->project_range(axis, *transform_A, min_A, max_A);
651
shape_B->project_range(axis, *transform_B, min_B, max_B);
652
653
if (withMargin) {
654
min_A -= margin_A;
655
max_A += margin_A;
656
min_B -= margin_B;
657
max_B += margin_B;
658
}
659
660
min_B -= (max_A - min_A) * 0.5;
661
max_B += (max_A - min_A) * 0.5;
662
663
min_B -= (min_A + max_A) * 0.5;
664
max_B -= (min_A + max_A) * 0.5;
665
666
if (min_B > 0.0 || max_B < 0.0) {
667
separator_axis = axis;
668
return false; // doesn't contain 0
669
}
670
671
//use the smallest depth
672
673
if (min_B < 0.0) { // could be +0.0, we don't want it to become -0.0
674
min_B = -min_B;
675
}
676
677
if (max_B < min_B) {
678
if (max_B < best_depth) {
679
best_depth = max_B;
680
best_axis = axis;
681
}
682
} else {
683
if (min_B < best_depth) {
684
best_depth = min_B;
685
best_axis = -axis; // keep it as A axis
686
}
687
}
688
689
return true;
690
}
691
692
static _FORCE_INLINE_ void test_contact_points(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
693
SeparatorAxisTest<ShapeA, ShapeB, withMargin> *separator = (SeparatorAxisTest<ShapeA, ShapeB, withMargin> *)p_userdata;
694
Vector3 axis = (p_point_B - p_point_A);
695
real_t depth = axis.length();
696
697
// Filter out bogus directions with a threshold and re-testing axis.
698
if (separator->best_depth - depth > 0.001) {
699
separator->test_axis(axis / depth);
700
}
701
}
702
703
_FORCE_INLINE_ void generate_contacts() {
704
// nothing to do, don't generate
705
if (best_axis == Vector3(0.0, 0.0, 0.0)) {
706
return;
707
}
708
709
if (!callback->callback) {
710
//just was checking intersection?
711
callback->collided = true;
712
if (callback->prev_axis) {
713
*callback->prev_axis = best_axis;
714
}
715
return;
716
}
717
718
static const int max_supports = 16;
719
720
Vector3 supports_A[max_supports];
721
int support_count_A;
722
GodotShape3D::FeatureType support_type_A;
723
shape_A->get_supports(transform_A->basis.xform_inv(-best_axis).normalized(), max_supports, supports_A, support_count_A, support_type_A);
724
for (int i = 0; i < support_count_A; i++) {
725
supports_A[i] = transform_A->xform(supports_A[i]);
726
}
727
728
if (withMargin) {
729
for (int i = 0; i < support_count_A; i++) {
730
supports_A[i] += -best_axis * margin_A;
731
}
732
}
733
734
Vector3 supports_B[max_supports];
735
int support_count_B;
736
GodotShape3D::FeatureType support_type_B;
737
shape_B->get_supports(transform_B->basis.xform_inv(best_axis).normalized(), max_supports, supports_B, support_count_B, support_type_B);
738
for (int i = 0; i < support_count_B; i++) {
739
supports_B[i] = transform_B->xform(supports_B[i]);
740
}
741
742
if (withMargin) {
743
for (int i = 0; i < support_count_B; i++) {
744
supports_B[i] += best_axis * margin_B;
745
}
746
}
747
748
callback->normal = best_axis;
749
if (callback->prev_axis) {
750
*callback->prev_axis = best_axis;
751
}
752
_generate_contacts_from_supports(supports_A, support_count_A, support_type_A, supports_B, support_count_B, support_type_B, callback);
753
754
callback->collided = true;
755
}
756
757
_FORCE_INLINE_ SeparatorAxisTest(const ShapeA *p_shape_A, const Transform3D &p_transform_A, const ShapeB *p_shape_B, const Transform3D &p_transform_B, _CollectorCallback *p_callback, real_t p_margin_A = 0, real_t p_margin_B = 0) {
758
shape_A = p_shape_A;
759
shape_B = p_shape_B;
760
transform_A = &p_transform_A;
761
transform_B = &p_transform_B;
762
callback = p_callback;
763
margin_A = p_margin_A;
764
margin_B = p_margin_B;
765
}
766
};
767
768
/****** SAT TESTS *******/
769
770
typedef void (*CollisionFunc)(const GodotShape3D *, const Transform3D &, const GodotShape3D *, const Transform3D &, _CollectorCallback *p_callback, real_t, real_t);
771
772
// Perform analytic sphere-sphere collision and report results to collector
773
template <bool withMargin>
774
static void analytic_sphere_collision(const Vector3 &p_origin_a, real_t p_radius_a, const Vector3 &p_origin_b, real_t p_radius_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
775
// Expand the spheres by the margins if enabled
776
if (withMargin) {
777
p_radius_a += p_margin_a;
778
p_radius_b += p_margin_b;
779
}
780
781
// Get the vector from sphere B to A
782
Vector3 b_to_a = p_origin_a - p_origin_b;
783
784
// Get the length from B to A
785
real_t b_to_a_len = b_to_a.length();
786
787
// Calculate the sphere overlap, and bail if not overlapping
788
real_t overlap = p_radius_a + p_radius_b - b_to_a_len;
789
if (overlap < 0) {
790
return;
791
}
792
793
// Report collision
794
p_collector->collided = true;
795
796
// Bail if there is no callback to receive the A and B collision points.
797
if (!p_collector->callback) {
798
return;
799
}
800
801
// Normalize the B to A vector
802
if (b_to_a_len < CMP_EPSILON) {
803
b_to_a = Vector3(0, 1, 0); // Spheres coincident, use arbitrary direction
804
} else {
805
b_to_a /= b_to_a_len;
806
}
807
808
// Report collision points. The operations below are intended to minimize
809
// floating-point precision errors. This is done by calculating the first
810
// collision point from the smaller sphere, and then jumping across to
811
// the larger spheres collision point using the overlap distance. This
812
// jump is usually small even if the large sphere is massive, and so the
813
// second point will not suffer from precision errors.
814
if (p_radius_a < p_radius_b) {
815
Vector3 point_a = p_origin_a - b_to_a * p_radius_a;
816
Vector3 point_b = point_a + b_to_a * overlap;
817
p_collector->call(point_a, point_b, b_to_a); // Consider adding b_to_a vector
818
} else {
819
Vector3 point_b = p_origin_b + b_to_a * p_radius_b;
820
Vector3 point_a = point_b - b_to_a * overlap;
821
p_collector->call(point_a, point_b, b_to_a); // Consider adding b_to_a vector
822
}
823
}
824
825
template <bool withMargin>
826
static void _collision_sphere_sphere(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
827
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
828
const GodotSphereShape3D *sphere_B = static_cast<const GodotSphereShape3D *>(p_b);
829
830
// Perform an analytic sphere collision between the two spheres
831
analytic_sphere_collision<withMargin>(
832
p_transform_a.origin,
833
sphere_A->get_radius() * p_transform_a.basis[0].length(),
834
p_transform_b.origin,
835
sphere_B->get_radius() * p_transform_b.basis[0].length(),
836
p_collector,
837
p_margin_a,
838
p_margin_b);
839
}
840
841
template <bool withMargin>
842
static void _collision_sphere_box(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
843
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
844
const GodotBoxShape3D *box_B = static_cast<const GodotBoxShape3D *>(p_b);
845
846
// Find the point on the box nearest to the center of the sphere.
847
848
Vector3 center = p_transform_b.affine_inverse().xform(p_transform_a.origin);
849
Vector3 extents = box_B->get_half_extents();
850
Vector3 nearest(MIN(MAX(center.x, -extents.x), extents.x),
851
MIN(MAX(center.y, -extents.y), extents.y),
852
MIN(MAX(center.z, -extents.z), extents.z));
853
nearest = p_transform_b.xform(nearest);
854
855
// See if it is inside the sphere.
856
857
Vector3 delta = nearest - p_transform_a.origin;
858
real_t length = delta.length();
859
real_t radius = sphere_A->get_radius() * p_transform_a.basis[0].length();
860
if (length > radius + p_margin_a + p_margin_b) {
861
return;
862
}
863
p_collector->collided = true;
864
if (!p_collector->callback) {
865
return;
866
}
867
Vector3 axis;
868
if (length == 0) {
869
// The box passes through the sphere center. Select an axis based on the box's center.
870
axis = (p_transform_b.origin - nearest).normalized();
871
} else {
872
axis = delta / length;
873
}
874
Vector3 point_a = p_transform_a.origin + (radius + p_margin_a) * axis;
875
Vector3 point_b = (withMargin ? nearest - p_margin_b * axis : nearest);
876
p_collector->call(point_a, point_b, axis);
877
}
878
879
template <bool withMargin>
880
static void _collision_sphere_capsule(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
881
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
882
const GodotCapsuleShape3D *capsule_B = static_cast<const GodotCapsuleShape3D *>(p_b);
883
884
real_t scale_A = p_transform_a.basis[0].length();
885
real_t scale_B = p_transform_b.basis[0].length();
886
887
// Construct the capsule segment (ball-center to ball-center)
888
Vector3 capsule_axis = p_transform_b.basis.get_column(1) * (capsule_B->get_height() * 0.5 - capsule_B->get_radius());
889
const Vector3 capsule_segment_a = p_transform_b.origin + capsule_axis;
890
const Vector3 capsule_segment_b = p_transform_b.origin - capsule_axis;
891
892
// Get the capsules closest segment-point to the sphere
893
Vector3 capsule_closest = Geometry3D::get_closest_point_to_segment(p_transform_a.origin, capsule_segment_a, capsule_segment_b);
894
895
// Perform an analytic sphere collision between the sphere and the sphere-collider in the capsule
896
analytic_sphere_collision<withMargin>(
897
p_transform_a.origin,
898
sphere_A->get_radius() * scale_A,
899
capsule_closest,
900
capsule_B->get_radius() * scale_B,
901
p_collector,
902
p_margin_a,
903
p_margin_b);
904
}
905
906
template <bool withMargin>
907
static void analytic_sphere_cylinder_collision(real_t p_radius_a, real_t p_radius_b, real_t p_height_b, const Transform3D &p_transform_a, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
908
// Find the point on the cylinder nearest to the center of the sphere.
909
910
Vector3 center = p_transform_b.affine_inverse().xform(p_transform_a.origin);
911
Vector3 nearest = center;
912
real_t scale_A = p_transform_a.basis[0].length();
913
real_t r = Math::sqrt(center.x * center.x + center.z * center.z);
914
if (r > p_radius_b) {
915
real_t scale = p_radius_b / r;
916
nearest.x *= scale;
917
nearest.z *= scale;
918
}
919
real_t half_height = p_height_b / 2;
920
nearest.y = MIN(MAX(center.y, -half_height), half_height);
921
nearest = p_transform_b.xform(nearest);
922
923
// See if it is inside the sphere.
924
925
Vector3 delta = nearest - p_transform_a.origin;
926
real_t length = delta.length();
927
if (length > p_radius_a * scale_A + p_margin_a + p_margin_b) {
928
return;
929
}
930
p_collector->collided = true;
931
if (!p_collector->callback) {
932
return;
933
}
934
Vector3 axis;
935
if (length == 0) {
936
// The cylinder passes through the sphere center. Select an axis based on the cylinder's center.
937
axis = (p_transform_b.origin - nearest).normalized();
938
} else {
939
axis = delta / length;
940
}
941
Vector3 point_a = p_transform_a.origin + (p_radius_a * scale_A + p_margin_a) * axis;
942
Vector3 point_b = (withMargin ? nearest - p_margin_b * axis : nearest);
943
p_collector->call(point_a, point_b, axis);
944
}
945
946
template <bool withMargin>
947
static void _collision_sphere_cylinder(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
948
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
949
const GodotCylinderShape3D *cylinder_B = static_cast<const GodotCylinderShape3D *>(p_b);
950
951
analytic_sphere_cylinder_collision<withMargin>(sphere_A->get_radius(), cylinder_B->get_radius(), cylinder_B->get_height(), p_transform_a, p_transform_b, p_collector, p_margin_a, p_margin_b);
952
}
953
954
template <bool withMargin>
955
static void _collision_sphere_convex_polygon(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
956
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
957
const GodotConvexPolygonShape3D *convex_polygon_B = static_cast<const GodotConvexPolygonShape3D *>(p_b);
958
959
SeparatorAxisTest<GodotSphereShape3D, GodotConvexPolygonShape3D, withMargin> separator(sphere_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
960
961
if (!separator.test_previous_axis()) {
962
return;
963
}
964
965
const Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
966
967
const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
968
int face_count = mesh.faces.size();
969
const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
970
int edge_count = mesh.edges.size();
971
const Vector3 *vertices = mesh.vertices.ptr();
972
int vertex_count = mesh.vertices.size();
973
974
// Precalculating this makes the transforms faster.
975
Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
976
977
// faces of B
978
for (int i = 0; i < face_count; i++) {
979
Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
980
981
if (!separator.test_axis(axis)) {
982
return;
983
}
984
}
985
986
// edges of B
987
for (int i = 0; i < edge_count; i++) {
988
Vector3 v1 = p_transform_b.xform(vertices[edges[i].vertex_a]);
989
Vector3 v2 = p_transform_b.xform(vertices[edges[i].vertex_b]);
990
Vector3 v3 = p_transform_a.origin;
991
992
Vector3 n1 = v2 - v1;
993
Vector3 n2 = v2 - v3;
994
995
Vector3 axis = n1.cross(n2).cross(n1).normalized();
996
997
if (!separator.test_axis(axis)) {
998
return;
999
}
1000
}
1001
1002
// vertices of B
1003
for (int i = 0; i < vertex_count; i++) {
1004
Vector3 v1 = p_transform_b.xform(vertices[i]);
1005
Vector3 v2 = p_transform_a.origin;
1006
1007
Vector3 axis = (v2 - v1).normalized();
1008
1009
if (!separator.test_axis(axis)) {
1010
return;
1011
}
1012
}
1013
1014
separator.generate_contacts();
1015
}
1016
1017
template <bool withMargin>
1018
static void _collision_sphere_face(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1019
const GodotSphereShape3D *sphere_A = static_cast<const GodotSphereShape3D *>(p_a);
1020
const GodotFaceShape3D *face_B = static_cast<const GodotFaceShape3D *>(p_b);
1021
1022
SeparatorAxisTest<GodotSphereShape3D, GodotFaceShape3D, withMargin> separator(sphere_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1023
1024
Vector3 vertex[3] = {
1025
p_transform_b.xform(face_B->vertex[0]),
1026
p_transform_b.xform(face_B->vertex[1]),
1027
p_transform_b.xform(face_B->vertex[2]),
1028
};
1029
1030
Vector3 normal = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized();
1031
1032
if (!separator.test_axis(normal)) {
1033
return;
1034
}
1035
1036
// edges and points of B
1037
for (int i = 0; i < 3; i++) {
1038
Vector3 n1 = vertex[i] - p_transform_a.origin;
1039
if (n1.dot(normal) < 0.0) {
1040
n1 *= -1.0;
1041
}
1042
1043
if (!separator.test_axis(n1.normalized())) {
1044
return;
1045
}
1046
1047
Vector3 n2 = vertex[(i + 1) % 3] - vertex[i];
1048
1049
Vector3 axis = n1.cross(n2).cross(n2).normalized();
1050
if (axis.dot(normal) < 0.0) {
1051
axis *= -1.0;
1052
}
1053
1054
if (!separator.test_axis(axis)) {
1055
return;
1056
}
1057
}
1058
1059
if (!face_B->backface_collision) {
1060
if (separator.best_axis.dot(normal) < _BACKFACE_NORMAL_THRESHOLD) {
1061
if (face_B->invert_backface_collision) {
1062
separator.best_axis = separator.best_axis.bounce(normal);
1063
} else {
1064
// Just ignore backface collision.
1065
return;
1066
}
1067
}
1068
}
1069
1070
separator.generate_contacts();
1071
}
1072
1073
template <bool withMargin>
1074
static void _collision_box_box(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1075
const GodotBoxShape3D *box_A = static_cast<const GodotBoxShape3D *>(p_a);
1076
const GodotBoxShape3D *box_B = static_cast<const GodotBoxShape3D *>(p_b);
1077
1078
SeparatorAxisTest<GodotBoxShape3D, GodotBoxShape3D, withMargin> separator(box_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1079
1080
if (!separator.test_previous_axis()) {
1081
return;
1082
}
1083
1084
// test faces of A
1085
1086
for (int i = 0; i < 3; i++) {
1087
Vector3 axis = p_transform_a.basis.get_column(i).normalized();
1088
1089
if (!separator.test_axis(axis)) {
1090
return;
1091
}
1092
}
1093
1094
// test faces of B
1095
1096
for (int i = 0; i < 3; i++) {
1097
Vector3 axis = p_transform_b.basis.get_column(i).normalized();
1098
1099
if (!separator.test_axis(axis)) {
1100
return;
1101
}
1102
}
1103
1104
// test combined edges
1105
for (int i = 0; i < 3; i++) {
1106
for (int j = 0; j < 3; j++) {
1107
Vector3 axis = p_transform_a.basis.get_column(i).cross(p_transform_b.basis.get_column(j));
1108
1109
if (Math::is_zero_approx(axis.length_squared())) {
1110
continue;
1111
}
1112
axis.normalize();
1113
1114
if (!separator.test_axis(axis)) {
1115
return;
1116
}
1117
}
1118
}
1119
1120
if (withMargin) {
1121
//add endpoint test between closest vertices and edges
1122
1123
// calculate closest point to sphere
1124
1125
Vector3 ab_vec = p_transform_b.origin - p_transform_a.origin;
1126
1127
Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
1128
1129
Vector3 support_a = p_transform_a.xform(Vector3(
1130
1131
(cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
1132
(cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
1133
(cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
1134
1135
Vector3 cnormal_b = p_transform_b.basis.xform_inv(-ab_vec);
1136
1137
Vector3 support_b = p_transform_b.xform(Vector3(
1138
1139
(cnormal_b.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
1140
(cnormal_b.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
1141
(cnormal_b.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
1142
1143
Vector3 axis_ab = (support_a - support_b);
1144
1145
if (!separator.test_axis(axis_ab.normalized())) {
1146
return;
1147
}
1148
1149
//now try edges, which become cylinders!
1150
1151
for (int i = 0; i < 3; i++) {
1152
//a ->b
1153
Vector3 axis_a = p_transform_a.basis.get_column(i);
1154
1155
if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
1156
return;
1157
}
1158
1159
//b ->a
1160
Vector3 axis_b = p_transform_b.basis.get_column(i);
1161
1162
if (!separator.test_axis(axis_ab.cross(axis_b).cross(axis_b).normalized())) {
1163
return;
1164
}
1165
}
1166
}
1167
1168
separator.generate_contacts();
1169
}
1170
1171
template <bool withMargin>
1172
static void _collision_box_capsule(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1173
const GodotBoxShape3D *box_A = static_cast<const GodotBoxShape3D *>(p_a);
1174
const GodotCapsuleShape3D *capsule_B = static_cast<const GodotCapsuleShape3D *>(p_b);
1175
1176
SeparatorAxisTest<GodotBoxShape3D, GodotCapsuleShape3D, withMargin> separator(box_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1177
1178
if (!separator.test_previous_axis()) {
1179
return;
1180
}
1181
1182
// faces of A
1183
for (int i = 0; i < 3; i++) {
1184
Vector3 axis = p_transform_a.basis.get_column(i).normalized();
1185
1186
if (!separator.test_axis(axis)) {
1187
return;
1188
}
1189
}
1190
1191
Vector3 cyl_axis = p_transform_b.basis.get_column(1).normalized();
1192
1193
// edges of A, capsule cylinder
1194
1195
for (int i = 0; i < 3; i++) {
1196
// cylinder
1197
Vector3 box_axis = p_transform_a.basis.get_column(i);
1198
Vector3 axis = box_axis.cross(cyl_axis);
1199
if (Math::is_zero_approx(axis.length_squared())) {
1200
continue;
1201
}
1202
1203
if (!separator.test_axis(axis.normalized())) {
1204
return;
1205
}
1206
}
1207
1208
// points of A, capsule cylinder
1209
// this sure could be made faster somehow..
1210
1211
for (int i = 0; i < 2; i++) {
1212
for (int j = 0; j < 2; j++) {
1213
for (int k = 0; k < 2; k++) {
1214
Vector3 he = box_A->get_half_extents();
1215
he.x *= (i * 2 - 1);
1216
he.y *= (j * 2 - 1);
1217
he.z *= (k * 2 - 1);
1218
Vector3 point = p_transform_a.origin;
1219
for (int l = 0; l < 3; l++) {
1220
point += p_transform_a.basis.get_column(l) * he[l];
1221
}
1222
1223
//Vector3 axis = (point - cyl_axis * cyl_axis.dot(point)).normalized();
1224
Vector3 axis = Plane(cyl_axis).project(point).normalized();
1225
1226
if (!separator.test_axis(axis)) {
1227
return;
1228
}
1229
}
1230
}
1231
}
1232
1233
// capsule balls, edges of A
1234
1235
for (int i = 0; i < 2; i++) {
1236
Vector3 capsule_axis = p_transform_b.basis.get_column(1) * (capsule_B->get_height() * 0.5 - capsule_B->get_radius());
1237
1238
Vector3 sphere_pos = p_transform_b.origin + ((i == 0) ? capsule_axis : -capsule_axis);
1239
1240
Vector3 cnormal = p_transform_a.xform_inv(sphere_pos);
1241
1242
Vector3 cpoint = p_transform_a.xform(Vector3(
1243
1244
(cnormal.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
1245
(cnormal.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
1246
(cnormal.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
1247
1248
// use point to test axis
1249
Vector3 point_axis = (sphere_pos - cpoint).normalized();
1250
1251
if (!separator.test_axis(point_axis)) {
1252
return;
1253
}
1254
1255
// test edges of A
1256
1257
for (int j = 0; j < 3; j++) {
1258
Vector3 axis = point_axis.cross(p_transform_a.basis.get_column(j)).cross(p_transform_a.basis.get_column(j)).normalized();
1259
1260
if (!separator.test_axis(axis)) {
1261
return;
1262
}
1263
}
1264
}
1265
1266
separator.generate_contacts();
1267
}
1268
1269
template <bool withMargin>
1270
static void _collision_box_cylinder(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1271
const GodotBoxShape3D *box_A = static_cast<const GodotBoxShape3D *>(p_a);
1272
const GodotCylinderShape3D *cylinder_B = static_cast<const GodotCylinderShape3D *>(p_b);
1273
1274
SeparatorAxisTest<GodotBoxShape3D, GodotCylinderShape3D, withMargin> separator(box_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1275
1276
if (!separator.test_previous_axis()) {
1277
return;
1278
}
1279
1280
// Faces of A.
1281
for (int i = 0; i < 3; i++) {
1282
Vector3 axis = p_transform_a.basis.get_column(i).normalized();
1283
1284
if (!separator.test_axis(axis)) {
1285
return;
1286
}
1287
}
1288
1289
Vector3 cyl_axis = p_transform_b.basis.get_column(1).normalized();
1290
1291
// Cylinder end caps.
1292
{
1293
if (!separator.test_axis(cyl_axis)) {
1294
return;
1295
}
1296
}
1297
1298
// Edges of A, cylinder lateral surface.
1299
for (int i = 0; i < 3; i++) {
1300
Vector3 box_axis = p_transform_a.basis.get_column(i);
1301
Vector3 axis = box_axis.cross(cyl_axis);
1302
if (Math::is_zero_approx(axis.length_squared())) {
1303
continue;
1304
}
1305
1306
if (!separator.test_axis(axis.normalized())) {
1307
return;
1308
}
1309
}
1310
1311
// Gather points of A.
1312
Vector3 vertices_A[8];
1313
Vector3 box_extent = box_A->get_half_extents();
1314
for (int i = 0; i < 2; i++) {
1315
for (int j = 0; j < 2; j++) {
1316
for (int k = 0; k < 2; k++) {
1317
Vector3 extent = box_extent;
1318
extent.x *= (i * 2 - 1);
1319
extent.y *= (j * 2 - 1);
1320
extent.z *= (k * 2 - 1);
1321
Vector3 &point = vertices_A[i * 2 * 2 + j * 2 + k];
1322
point = p_transform_a.origin;
1323
for (int l = 0; l < 3; l++) {
1324
point += p_transform_a.basis.get_column(l) * extent[l];
1325
}
1326
}
1327
}
1328
}
1329
1330
// Points of A, cylinder lateral surface.
1331
for (int i = 0; i < 8; i++) {
1332
const Vector3 &point = vertices_A[i];
1333
Vector3 axis = Plane(cyl_axis).project(point).normalized();
1334
1335
if (!separator.test_axis(axis)) {
1336
return;
1337
}
1338
}
1339
1340
// Edges of A, cylinder end caps rim.
1341
int edges_start_A[12] = { 0, 2, 4, 6, 0, 1, 4, 5, 0, 1, 2, 3 };
1342
int edges_end_A[12] = { 1, 3, 5, 7, 2, 3, 6, 7, 4, 5, 6, 7 };
1343
1344
Vector3 cap_axis = cyl_axis * (cylinder_B->get_height() * 0.5);
1345
1346
for (int i = 0; i < 2; i++) {
1347
Vector3 cap_pos = p_transform_b.origin + ((i == 0) ? cap_axis : -cap_axis);
1348
1349
for (int e = 0; e < 12; e++) {
1350
const Vector3 &edge_start = vertices_A[edges_start_A[e]];
1351
const Vector3 &edge_end = vertices_A[edges_end_A[e]];
1352
1353
Vector3 edge_dir = (edge_end - edge_start);
1354
edge_dir.normalize();
1355
1356
real_t edge_dot = edge_dir.dot(cyl_axis);
1357
if (Math::is_zero_approx(edge_dot)) {
1358
// Edge is perpendicular to cylinder axis.
1359
continue;
1360
}
1361
1362
// Calculate intersection between edge and circle plane.
1363
Vector3 edge_diff = cap_pos - edge_start;
1364
real_t diff_dot = edge_diff.dot(cyl_axis);
1365
Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
1366
1367
// Calculate tangent that touches intersection.
1368
Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
1369
1370
// Axis is orthogonal both to tangent and edge direction.
1371
Vector3 axis = tangent.cross(edge_dir);
1372
1373
if (!separator.test_axis(axis.normalized())) {
1374
return;
1375
}
1376
}
1377
}
1378
1379
separator.generate_contacts();
1380
}
1381
1382
template <bool withMargin>
1383
static void _collision_box_convex_polygon(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1384
const GodotBoxShape3D *box_A = static_cast<const GodotBoxShape3D *>(p_a);
1385
const GodotConvexPolygonShape3D *convex_polygon_B = static_cast<const GodotConvexPolygonShape3D *>(p_b);
1386
1387
SeparatorAxisTest<GodotBoxShape3D, GodotConvexPolygonShape3D, withMargin> separator(box_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1388
1389
if (!separator.test_previous_axis()) {
1390
return;
1391
}
1392
1393
const Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
1394
1395
const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
1396
int face_count = mesh.faces.size();
1397
const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
1398
int edge_count = mesh.edges.size();
1399
const Vector3 *vertices = mesh.vertices.ptr();
1400
int vertex_count = mesh.vertices.size();
1401
1402
// faces of A
1403
for (int i = 0; i < 3; i++) {
1404
Vector3 axis = p_transform_a.basis.get_column(i).normalized();
1405
1406
if (!separator.test_axis(axis)) {
1407
return;
1408
}
1409
}
1410
1411
// Precalculating this makes the transforms faster.
1412
Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
1413
1414
// faces of B
1415
for (int i = 0; i < face_count; i++) {
1416
Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
1417
1418
if (!separator.test_axis(axis)) {
1419
return;
1420
}
1421
}
1422
1423
// A<->B edges
1424
for (int i = 0; i < 3; i++) {
1425
Vector3 e1 = p_transform_a.basis.get_column(i);
1426
1427
for (int j = 0; j < edge_count; j++) {
1428
Vector3 e2 = p_transform_b.basis.xform(vertices[edges[j].vertex_a]) - p_transform_b.basis.xform(vertices[edges[j].vertex_b]);
1429
1430
Vector3 axis = e1.cross(e2).normalized();
1431
1432
if (!separator.test_axis(axis)) {
1433
return;
1434
}
1435
}
1436
}
1437
1438
if (withMargin) {
1439
// calculate closest points between vertices and box edges
1440
for (int v = 0; v < vertex_count; v++) {
1441
Vector3 vtxb = p_transform_b.xform(vertices[v]);
1442
Vector3 ab_vec = vtxb - p_transform_a.origin;
1443
1444
Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
1445
1446
Vector3 support_a = p_transform_a.xform(Vector3(
1447
1448
(cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
1449
(cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
1450
(cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
1451
1452
Vector3 axis_ab = support_a - vtxb;
1453
1454
if (!separator.test_axis(axis_ab.normalized())) {
1455
return;
1456
}
1457
1458
//now try edges, which become cylinders!
1459
1460
for (int i = 0; i < 3; i++) {
1461
//a ->b
1462
Vector3 axis_a = p_transform_a.basis.get_column(i);
1463
1464
if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
1465
return;
1466
}
1467
}
1468
}
1469
1470
//convex edges and box points
1471
for (int i = 0; i < 2; i++) {
1472
for (int j = 0; j < 2; j++) {
1473
for (int k = 0; k < 2; k++) {
1474
Vector3 he = box_A->get_half_extents();
1475
he.x *= (i * 2 - 1);
1476
he.y *= (j * 2 - 1);
1477
he.z *= (k * 2 - 1);
1478
Vector3 point = p_transform_a.origin;
1479
for (int l = 0; l < 3; l++) {
1480
point += p_transform_a.basis.get_column(l) * he[l];
1481
}
1482
1483
for (int e = 0; e < edge_count; e++) {
1484
Vector3 p1 = p_transform_b.xform(vertices[edges[e].vertex_a]);
1485
Vector3 p2 = p_transform_b.xform(vertices[edges[e].vertex_b]);
1486
Vector3 n = (p2 - p1);
1487
1488
if (!separator.test_axis((point - p2).cross(n).cross(n).normalized())) {
1489
return;
1490
}
1491
}
1492
}
1493
}
1494
}
1495
}
1496
1497
separator.generate_contacts();
1498
}
1499
1500
template <bool withMargin>
1501
static void _collision_box_face(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1502
const GodotBoxShape3D *box_A = static_cast<const GodotBoxShape3D *>(p_a);
1503
const GodotFaceShape3D *face_B = static_cast<const GodotFaceShape3D *>(p_b);
1504
1505
SeparatorAxisTest<GodotBoxShape3D, GodotFaceShape3D, withMargin> separator(box_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1506
1507
Vector3 vertex[3] = {
1508
p_transform_b.xform(face_B->vertex[0]),
1509
p_transform_b.xform(face_B->vertex[1]),
1510
p_transform_b.xform(face_B->vertex[2]),
1511
};
1512
1513
Vector3 normal = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized();
1514
1515
if (!separator.test_axis(normal)) {
1516
return;
1517
}
1518
1519
// faces of A
1520
for (int i = 0; i < 3; i++) {
1521
Vector3 axis = p_transform_a.basis.get_column(i).normalized();
1522
if (axis.dot(normal) < 0.0) {
1523
axis *= -1.0;
1524
}
1525
1526
if (!separator.test_axis(axis)) {
1527
return;
1528
}
1529
}
1530
1531
// combined edges
1532
1533
for (int i = 0; i < 3; i++) {
1534
Vector3 e = vertex[i] - vertex[(i + 1) % 3];
1535
1536
for (int j = 0; j < 3; j++) {
1537
Vector3 axis = e.cross(p_transform_a.basis.get_column(j)).normalized();
1538
if (axis.dot(normal) < 0.0) {
1539
axis *= -1.0;
1540
}
1541
1542
if (!separator.test_axis(axis)) {
1543
return;
1544
}
1545
}
1546
}
1547
1548
if (withMargin) {
1549
// calculate closest points between vertices and box edges
1550
for (int v = 0; v < 3; v++) {
1551
Vector3 ab_vec = vertex[v] - p_transform_a.origin;
1552
1553
Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
1554
1555
Vector3 support_a = p_transform_a.xform(Vector3(
1556
1557
(cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
1558
(cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
1559
(cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
1560
1561
Vector3 axis_ab = support_a - vertex[v];
1562
if (axis_ab.dot(normal) < 0.0) {
1563
axis_ab *= -1.0;
1564
}
1565
1566
if (!separator.test_axis(axis_ab.normalized())) {
1567
return;
1568
}
1569
1570
//now try edges, which become cylinders!
1571
1572
for (int i = 0; i < 3; i++) {
1573
//a ->b
1574
Vector3 axis_a = p_transform_a.basis.get_column(i);
1575
1576
Vector3 axis = axis_ab.cross(axis_a).cross(axis_a).normalized();
1577
if (axis.dot(normal) < 0.0) {
1578
axis *= -1.0;
1579
}
1580
1581
if (!separator.test_axis(axis)) {
1582
return;
1583
}
1584
}
1585
}
1586
1587
//convex edges and box points, there has to be a way to speed up this (get closest point?)
1588
for (int i = 0; i < 2; i++) {
1589
for (int j = 0; j < 2; j++) {
1590
for (int k = 0; k < 2; k++) {
1591
Vector3 he = box_A->get_half_extents();
1592
he.x *= (i * 2 - 1);
1593
he.y *= (j * 2 - 1);
1594
he.z *= (k * 2 - 1);
1595
Vector3 point = p_transform_a.origin;
1596
for (int l = 0; l < 3; l++) {
1597
point += p_transform_a.basis.get_column(l) * he[l];
1598
}
1599
1600
for (int e = 0; e < 3; e++) {
1601
Vector3 p1 = vertex[e];
1602
Vector3 p2 = vertex[(e + 1) % 3];
1603
1604
Vector3 n = (p2 - p1);
1605
1606
Vector3 axis = (point - p2).cross(n).cross(n).normalized();
1607
if (axis.dot(normal) < 0.0) {
1608
axis *= -1.0;
1609
}
1610
1611
if (!separator.test_axis(axis)) {
1612
return;
1613
}
1614
}
1615
}
1616
}
1617
}
1618
}
1619
1620
if (!face_B->backface_collision) {
1621
if (separator.best_axis.dot(normal) < _BACKFACE_NORMAL_THRESHOLD) {
1622
if (face_B->invert_backface_collision) {
1623
separator.best_axis = separator.best_axis.bounce(normal);
1624
} else {
1625
// Just ignore backface collision.
1626
return;
1627
}
1628
}
1629
}
1630
1631
separator.generate_contacts();
1632
}
1633
1634
template <bool withMargin>
1635
static void _collision_capsule_capsule(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1636
const GodotCapsuleShape3D *capsule_A = static_cast<const GodotCapsuleShape3D *>(p_a);
1637
const GodotCapsuleShape3D *capsule_B = static_cast<const GodotCapsuleShape3D *>(p_b);
1638
1639
real_t scale_A = p_transform_a.basis[0].length();
1640
real_t scale_B = p_transform_b.basis[0].length();
1641
1642
// Get the closest points between the capsule segments
1643
Vector3 capsule_A_closest;
1644
Vector3 capsule_B_closest;
1645
Vector3 capsule_A_axis = p_transform_a.basis.get_column(1) * (capsule_A->get_height() * 0.5 - capsule_A->get_radius());
1646
Vector3 capsule_B_axis = p_transform_b.basis.get_column(1) * (capsule_B->get_height() * 0.5 - capsule_B->get_radius());
1647
Geometry3D::get_closest_points_between_segments(
1648
p_transform_a.origin + capsule_A_axis,
1649
p_transform_a.origin - capsule_A_axis,
1650
p_transform_b.origin + capsule_B_axis,
1651
p_transform_b.origin - capsule_B_axis,
1652
capsule_A_closest,
1653
capsule_B_closest);
1654
1655
// Perform the analytic collision between the two closest capsule spheres
1656
analytic_sphere_collision<withMargin>(
1657
capsule_A_closest,
1658
capsule_A->get_radius() * scale_A,
1659
capsule_B_closest,
1660
capsule_B->get_radius() * scale_B,
1661
p_collector,
1662
p_margin_a,
1663
p_margin_b);
1664
}
1665
1666
template <bool withMargin>
1667
static void _collision_capsule_cylinder(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1668
const GodotCapsuleShape3D *capsule_A = static_cast<const GodotCapsuleShape3D *>(p_a);
1669
const GodotCylinderShape3D *cylinder_B = static_cast<const GodotCylinderShape3D *>(p_b);
1670
1671
// Find the closest points between the axes of the two objects.
1672
1673
Vector3 capsule_A_closest;
1674
Vector3 cylinder_B_closest;
1675
Vector3 capsule_A_axis = p_transform_a.basis.get_column(1) * (capsule_A->get_height() * 0.5 - capsule_A->get_radius());
1676
Vector3 cylinder_B_axis = p_transform_b.basis.get_column(1) * (cylinder_B->get_height() * 0.5);
1677
Geometry3D::get_closest_points_between_segments(
1678
p_transform_a.origin + capsule_A_axis,
1679
p_transform_a.origin - capsule_A_axis,
1680
p_transform_b.origin + cylinder_B_axis,
1681
p_transform_b.origin - cylinder_B_axis,
1682
capsule_A_closest,
1683
cylinder_B_closest);
1684
1685
// Perform the collision test between the cylinder and the nearest sphere on the capsule axis.
1686
1687
Transform3D sphere_transform(p_transform_a.basis, capsule_A_closest);
1688
analytic_sphere_cylinder_collision<withMargin>(capsule_A->get_radius(), cylinder_B->get_radius(), cylinder_B->get_height(), sphere_transform, p_transform_b, p_collector, p_margin_a, p_margin_b);
1689
}
1690
1691
template <bool withMargin>
1692
static void _collision_capsule_convex_polygon(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1693
const GodotCapsuleShape3D *capsule_A = static_cast<const GodotCapsuleShape3D *>(p_a);
1694
const GodotConvexPolygonShape3D *convex_polygon_B = static_cast<const GodotConvexPolygonShape3D *>(p_b);
1695
1696
SeparatorAxisTest<GodotCapsuleShape3D, GodotConvexPolygonShape3D, withMargin> separator(capsule_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1697
1698
if (!separator.test_previous_axis()) {
1699
return;
1700
}
1701
1702
const Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
1703
1704
const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
1705
int face_count = mesh.faces.size();
1706
const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
1707
int edge_count = mesh.edges.size();
1708
const Vector3 *vertices = mesh.vertices.ptr();
1709
1710
// Precalculating this makes the transforms faster.
1711
Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
1712
1713
// faces of B
1714
for (int i = 0; i < face_count; i++) {
1715
Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
1716
1717
if (!separator.test_axis(axis)) {
1718
return;
1719
}
1720
}
1721
1722
// edges of B, capsule cylinder
1723
1724
for (int i = 0; i < edge_count; i++) {
1725
// cylinder
1726
Vector3 edge_axis = p_transform_b.basis.xform(vertices[edges[i].vertex_a]) - p_transform_b.basis.xform(vertices[edges[i].vertex_b]);
1727
Vector3 axis = edge_axis.cross(p_transform_a.basis.get_column(1)).normalized();
1728
1729
if (!separator.test_axis(axis)) {
1730
return;
1731
}
1732
}
1733
1734
// capsule balls, edges of B
1735
1736
for (int i = 0; i < 2; i++) {
1737
// edges of B, capsule cylinder
1738
1739
Vector3 capsule_axis = p_transform_a.basis.get_column(1) * (capsule_A->get_height() * 0.5 - capsule_A->get_radius());
1740
1741
Vector3 sphere_pos = p_transform_a.origin + ((i == 0) ? capsule_axis : -capsule_axis);
1742
1743
for (int j = 0; j < edge_count; j++) {
1744
Vector3 n1 = sphere_pos - p_transform_b.xform(vertices[edges[j].vertex_a]);
1745
Vector3 n2 = p_transform_b.basis.xform(vertices[edges[j].vertex_a]) - p_transform_b.basis.xform(vertices[edges[j].vertex_b]);
1746
1747
Vector3 axis = n1.cross(n2).cross(n2).normalized();
1748
1749
if (!separator.test_axis(axis)) {
1750
return;
1751
}
1752
}
1753
}
1754
1755
separator.generate_contacts();
1756
}
1757
1758
template <bool withMargin>
1759
static void _collision_capsule_face(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1760
const GodotCapsuleShape3D *capsule_A = static_cast<const GodotCapsuleShape3D *>(p_a);
1761
const GodotFaceShape3D *face_B = static_cast<const GodotFaceShape3D *>(p_b);
1762
1763
SeparatorAxisTest<GodotCapsuleShape3D, GodotFaceShape3D, withMargin> separator(capsule_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1764
1765
Vector3 vertex[3] = {
1766
p_transform_b.xform(face_B->vertex[0]),
1767
p_transform_b.xform(face_B->vertex[1]),
1768
p_transform_b.xform(face_B->vertex[2]),
1769
};
1770
1771
Vector3 normal = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized();
1772
1773
if (!separator.test_axis(normal)) {
1774
return;
1775
}
1776
1777
// edges of B, capsule cylinder
1778
1779
Vector3 capsule_axis = p_transform_a.basis.get_column(1) * (capsule_A->get_height() * 0.5 - capsule_A->get_radius());
1780
1781
for (int i = 0; i < 3; i++) {
1782
// edge-cylinder
1783
Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
1784
1785
Vector3 axis = edge_axis.cross(capsule_axis).normalized();
1786
if (axis.dot(normal) < 0.0) {
1787
axis *= -1.0;
1788
}
1789
1790
if (!separator.test_axis(axis)) {
1791
return;
1792
}
1793
1794
Vector3 dir_axis = (p_transform_a.origin - vertex[i]).cross(capsule_axis).cross(capsule_axis).normalized();
1795
if (dir_axis.dot(normal) < 0.0) {
1796
dir_axis *= -1.0;
1797
}
1798
1799
if (!separator.test_axis(dir_axis)) {
1800
return;
1801
}
1802
1803
for (int j = 0; j < 2; j++) {
1804
// point-spheres
1805
Vector3 sphere_pos = p_transform_a.origin + ((j == 0) ? capsule_axis : -capsule_axis);
1806
1807
Vector3 n1 = sphere_pos - vertex[i];
1808
if (n1.dot(normal) < 0.0) {
1809
n1 *= -1.0;
1810
}
1811
1812
if (!separator.test_axis(n1.normalized())) {
1813
return;
1814
}
1815
1816
Vector3 n2 = edge_axis;
1817
1818
axis = n1.cross(n2).cross(n2);
1819
if (axis.dot(normal) < 0.0) {
1820
axis *= -1.0;
1821
}
1822
1823
if (!separator.test_axis(axis.normalized())) {
1824
return;
1825
}
1826
}
1827
}
1828
1829
if (!face_B->backface_collision) {
1830
if (separator.best_axis.dot(normal) < _BACKFACE_NORMAL_THRESHOLD) {
1831
if (face_B->invert_backface_collision) {
1832
separator.best_axis = separator.best_axis.bounce(normal);
1833
} else {
1834
// Just ignore backface collision.
1835
return;
1836
}
1837
}
1838
}
1839
1840
separator.generate_contacts();
1841
}
1842
1843
template <bool withMargin>
1844
static void _collision_cylinder_cylinder(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1845
const GodotCylinderShape3D *cylinder_A = static_cast<const GodotCylinderShape3D *>(p_a);
1846
const GodotCylinderShape3D *cylinder_B = static_cast<const GodotCylinderShape3D *>(p_b);
1847
1848
SeparatorAxisTest<GodotCylinderShape3D, GodotCylinderShape3D, withMargin> separator(cylinder_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1849
1850
Vector3 cylinder_A_axis = p_transform_a.basis.get_column(1);
1851
Vector3 cylinder_B_axis = p_transform_b.basis.get_column(1);
1852
1853
if (!separator.test_previous_axis()) {
1854
return;
1855
}
1856
1857
// Cylinder A end caps.
1858
if (!separator.test_axis(cylinder_A_axis.normalized())) {
1859
return;
1860
}
1861
1862
// Cylinder B end caps.
1863
if (!separator.test_axis(cylinder_B_axis.normalized())) {
1864
return;
1865
}
1866
1867
Vector3 cylinder_diff = p_transform_b.origin - p_transform_a.origin;
1868
1869
// Cylinder A lateral surface.
1870
if (!separator.test_axis(cylinder_A_axis.cross(cylinder_diff).cross(cylinder_A_axis).normalized())) {
1871
return;
1872
}
1873
1874
// Cylinder B lateral surface.
1875
if (!separator.test_axis(cylinder_B_axis.cross(cylinder_diff).cross(cylinder_B_axis).normalized())) {
1876
return;
1877
}
1878
1879
real_t proj = cylinder_A_axis.cross(cylinder_B_axis).cross(cylinder_B_axis).dot(cylinder_A_axis);
1880
if (Math::is_zero_approx(proj)) {
1881
// Parallel cylinders, handle with specific axes only.
1882
// Note: GJKEPA with no margin can lead to degenerate cases in this situation.
1883
separator.generate_contacts();
1884
return;
1885
}
1886
1887
GodotCollisionSolver3D::CallbackResult callback = SeparatorAxisTest<GodotCylinderShape3D, GodotCylinderShape3D, withMargin>::test_contact_points;
1888
1889
// Fallback to generic algorithm to find the best separating axis.
1890
if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator, false, p_margin_a, p_margin_b)) {
1891
return;
1892
}
1893
1894
separator.generate_contacts();
1895
}
1896
1897
template <bool withMargin>
1898
static void _collision_cylinder_convex_polygon(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1899
const GodotCylinderShape3D *cylinder_A = static_cast<const GodotCylinderShape3D *>(p_a);
1900
const GodotConvexPolygonShape3D *convex_polygon_B = static_cast<const GodotConvexPolygonShape3D *>(p_b);
1901
1902
SeparatorAxisTest<GodotCylinderShape3D, GodotConvexPolygonShape3D, withMargin> separator(cylinder_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1903
1904
GodotCollisionSolver3D::CallbackResult callback = SeparatorAxisTest<GodotCylinderShape3D, GodotConvexPolygonShape3D, withMargin>::test_contact_points;
1905
1906
// Fallback to generic algorithm to find the best separating axis.
1907
if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator, false, p_margin_a, p_margin_b)) {
1908
return;
1909
}
1910
1911
separator.generate_contacts();
1912
}
1913
1914
template <bool withMargin>
1915
static void _collision_cylinder_face(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
1916
const GodotCylinderShape3D *cylinder_A = static_cast<const GodotCylinderShape3D *>(p_a);
1917
const GodotFaceShape3D *face_B = static_cast<const GodotFaceShape3D *>(p_b);
1918
1919
SeparatorAxisTest<GodotCylinderShape3D, GodotFaceShape3D, withMargin> separator(cylinder_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
1920
1921
if (!separator.test_previous_axis()) {
1922
return;
1923
}
1924
1925
Vector3 vertex[3] = {
1926
p_transform_b.xform(face_B->vertex[0]),
1927
p_transform_b.xform(face_B->vertex[1]),
1928
p_transform_b.xform(face_B->vertex[2]),
1929
};
1930
1931
Vector3 normal = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized();
1932
1933
// Face B normal.
1934
if (!separator.test_axis(normal)) {
1935
return;
1936
}
1937
1938
Vector3 cyl_axis = p_transform_a.basis.get_column(1).normalized();
1939
if (cyl_axis.dot(normal) < 0.0) {
1940
cyl_axis *= -1.0;
1941
}
1942
1943
// Cylinder end caps.
1944
if (!separator.test_axis(cyl_axis)) {
1945
return;
1946
}
1947
1948
// Edges of B, cylinder lateral surface.
1949
for (int i = 0; i < 3; i++) {
1950
Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
1951
Vector3 axis = edge_axis.cross(cyl_axis);
1952
if (Math::is_zero_approx(axis.length_squared())) {
1953
continue;
1954
}
1955
1956
if (axis.dot(normal) < 0.0) {
1957
axis *= -1.0;
1958
}
1959
1960
if (!separator.test_axis(axis.normalized())) {
1961
return;
1962
}
1963
}
1964
1965
// Points of B, cylinder lateral surface.
1966
for (int i = 0; i < 3; i++) {
1967
const Vector3 point = vertex[i] - p_transform_a.origin;
1968
Vector3 axis = Plane(cyl_axis).project(point).normalized();
1969
if (axis.dot(normal) < 0.0) {
1970
axis *= -1.0;
1971
}
1972
1973
if (!separator.test_axis(axis)) {
1974
return;
1975
}
1976
}
1977
1978
// Edges of B, cylinder end caps rim.
1979
Vector3 cap_axis = cyl_axis * (cylinder_A->get_height() * 0.5);
1980
1981
for (int i = 0; i < 2; i++) {
1982
Vector3 cap_pos = p_transform_a.origin + ((i == 0) ? cap_axis : -cap_axis);
1983
1984
for (int j = 0; j < 3; j++) {
1985
const Vector3 &edge_start = vertex[j];
1986
const Vector3 &edge_end = vertex[(j + 1) % 3];
1987
Vector3 edge_dir = edge_end - edge_start;
1988
edge_dir.normalize();
1989
1990
real_t edge_dot = edge_dir.dot(cyl_axis);
1991
if (Math::is_zero_approx(edge_dot)) {
1992
// Edge is perpendicular to cylinder axis.
1993
continue;
1994
}
1995
1996
// Calculate intersection between edge and circle plane.
1997
Vector3 edge_diff = cap_pos - edge_start;
1998
real_t diff_dot = edge_diff.dot(cyl_axis);
1999
Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
2000
2001
// Calculate tangent that touches intersection.
2002
Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
2003
2004
// Axis is orthogonal both to tangent and edge direction.
2005
Vector3 axis = tangent.cross(edge_dir);
2006
if (axis.dot(normal) < 0.0) {
2007
axis *= -1.0;
2008
}
2009
2010
if (!separator.test_axis(axis.normalized())) {
2011
return;
2012
}
2013
}
2014
}
2015
2016
if (!face_B->backface_collision) {
2017
if (separator.best_axis.dot(normal) < _BACKFACE_NORMAL_THRESHOLD) {
2018
if (face_B->invert_backface_collision) {
2019
separator.best_axis = separator.best_axis.bounce(normal);
2020
} else {
2021
// Just ignore backface collision.
2022
return;
2023
}
2024
}
2025
}
2026
2027
separator.generate_contacts();
2028
}
2029
2030
static _FORCE_INLINE_ bool is_minkowski_face(const Vector3 &A, const Vector3 &B, const Vector3 &B_x_A, const Vector3 &C, const Vector3 &D, const Vector3 &D_x_C) {
2031
// Test if arcs AB and CD intersect on the unit sphere
2032
real_t CBA = C.dot(B_x_A);
2033
real_t DBA = D.dot(B_x_A);
2034
real_t ADC = A.dot(D_x_C);
2035
real_t BDC = B.dot(D_x_C);
2036
2037
return (CBA * DBA < 0.0f) && (ADC * BDC < 0.0f) && (CBA * BDC > 0.0f);
2038
}
2039
2040
template <bool withMargin>
2041
static void _collision_convex_polygon_convex_polygon(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
2042
const GodotConvexPolygonShape3D *convex_polygon_A = static_cast<const GodotConvexPolygonShape3D *>(p_a);
2043
const GodotConvexPolygonShape3D *convex_polygon_B = static_cast<const GodotConvexPolygonShape3D *>(p_b);
2044
2045
SeparatorAxisTest<GodotConvexPolygonShape3D, GodotConvexPolygonShape3D, withMargin> separator(convex_polygon_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
2046
2047
if (!separator.test_previous_axis()) {
2048
return;
2049
}
2050
2051
const Geometry3D::MeshData &mesh_A = convex_polygon_A->get_mesh();
2052
2053
const Geometry3D::MeshData::Face *faces_A = mesh_A.faces.ptr();
2054
int face_count_A = mesh_A.faces.size();
2055
const Geometry3D::MeshData::Edge *edges_A = mesh_A.edges.ptr();
2056
int edge_count_A = mesh_A.edges.size();
2057
const Vector3 *vertices_A = mesh_A.vertices.ptr();
2058
int vertex_count_A = mesh_A.vertices.size();
2059
2060
const Geometry3D::MeshData &mesh_B = convex_polygon_B->get_mesh();
2061
2062
const Geometry3D::MeshData::Face *faces_B = mesh_B.faces.ptr();
2063
int face_count_B = mesh_B.faces.size();
2064
const Geometry3D::MeshData::Edge *edges_B = mesh_B.edges.ptr();
2065
int edge_count_B = mesh_B.edges.size();
2066
const Vector3 *vertices_B = mesh_B.vertices.ptr();
2067
int vertex_count_B = mesh_B.vertices.size();
2068
2069
// Precalculating this makes the transforms faster.
2070
Basis a_xform_normal = p_transform_a.basis.inverse().transposed();
2071
2072
// faces of A
2073
for (int i = 0; i < face_count_A; i++) {
2074
Vector3 axis = a_xform_normal.xform(faces_A[i].plane.normal).normalized();
2075
2076
if (!separator.test_axis(axis)) {
2077
return;
2078
}
2079
}
2080
2081
// Precalculating this makes the transforms faster.
2082
Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
2083
2084
// faces of B
2085
for (int i = 0; i < face_count_B; i++) {
2086
Vector3 axis = b_xform_normal.xform(faces_B[i].plane.normal).normalized();
2087
2088
if (!separator.test_axis(axis)) {
2089
return;
2090
}
2091
}
2092
2093
// A<->B edges
2094
2095
for (int i = 0; i < edge_count_A; i++) {
2096
Vector3 p1 = p_transform_a.xform(vertices_A[edges_A[i].vertex_a]);
2097
Vector3 q1 = p_transform_a.xform(vertices_A[edges_A[i].vertex_b]);
2098
Vector3 e1 = q1 - p1;
2099
Vector3 u1 = p_transform_a.basis.xform(faces_A[edges_A[i].face_a].plane.normal).normalized();
2100
Vector3 v1 = p_transform_a.basis.xform(faces_A[edges_A[i].face_b].plane.normal).normalized();
2101
2102
for (int j = 0; j < edge_count_B; j++) {
2103
Vector3 p2 = p_transform_b.xform(vertices_B[edges_B[j].vertex_a]);
2104
Vector3 q2 = p_transform_b.xform(vertices_B[edges_B[j].vertex_b]);
2105
Vector3 e2 = q2 - p2;
2106
Vector3 u2 = p_transform_b.basis.xform(faces_B[edges_B[j].face_a].plane.normal).normalized();
2107
Vector3 v2 = p_transform_b.basis.xform(faces_B[edges_B[j].face_b].plane.normal).normalized();
2108
2109
if (is_minkowski_face(u1, v1, -e1, -u2, -v2, -e2)) {
2110
Vector3 axis = e1.cross(e2).normalized();
2111
2112
if (!separator.test_axis(axis)) {
2113
return;
2114
}
2115
}
2116
}
2117
}
2118
2119
if (withMargin) {
2120
//vertex-vertex
2121
for (int i = 0; i < vertex_count_A; i++) {
2122
Vector3 va = p_transform_a.xform(vertices_A[i]);
2123
2124
for (int j = 0; j < vertex_count_B; j++) {
2125
if (!separator.test_axis((va - p_transform_b.xform(vertices_B[j])).normalized())) {
2126
return;
2127
}
2128
}
2129
}
2130
//edge-vertex (shell)
2131
2132
for (int i = 0; i < edge_count_A; i++) {
2133
Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].vertex_a]);
2134
Vector3 e2 = p_transform_a.basis.xform(vertices_A[edges_A[i].vertex_b]);
2135
Vector3 n = (e2 - e1);
2136
2137
for (int j = 0; j < vertex_count_B; j++) {
2138
Vector3 e3 = p_transform_b.xform(vertices_B[j]);
2139
2140
if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
2141
return;
2142
}
2143
}
2144
}
2145
2146
for (int i = 0; i < edge_count_B; i++) {
2147
Vector3 e1 = p_transform_b.basis.xform(vertices_B[edges_B[i].vertex_a]);
2148
Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[i].vertex_b]);
2149
Vector3 n = (e2 - e1);
2150
2151
for (int j = 0; j < vertex_count_A; j++) {
2152
Vector3 e3 = p_transform_a.xform(vertices_A[j]);
2153
2154
if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
2155
return;
2156
}
2157
}
2158
}
2159
}
2160
2161
separator.generate_contacts();
2162
}
2163
2164
template <bool withMargin>
2165
static void _collision_convex_polygon_face(const GodotShape3D *p_a, const Transform3D &p_transform_a, const GodotShape3D *p_b, const Transform3D &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
2166
const GodotConvexPolygonShape3D *convex_polygon_A = static_cast<const GodotConvexPolygonShape3D *>(p_a);
2167
const GodotFaceShape3D *face_B = static_cast<const GodotFaceShape3D *>(p_b);
2168
2169
SeparatorAxisTest<GodotConvexPolygonShape3D, GodotFaceShape3D, withMargin> separator(convex_polygon_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
2170
2171
const Geometry3D::MeshData &mesh = convex_polygon_A->get_mesh();
2172
2173
const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
2174
int face_count = mesh.faces.size();
2175
const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
2176
int edge_count = mesh.edges.size();
2177
const Vector3 *vertices = mesh.vertices.ptr();
2178
int vertex_count = mesh.vertices.size();
2179
2180
Vector3 vertex[3] = {
2181
p_transform_b.xform(face_B->vertex[0]),
2182
p_transform_b.xform(face_B->vertex[1]),
2183
p_transform_b.xform(face_B->vertex[2]),
2184
};
2185
2186
Vector3 normal = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized();
2187
2188
if (!separator.test_axis(normal)) {
2189
return;
2190
}
2191
2192
// faces of A
2193
for (int i = 0; i < face_count; i++) {
2194
//Vector3 axis = p_transform_a.xform( faces[i].plane ).normal;
2195
Vector3 axis = p_transform_a.basis.xform(faces[i].plane.normal).normalized();
2196
if (axis.dot(normal) < 0.0) {
2197
axis *= -1.0;
2198
}
2199
2200
if (!separator.test_axis(axis)) {
2201
return;
2202
}
2203
}
2204
2205
// A<->B edges
2206
for (int i = 0; i < edge_count; i++) {
2207
Vector3 e1 = p_transform_a.xform(vertices[edges[i].vertex_a]) - p_transform_a.xform(vertices[edges[i].vertex_b]);
2208
2209
for (int j = 0; j < 3; j++) {
2210
Vector3 e2 = vertex[j] - vertex[(j + 1) % 3];
2211
2212
Vector3 axis = e1.cross(e2).normalized();
2213
if (axis.dot(normal) < 0.0) {
2214
axis *= -1.0;
2215
}
2216
2217
if (!separator.test_axis(axis)) {
2218
return;
2219
}
2220
}
2221
}
2222
2223
if (withMargin) {
2224
//vertex-vertex
2225
for (int i = 0; i < vertex_count; i++) {
2226
Vector3 va = p_transform_a.xform(vertices[i]);
2227
2228
for (int j = 0; j < 3; j++) {
2229
Vector3 axis = (va - vertex[j]).normalized();
2230
if (axis.dot(normal) < 0.0) {
2231
axis *= -1.0;
2232
}
2233
2234
if (!separator.test_axis(axis)) {
2235
return;
2236
}
2237
}
2238
}
2239
//edge-vertex (shell)
2240
2241
for (int i = 0; i < edge_count; i++) {
2242
Vector3 e1 = p_transform_a.basis.xform(vertices[edges[i].vertex_a]);
2243
Vector3 e2 = p_transform_a.basis.xform(vertices[edges[i].vertex_b]);
2244
Vector3 n = (e2 - e1);
2245
2246
for (int j = 0; j < 3; j++) {
2247
Vector3 e3 = vertex[j];
2248
2249
Vector3 axis = (e1 - e3).cross(n).cross(n).normalized();
2250
if (axis.dot(normal) < 0.0) {
2251
axis *= -1.0;
2252
}
2253
2254
if (!separator.test_axis(axis)) {
2255
return;
2256
}
2257
}
2258
}
2259
2260
for (int i = 0; i < 3; i++) {
2261
Vector3 e1 = vertex[i];
2262
Vector3 e2 = vertex[(i + 1) % 3];
2263
Vector3 n = (e2 - e1);
2264
2265
for (int j = 0; j < vertex_count; j++) {
2266
Vector3 e3 = p_transform_a.xform(vertices[j]);
2267
2268
Vector3 axis = (e1 - e3).cross(n).cross(n).normalized();
2269
if (axis.dot(normal) < 0.0) {
2270
axis *= -1.0;
2271
}
2272
2273
if (!separator.test_axis(axis)) {
2274
return;
2275
}
2276
}
2277
}
2278
}
2279
2280
if (!face_B->backface_collision) {
2281
if (separator.best_axis.dot(normal) < _BACKFACE_NORMAL_THRESHOLD) {
2282
if (face_B->invert_backface_collision) {
2283
separator.best_axis = separator.best_axis.bounce(normal);
2284
} else {
2285
// Just ignore backface collision.
2286
return;
2287
}
2288
}
2289
}
2290
2291
separator.generate_contacts();
2292
}
2293
2294
bool sat_calculate_penetration(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, GodotCollisionSolver3D::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector3 *r_prev_axis, real_t p_margin_a, real_t p_margin_b) {
2295
PhysicsServer3D::ShapeType type_A = p_shape_A->get_type();
2296
2297
ERR_FAIL_COND_V(type_A == PhysicsServer3D::SHAPE_WORLD_BOUNDARY, false);
2298
ERR_FAIL_COND_V(type_A == PhysicsServer3D::SHAPE_SEPARATION_RAY, false);
2299
ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
2300
2301
PhysicsServer3D::ShapeType type_B = p_shape_B->get_type();
2302
2303
ERR_FAIL_COND_V(type_B == PhysicsServer3D::SHAPE_WORLD_BOUNDARY, false);
2304
ERR_FAIL_COND_V(type_B == PhysicsServer3D::SHAPE_SEPARATION_RAY, false);
2305
ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
2306
2307
static const CollisionFunc collision_table[6][6] = {
2308
{ _collision_sphere_sphere<false>,
2309
_collision_sphere_box<false>,
2310
_collision_sphere_capsule<false>,
2311
_collision_sphere_cylinder<false>,
2312
_collision_sphere_convex_polygon<false>,
2313
_collision_sphere_face<false> },
2314
{ nullptr,
2315
_collision_box_box<false>,
2316
_collision_box_capsule<false>,
2317
_collision_box_cylinder<false>,
2318
_collision_box_convex_polygon<false>,
2319
_collision_box_face<false> },
2320
{ nullptr,
2321
nullptr,
2322
_collision_capsule_capsule<false>,
2323
_collision_capsule_cylinder<false>,
2324
_collision_capsule_convex_polygon<false>,
2325
_collision_capsule_face<false> },
2326
{ nullptr,
2327
nullptr,
2328
nullptr,
2329
_collision_cylinder_cylinder<false>,
2330
_collision_cylinder_convex_polygon<false>,
2331
_collision_cylinder_face<false> },
2332
{ nullptr,
2333
nullptr,
2334
nullptr,
2335
nullptr,
2336
_collision_convex_polygon_convex_polygon<false>,
2337
_collision_convex_polygon_face<false> },
2338
{ nullptr,
2339
nullptr,
2340
nullptr,
2341
nullptr,
2342
nullptr,
2343
nullptr },
2344
};
2345
2346
static const CollisionFunc collision_table_margin[6][6] = {
2347
{ _collision_sphere_sphere<true>,
2348
_collision_sphere_box<true>,
2349
_collision_sphere_capsule<true>,
2350
_collision_sphere_cylinder<true>,
2351
_collision_sphere_convex_polygon<true>,
2352
_collision_sphere_face<true> },
2353
{ nullptr,
2354
_collision_box_box<true>,
2355
_collision_box_capsule<true>,
2356
_collision_box_cylinder<true>,
2357
_collision_box_convex_polygon<true>,
2358
_collision_box_face<true> },
2359
{ nullptr,
2360
nullptr,
2361
_collision_capsule_capsule<true>,
2362
_collision_capsule_cylinder<true>,
2363
_collision_capsule_convex_polygon<true>,
2364
_collision_capsule_face<true> },
2365
{ nullptr,
2366
nullptr,
2367
nullptr,
2368
_collision_cylinder_cylinder<true>,
2369
_collision_cylinder_convex_polygon<true>,
2370
_collision_cylinder_face<true> },
2371
{ nullptr,
2372
nullptr,
2373
nullptr,
2374
nullptr,
2375
_collision_convex_polygon_convex_polygon<true>,
2376
_collision_convex_polygon_face<true> },
2377
{ nullptr,
2378
nullptr,
2379
nullptr,
2380
nullptr,
2381
nullptr,
2382
nullptr },
2383
};
2384
2385
_CollectorCallback callback;
2386
callback.callback = p_result_callback;
2387
callback.swap = p_swap;
2388
callback.userdata = p_userdata;
2389
callback.collided = false;
2390
callback.prev_axis = r_prev_axis;
2391
2392
const GodotShape3D *A = p_shape_A;
2393
const GodotShape3D *B = p_shape_B;
2394
const Transform3D *transform_A = &p_transform_A;
2395
const Transform3D *transform_B = &p_transform_B;
2396
real_t margin_A = p_margin_a;
2397
real_t margin_B = p_margin_b;
2398
2399
if (type_A > type_B) {
2400
SWAP(A, B);
2401
SWAP(transform_A, transform_B);
2402
SWAP(type_A, type_B);
2403
SWAP(margin_A, margin_B);
2404
callback.swap = !callback.swap;
2405
}
2406
2407
CollisionFunc collision_func;
2408
if (margin_A != 0.0 || margin_B != 0.0) {
2409
collision_func = collision_table_margin[type_A - 2][type_B - 2];
2410
2411
} else {
2412
collision_func = collision_table[type_A - 2][type_B - 2];
2413
}
2414
ERR_FAIL_NULL_V(collision_func, false);
2415
2416
collision_func(A, *transform_A, B, *transform_B, &callback, margin_A, margin_B);
2417
2418
return callback.collided;
2419
}
2420
2421