2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* collision_solver_2d_sat.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "collision_solver_2d_sat.h"
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
#include "core/math/geometry_2d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct _CollectorCallback2D {
|
|
|
|
CollisionSolver2DSW::CallbackResult callback;
|
|
|
|
void *userdata;
|
|
|
|
bool swap;
|
|
|
|
bool collided;
|
|
|
|
Vector2 normal;
|
|
|
|
Vector2 *sep_axis;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void call(const Vector2 &p_point_A, const Vector2 &p_point_B) {
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (normal.dot(p_point_A) >= normal.dot(p_point_B))
|
|
|
|
return;
|
|
|
|
*/
|
2020-05-14 16:41:43 +02:00
|
|
|
if (swap) {
|
2017-03-05 16:44:50 +01:00
|
|
|
callback(p_point_B, p_point_A, userdata);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
callback(p_point_A, p_point_B, userdata);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef void (*GenerateContactsFunc)(const Vector2 *, int, const Vector2 *, int, _CollectorCallback2D *);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ static void _generate_contacts_point_point(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_point_count_A != 1);
|
|
|
|
ERR_FAIL_COND(p_point_count_B != 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_collector->call(*p_points_A, *p_points_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ static void _generate_contacts_point_edge(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_point_count_A != 1);
|
|
|
|
ERR_FAIL_COND(p_point_count_B != 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
Vector2 closest_B = Geometry2D::get_closest_point_to_segment_uncapped(*p_points_A, p_points_B);
|
2017-03-05 16:44:50 +01:00
|
|
|
p_collector->call(*p_points_A, closest_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct _generate_contacts_Pair {
|
2016-06-18 23:32:10 +02:00
|
|
|
bool a;
|
2014-02-10 02:10:30 +01:00
|
|
|
int idx;
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t d;
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator<(const _generate_contacts_Pair &l) const { return d < l.d; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ static void _generate_contacts_edge_edge(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_point_count_A != 2);
|
|
|
|
ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-18 23:32:10 +02:00
|
|
|
Vector2 n = p_collector->normal;
|
|
|
|
Vector2 t = n.tangent();
|
|
|
|
real_t dA = n.dot(p_points_A[0]);
|
|
|
|
real_t dB = n.dot(p_points_B[0]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_generate_contacts_Pair dvec[4];
|
2016-06-18 23:32:10 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dvec[0].d = t.dot(p_points_A[0]);
|
|
|
|
dvec[0].a = true;
|
|
|
|
dvec[0].idx = 0;
|
|
|
|
dvec[1].d = t.dot(p_points_A[1]);
|
|
|
|
dvec[1].a = true;
|
|
|
|
dvec[1].idx = 1;
|
|
|
|
dvec[2].d = t.dot(p_points_B[0]);
|
|
|
|
dvec[2].a = false;
|
|
|
|
dvec[2].idx = 0;
|
|
|
|
dvec[3].d = t.dot(p_points_B[1]);
|
|
|
|
dvec[3].a = false;
|
|
|
|
dvec[3].idx = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
SortArray<_generate_contacts_Pair> sa;
|
2017-03-05 16:44:50 +01:00
|
|
|
sa.sort(dvec, 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 1; i <= 2; i++) {
|
2016-06-18 23:32:10 +02:00
|
|
|
if (dvec[i].a) {
|
|
|
|
Vector2 a = p_points_A[dvec[i].idx];
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 b = n.plane_project(dB, a);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (n.dot(a) > n.dot(b) - CMP_EPSILON) {
|
2016-06-18 23:32:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
p_collector->call(a, b);
|
2016-06-18 23:32:10 +02:00
|
|
|
} else {
|
|
|
|
Vector2 b = p_points_B[dvec[i].idx];
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 a = n.plane_project(dA, b);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (n.dot(a) > n.dot(b) - CMP_EPSILON) {
|
2016-06-18 23:32:10 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
p_collector->call(a, b);
|
2016-06-18 23:32:10 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_point_count_A < 1);
|
|
|
|
ERR_FAIL_COND(p_point_count_B < 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const GenerateContactsFunc generate_contacts_func_table[2][2] = {
|
2014-02-10 02:10:30 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
_generate_contacts_point_point,
|
|
|
|
_generate_contacts_point_edge,
|
|
|
|
},
|
|
|
|
{
|
2020-04-02 01:20:12 +02:00
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_generate_contacts_edge_edge,
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int pointcount_B;
|
|
|
|
int pointcount_A;
|
|
|
|
const Vector2 *points_A;
|
|
|
|
const Vector2 *points_B;
|
|
|
|
|
|
|
|
if (p_point_count_A > p_point_count_B) {
|
|
|
|
//swap
|
|
|
|
p_collector->swap = !p_collector->swap;
|
|
|
|
p_collector->normal = -p_collector->normal;
|
|
|
|
|
|
|
|
pointcount_B = p_point_count_A;
|
|
|
|
pointcount_A = p_point_count_B;
|
2017-03-05 16:44:50 +01:00
|
|
|
points_A = p_points_B;
|
|
|
|
points_B = p_points_A;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
pointcount_B = p_point_count_B;
|
|
|
|
pointcount_A = p_point_count_A;
|
2017-03-05 16:44:50 +01:00
|
|
|
points_A = p_points_A;
|
|
|
|
points_B = p_points_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 09:14:31 +02:00
|
|
|
int version_A = (pointcount_A > 2 ? 2 : pointcount_A) - 1;
|
|
|
|
int version_B = (pointcount_B > 2 ? 2 : pointcount_B) - 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B];
|
|
|
|
ERR_FAIL_COND(!contacts_func);
|
2017-03-05 16:44:50 +01:00
|
|
|
contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_collector);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class ShapeA, class ShapeB, bool castA = false, bool castB = false, bool withMargin = false>
|
2014-02-10 02:10:30 +01:00
|
|
|
class SeparatorAxisTest2D {
|
|
|
|
const ShapeA *shape_A;
|
|
|
|
const ShapeB *shape_B;
|
2017-01-11 04:52:51 +01:00
|
|
|
const Transform2D *transform_A;
|
|
|
|
const Transform2D *transform_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t best_depth;
|
|
|
|
Vector2 best_axis;
|
|
|
|
int best_axis_count;
|
|
|
|
int best_axis_index;
|
2014-02-19 15:57:14 +01:00
|
|
|
Vector2 motion_A;
|
|
|
|
Vector2 motion_B;
|
2014-02-23 00:28:19 +01:00
|
|
|
real_t margin_A;
|
|
|
|
real_t margin_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
_CollectorCallback2D *callback;
|
|
|
|
|
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ bool test_previous_axis() {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (callback && callback->sep_axis && *callback->sep_axis != Vector2()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return test_axis(*callback->sep_axis);
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
best_axis_count++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ bool test_cast() {
|
|
|
|
if (castA) {
|
|
|
|
Vector2 na = motion_A.normalized();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!test_axis(na)) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!test_axis(na.tangent())) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castB) {
|
|
|
|
Vector2 nb = motion_B.normalized();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!test_axis(nb)) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!test_axis(nb.tangent())) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool test_axis(const Vector2 &p_axis) {
|
|
|
|
Vector2 axis = p_axis;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-01-16 16:42:53 +01:00
|
|
|
if (Math::is_zero_approx(axis.x) &&
|
|
|
|
Math::is_zero_approx(axis.y)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// strange case, try an upwards separator
|
2017-03-05 16:44:50 +01:00
|
|
|
axis = Vector2(0.0, 1.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t min_A, max_A, min_B, max_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (castA) {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_A->project_range_cast(motion_A, axis, *transform_A, min_A, max_A);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_A->project_range(axis, *transform_A, min_A, max_A);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (castB) {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_B->project_range_cast(motion_B, axis, *transform_B, min_B, max_B);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_B->project_range(axis, *transform_B, min_B, max_B);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
min_A -= margin_A;
|
|
|
|
max_A += margin_A;
|
|
|
|
min_B -= margin_B;
|
|
|
|
max_B += margin_B;
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
min_B -= (max_A - min_A) * 0.5;
|
|
|
|
max_B += (max_A - min_A) * 0.5;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t dmin = min_B - (min_A + max_A) * 0.5;
|
|
|
|
real_t dmax = max_B - (min_A + max_A) * 0.5;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (dmin > 0.0 || dmax < 0.0) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (callback && callback->sep_axis) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*callback->sep_axis = axis;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
best_axis_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false; // doesn't contain 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//use the smallest depth
|
|
|
|
|
|
|
|
dmin = Math::abs(dmin);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dmax < dmin) {
|
|
|
|
if (dmax < best_depth) {
|
|
|
|
best_depth = dmax;
|
|
|
|
best_axis = axis;
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
best_axis_index = best_axis_count;
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (dmin < best_depth) {
|
|
|
|
best_depth = dmin;
|
|
|
|
best_axis = -axis; // keep it as A axis
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
best_axis_index = best_axis_count;
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
best_axis_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void generate_contacts() {
|
|
|
|
// nothing to do, don't generate
|
2020-05-14 16:41:43 +02:00
|
|
|
if (best_axis == Vector2(0.0, 0.0)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-07-20 08:09:57 +02:00
|
|
|
if (callback) {
|
|
|
|
callback->collided = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!callback->callback) {
|
2019-07-20 08:09:57 +02:00
|
|
|
return; //only collide, no callback
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-20 08:09:57 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
static const int max_supports = 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector2 supports_A[max_supports];
|
|
|
|
int support_count_A;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (castA) {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_A->get_supports_transformed_cast(motion_A, -best_axis, *transform_A, supports_A, support_count_A);
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_A->get_supports(transform_A->basis_xform_inv(-best_axis).normalized(), supports_A, support_count_A);
|
|
|
|
for (int i = 0; i < support_count_A; i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
supports_A[i] = transform_A->xform(supports_A[i]);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < support_count_A; i++) {
|
|
|
|
supports_A[i] += -best_axis * margin_A;
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector2 supports_B[max_supports];
|
|
|
|
int support_count_B;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (castB) {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_B->get_supports_transformed_cast(motion_B, best_axis, *transform_B, supports_B, support_count_B);
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_B->get_supports(transform_B->basis_xform_inv(best_axis).normalized(), supports_B, support_count_B);
|
|
|
|
for (int i = 0; i < support_count_B; i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
supports_B[i] = transform_B->xform(supports_B[i]);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < support_count_B; i++) {
|
|
|
|
supports_B[i] += best_axis * margin_B;
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-20 08:09:57 +02:00
|
|
|
if (callback) {
|
|
|
|
callback->normal = best_axis;
|
|
|
|
_generate_contacts_from_supports(supports_A, support_count_A, supports_B, support_count_B, callback);
|
2014-02-23 00:28:19 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (callback->sep_axis && *callback->sep_axis != Vector2()) {
|
2019-07-20 08:09:57 +02:00
|
|
|
*callback->sep_axis = Vector2(); //invalidate previous axis (no test)
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-20 08:09:57 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A = Vector2(), const Vector2 &p_motion_B = Vector2(), real_t p_margin_A = 0, real_t p_margin_B = 0) {
|
|
|
|
margin_A = p_margin_A;
|
|
|
|
margin_B = p_margin_B;
|
|
|
|
best_depth = 1e15;
|
|
|
|
shape_A = p_shape_A;
|
|
|
|
shape_B = p_shape_B;
|
|
|
|
transform_A = &p_transform_a;
|
|
|
|
transform_B = &p_transform_b;
|
|
|
|
motion_A = p_motion_A;
|
|
|
|
motion_B = p_motion_B;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
callback = p_collector;
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
best_axis_count = 0;
|
|
|
|
best_axis_index = -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/****** SAT TESTS *******/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define TEST_POINT(m_a, m_b) \
|
|
|
|
((!separator.test_axis(((m_a) - (m_b)).normalized())) || \
|
|
|
|
(castA && !separator.test_axis(((m_a) + p_motion_a - (m_b)).normalized())) || \
|
|
|
|
(castB && !separator.test_axis(((m_a) - ((m_b) + p_motion_b)).normalized())) || \
|
|
|
|
(castA && castB && !separator.test_axis(((m_a) + p_motion_a - ((m_b) + p_motion_b)).normalized())))
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef void (*CollisionFunc)(const Shape2DSW *, const Transform2D &, const Shape2DSW *, const Transform2D &, _CollectorCallback2D *p_collector, const Vector2 &, const Vector2 &, real_t, real_t);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_segment_segment(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
|
|
|
|
const SegmentShape2DSW *segment_B = static_cast<const SegmentShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<SegmentShape2DSW, SegmentShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, segment_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
//this collision is kind of pointless
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!separator.test_axis(segment_B->get_xformed_normal(p_transform_b))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
|
|
|
//points grow to circles
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(segment_B->get_a()))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(segment_B->get_b()))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(segment_B->get_a()))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(segment_B->get_b()))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_segment_circle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
|
|
|
|
const CircleShape2DSW *circle_B = static_cast<const CircleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<SegmentShape2DSW, CircleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
//segment normal
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!separator.test_axis(
|
2020-05-14 16:41:43 +02:00
|
|
|
(p_transform_a.xform(segment_A->get_b()) - p_transform_a.xform(segment_A->get_a())).normalized().tangent())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
//endpoint a vs circle
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.get_origin())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
//endpoint b vs circle
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.get_origin())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_segment_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
|
|
|
|
const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<SegmentShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D inv = p_transform_b.affine_inverse();
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
Vector2 a = p_transform_a.xform(segment_A->get_a());
|
|
|
|
Vector2 b = p_transform_a.xform(segment_A->get_b());
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (castA) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a + p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b + p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b - p_motion_b))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA && castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b + p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b - p_motion_b + p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_segment_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
|
|
|
|
const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<SegmentShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_segment_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
|
|
|
|
const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<SegmentShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_B->get_point_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (withMargin) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(convex_B->get_point(i)))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(convex_B->get_point(i)))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_circle_circle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
|
|
|
|
const CircleShape2DSW *circle_B = static_cast<const CircleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CircleShape2DSW, CircleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.get_origin(), p_transform_b.get_origin())) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_circle_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
|
|
|
|
const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CircleShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector2 &sphere = p_transform_a.elements[2];
|
|
|
|
const Vector2 *axis = &p_transform_b.elements[0];
|
2017-01-14 12:26:56 +01:00
|
|
|
//const Vector2& half_extents = rectangle_B->get_half_extents();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(axis[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(axis[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D binv = p_transform_b.affine_inverse();
|
2014-02-19 15:57:14 +01:00
|
|
|
{
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphere))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA) {
|
|
|
|
Vector2 sphereofs = sphere + p_motion_a;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castB) {
|
|
|
|
Vector2 sphereofs = sphere - p_motion_b;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA && castB) {
|
|
|
|
Vector2 sphereofs = sphere - p_motion_b + p_motion_a;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_circle_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
|
|
|
|
const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CircleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//capsule axis
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//capsule endpoints
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.get_origin(), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (TEST_POINT(p_transform_a.get_origin(), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_circle_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
|
|
|
|
const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CircleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//poly faces and poly points vs circle
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_B->get_point_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.get_origin(), p_transform_b.xform(convex_B->get_point(i)))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_rectangle_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
|
|
|
|
const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<RectangleShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//box faces A
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//box faces B
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Transform2D invA = p_transform_a.affine_inverse();
|
|
|
|
Transform2D invB = p_transform_b.affine_inverse();
|
2014-02-23 00:28:19 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_box_axis(p_transform_a, invA, rectangle_B, p_transform_b, invB))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (castA || castB) {
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D aofs = p_transform_a;
|
2017-03-05 16:44:50 +01:00
|
|
|
aofs.elements[2] += p_motion_a;
|
2014-02-23 00:28:19 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D bofs = p_transform_b;
|
2017-03-05 16:44:50 +01:00
|
|
|
bofs.elements[2] += p_motion_b;
|
2014-02-23 00:28:19 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D aofsinv = aofs.affine_inverse();
|
|
|
|
Transform2D bofsinv = bofs.affine_inverse();
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (castA) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, p_transform_b, invB))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_box_axis(p_transform_a, invA, rectangle_B, bofs, bofsinv))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA && castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, bofs, bofsinv))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_rectangle_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
|
|
|
|
const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<RectangleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//box faces
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//capsule axis
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//box endpoints to capsule circles
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D boxinv = p_transform_a.affine_inverse();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
|
|
|
|
capsule_endpoint -= p_motion_a;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castB) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
|
|
|
|
capsule_endpoint += p_motion_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (castA && castB) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
|
|
|
|
capsule_endpoint -= p_motion_a;
|
|
|
|
capsule_endpoint += p_motion_b;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_rectangle_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
|
|
|
|
const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<RectangleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-11-02 15:31:01 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//box faces
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//convex faces
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D boxinv;
|
2014-02-23 00:28:19 +01:00
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
boxinv = p_transform_a.affine_inverse();
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_B->get_point_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (withMargin) {
|
|
|
|
//all points vs all points need to be tested if margin exist
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i))))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
if (castA) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) - p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
if (castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
if (castA && castB) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b - p_motion_a))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_capsule_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CapsuleShape2DSW *capsule_A = static_cast<const CapsuleShape2DSW *>(p_a);
|
|
|
|
const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CapsuleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//capsule axis
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//capsule endpoints
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
Vector2 capsule_endpoint_A = p_transform_a.get_origin() + p_transform_a.elements[1] * capsule_A->get_height() * (i == 0 ? 0.5 : -0.5);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
Vector2 capsule_endpoint_B = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (j == 0 ? 0.5 : -0.5);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(capsule_endpoint_A, capsule_endpoint_B)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_capsule_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const CapsuleShape2DSW *capsule_A = static_cast<const CapsuleShape2DSW *>(p_a);
|
|
|
|
const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<CapsuleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(capsule_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//capsule axis
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//poly vs capsule
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_B->get_point_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector2 cpoint = p_transform_b.xform(convex_B->get_point(i));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
Vector2 capsule_endpoint_A = p_transform_a.get_origin() + p_transform_a.elements[1] * capsule_A->get_height() * (j == 0 ? 0.5 : -0.5);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(capsule_endpoint_A, cpoint)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <bool castA, bool castB, bool withMargin>
|
|
|
|
static void _collision_convex_polygon_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const ConvexPolygonShape2DSW *convex_A = static_cast<const ConvexPolygonShape2DSW *>(p_a);
|
|
|
|
const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SeparatorAxisTest2D<ConvexPolygonShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(convex_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_previous_axis()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_cast()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_A->get_point_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_A->get_xformed_segment_normal(p_transform_a, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_B->get_point_count(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (withMargin) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < convex_A->get_point_count(); i++) {
|
|
|
|
for (int j = 0; j < convex_B->get_point_count(); j++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (TEST_POINT(p_transform_a.xform(convex_A->get_point(i)), p_transform_b.xform(convex_B->get_point(j)))) {
|
2014-02-23 00:28:19 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
separator.generate_contacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool sat_2d_calculate_penetration(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CollisionSolver2DSW::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector2 *sep_axis, real_t p_margin_A, real_t p_margin_B) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
ERR_FAIL_COND_V(type_A == PhysicsServer2D::SHAPE_LINE, false);
|
|
|
|
//ERR_FAIL_COND_V(type_A==PhysicsServer2D::SHAPE_RAY,false);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
ERR_FAIL_COND_V(type_B == PhysicsServer2D::SHAPE_LINE, false);
|
|
|
|
//ERR_FAIL_COND_V(type_B==PhysicsServer2D::SHAPE_RAY,false);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
|
|
|
|
|
|
|
|
static const CollisionFunc collision_table[5][5] = {
|
|
|
|
{ _collision_segment_segment<false, false, false>,
|
|
|
|
_collision_segment_circle<false, false, false>,
|
|
|
|
_collision_segment_rectangle<false, false, false>,
|
|
|
|
_collision_segment_capsule<false, false, false>,
|
|
|
|
_collision_segment_convex_polygon<false, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<false, false, false>,
|
|
|
|
_collision_circle_rectangle<false, false, false>,
|
|
|
|
_collision_circle_capsule<false, false, false>,
|
|
|
|
_collision_circle_convex_polygon<false, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<false, false, false>,
|
|
|
|
_collision_rectangle_capsule<false, false, false>,
|
|
|
|
_collision_rectangle_convex_polygon<false, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<false, false, false>,
|
|
|
|
_collision_capsule_convex_polygon<false, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<false, false, false> }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castA[5][5] = {
|
|
|
|
{ _collision_segment_segment<true, false, false>,
|
|
|
|
_collision_segment_circle<true, false, false>,
|
|
|
|
_collision_segment_rectangle<true, false, false>,
|
|
|
|
_collision_segment_capsule<true, false, false>,
|
|
|
|
_collision_segment_convex_polygon<true, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<true, false, false>,
|
|
|
|
_collision_circle_rectangle<true, false, false>,
|
|
|
|
_collision_circle_capsule<true, false, false>,
|
|
|
|
_collision_circle_convex_polygon<true, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<true, false, false>,
|
|
|
|
_collision_rectangle_capsule<true, false, false>,
|
|
|
|
_collision_rectangle_convex_polygon<true, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<true, false, false>,
|
|
|
|
_collision_capsule_convex_polygon<true, false, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<true, false, false> }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castB[5][5] = {
|
|
|
|
{ _collision_segment_segment<false, true, false>,
|
|
|
|
_collision_segment_circle<false, true, false>,
|
|
|
|
_collision_segment_rectangle<false, true, false>,
|
|
|
|
_collision_segment_capsule<false, true, false>,
|
|
|
|
_collision_segment_convex_polygon<false, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<false, true, false>,
|
|
|
|
_collision_circle_rectangle<false, true, false>,
|
|
|
|
_collision_circle_capsule<false, true, false>,
|
|
|
|
_collision_circle_convex_polygon<false, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<false, true, false>,
|
|
|
|
_collision_rectangle_capsule<false, true, false>,
|
|
|
|
_collision_rectangle_convex_polygon<false, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<false, true, false>,
|
|
|
|
_collision_capsule_convex_polygon<false, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<false, true, false> }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castA_castB[5][5] = {
|
|
|
|
{ _collision_segment_segment<true, true, false>,
|
|
|
|
_collision_segment_circle<true, true, false>,
|
|
|
|
_collision_segment_rectangle<true, true, false>,
|
|
|
|
_collision_segment_capsule<true, true, false>,
|
|
|
|
_collision_segment_convex_polygon<true, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<true, true, false>,
|
|
|
|
_collision_circle_rectangle<true, true, false>,
|
|
|
|
_collision_circle_capsule<true, true, false>,
|
|
|
|
_collision_circle_convex_polygon<true, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<true, true, false>,
|
|
|
|
_collision_rectangle_capsule<true, true, false>,
|
|
|
|
_collision_rectangle_convex_polygon<true, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<true, true, false>,
|
|
|
|
_collision_capsule_convex_polygon<true, true, false> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<true, true, false> }
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_margin[5][5] = {
|
|
|
|
{ _collision_segment_segment<false, false, true>,
|
|
|
|
_collision_segment_circle<false, false, true>,
|
|
|
|
_collision_segment_rectangle<false, false, true>,
|
|
|
|
_collision_segment_capsule<false, false, true>,
|
|
|
|
_collision_segment_convex_polygon<false, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<false, false, true>,
|
|
|
|
_collision_circle_rectangle<false, false, true>,
|
|
|
|
_collision_circle_capsule<false, false, true>,
|
|
|
|
_collision_circle_convex_polygon<false, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<false, false, true>,
|
|
|
|
_collision_rectangle_capsule<false, false, true>,
|
|
|
|
_collision_rectangle_convex_polygon<false, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<false, false, true>,
|
|
|
|
_collision_capsule_convex_polygon<false, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<false, false, true> }
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castA_margin[5][5] = {
|
|
|
|
{ _collision_segment_segment<true, false, true>,
|
|
|
|
_collision_segment_circle<true, false, true>,
|
|
|
|
_collision_segment_rectangle<true, false, true>,
|
|
|
|
_collision_segment_capsule<true, false, true>,
|
|
|
|
_collision_segment_convex_polygon<true, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<true, false, true>,
|
|
|
|
_collision_circle_rectangle<true, false, true>,
|
|
|
|
_collision_circle_capsule<true, false, true>,
|
|
|
|
_collision_circle_convex_polygon<true, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<true, false, true>,
|
|
|
|
_collision_rectangle_capsule<true, false, true>,
|
|
|
|
_collision_rectangle_convex_polygon<true, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<true, false, true>,
|
|
|
|
_collision_capsule_convex_polygon<true, false, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<true, false, true> }
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castB_margin[5][5] = {
|
|
|
|
{ _collision_segment_segment<false, true, true>,
|
|
|
|
_collision_segment_circle<false, true, true>,
|
|
|
|
_collision_segment_rectangle<false, true, true>,
|
|
|
|
_collision_segment_capsule<false, true, true>,
|
|
|
|
_collision_segment_convex_polygon<false, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<false, true, true>,
|
|
|
|
_collision_circle_rectangle<false, true, true>,
|
|
|
|
_collision_circle_capsule<false, true, true>,
|
|
|
|
_collision_circle_convex_polygon<false, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<false, true, true>,
|
|
|
|
_collision_rectangle_capsule<false, true, true>,
|
|
|
|
_collision_rectangle_convex_polygon<false, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<false, true, true>,
|
|
|
|
_collision_capsule_convex_polygon<false, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<false, true, true> }
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const CollisionFunc collision_table_castA_castB_margin[5][5] = {
|
|
|
|
{ _collision_segment_segment<true, true, true>,
|
|
|
|
_collision_segment_circle<true, true, true>,
|
|
|
|
_collision_segment_rectangle<true, true, true>,
|
|
|
|
_collision_segment_capsule<true, true, true>,
|
|
|
|
_collision_segment_convex_polygon<true, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_circle_circle<true, true, true>,
|
|
|
|
_collision_circle_rectangle<true, true, true>,
|
|
|
|
_collision_circle_capsule<true, true, true>,
|
|
|
|
_collision_circle_convex_polygon<true, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_rectangle_rectangle<true, true, true>,
|
|
|
|
_collision_rectangle_capsule<true, true, true>,
|
|
|
|
_collision_rectangle_convex_polygon<true, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_capsule_capsule<true, true, true>,
|
|
|
|
_collision_capsule_convex_polygon<true, true, true> },
|
2020-04-02 01:20:12 +02:00
|
|
|
{ nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-03-05 16:44:50 +01:00
|
|
|
_collision_convex_polygon_convex_polygon<true, true, true> }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
_CollectorCallback2D callback;
|
2017-03-05 16:44:50 +01:00
|
|
|
callback.callback = p_result_callback;
|
|
|
|
callback.swap = p_swap;
|
|
|
|
callback.userdata = p_userdata;
|
|
|
|
callback.collided = false;
|
|
|
|
callback.sep_axis = sep_axis;
|
|
|
|
|
|
|
|
const Shape2DSW *A = p_shape_A;
|
|
|
|
const Shape2DSW *B = p_shape_B;
|
|
|
|
const Transform2D *transform_A = &p_transform_A;
|
|
|
|
const Transform2D *transform_B = &p_transform_B;
|
|
|
|
const Vector2 *motion_A = &p_motion_A;
|
|
|
|
const Vector2 *motion_B = &p_motion_B;
|
|
|
|
real_t margin_A = p_margin_A, margin_B = p_margin_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (type_A > type_B) {
|
2017-03-05 16:44:50 +01:00
|
|
|
SWAP(A, B);
|
|
|
|
SWAP(transform_A, transform_B);
|
|
|
|
SWAP(type_A, type_B);
|
|
|
|
SWAP(motion_A, motion_B);
|
|
|
|
SWAP(margin_A, margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
callback.swap = !callback.swap;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
CollisionFunc collision_func;
|
2014-02-23 00:28:19 +01:00
|
|
|
|
|
|
|
if (p_margin_A || p_margin_B) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*motion_A == Vector2() && *motion_B == Vector2()) {
|
|
|
|
collision_func = collision_table_margin[type_A - 2][type_B - 2];
|
|
|
|
} else if (*motion_A != Vector2() && *motion_B == Vector2()) {
|
|
|
|
collision_func = collision_table_castA_margin[type_A - 2][type_B - 2];
|
|
|
|
} else if (*motion_A == Vector2() && *motion_B != Vector2()) {
|
|
|
|
collision_func = collision_table_castB_margin[type_A - 2][type_B - 2];
|
2014-02-23 00:28:19 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
collision_func = collision_table_castA_castB_margin[type_A - 2][type_B - 2];
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*motion_A == Vector2() && *motion_B == Vector2()) {
|
|
|
|
collision_func = collision_table[type_A - 2][type_B - 2];
|
|
|
|
} else if (*motion_A != Vector2() && *motion_B == Vector2()) {
|
|
|
|
collision_func = collision_table_castA[type_A - 2][type_B - 2];
|
|
|
|
} else if (*motion_A == Vector2() && *motion_B != Vector2()) {
|
|
|
|
collision_func = collision_table_castB[type_A - 2][type_B - 2];
|
2014-02-23 00:28:19 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
collision_func = collision_table_castA_castB[type_A - 2][type_B - 2];
|
2014-02-23 00:28:19 +01:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!collision_func, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
collision_func(A, *transform_A, B, *transform_B, &callback, *motion_A, *motion_B, margin_A, margin_B);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return callback.collided;
|
|
|
|
}
|