2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* collision_solver_sw.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "collision_solver_sw.h"
|
|
|
|
#include "collision_solver_sat.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "gjk_epa.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#define collision_solver sat_calculate_penetration
|
|
|
|
//#define collision_solver gjk_epa_calculate_penetration
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool CollisionSolverSW::solve_static_plane(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
|
|
|
|
const PlaneShapeSW *plane = static_cast<const PlaneShapeSW *>(p_shape_A);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
Plane p = p_transform_A.xform(plane->get_plane());
|
|
|
|
|
|
|
|
static const int max_supports = 16;
|
|
|
|
Vector3 supports[max_supports];
|
|
|
|
int support_count;
|
2021-01-23 20:32:59 +00:00
|
|
|
ShapeSW::FeatureType support_type;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-01-23 20:32:59 +00:00
|
|
|
p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
|
|
|
|
|
|
|
|
if (support_type == ShapeSW::FEATURE_CIRCLE) {
|
|
|
|
ERR_FAIL_COND_V(support_count != 3, false);
|
|
|
|
|
|
|
|
Vector3 circle_pos = supports[0];
|
|
|
|
Vector3 circle_axis_1 = supports[1] - circle_pos;
|
|
|
|
Vector3 circle_axis_2 = supports[2] - circle_pos;
|
|
|
|
|
|
|
|
// Use 3 equidistant points on the circle.
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
Vector3 vertex_pos = circle_pos;
|
|
|
|
vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
|
|
|
|
vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
|
|
|
|
supports[i] = vertex_pos;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool found = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < support_count; i++) {
|
|
|
|
supports[i] = p_transform_B.xform(supports[i]);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p.distance_to(supports[i]) >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector3 support_A = p.project(supports[i]);
|
|
|
|
|
|
|
|
if (p_result_callback) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_swap_result) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_result_callback(supports[i], support_A, p_userdata);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_result_callback(support_A, supports[i], p_userdata);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2021-01-08 20:07:50 +00:00
|
|
|
bool CollisionSolverSW::solve_ray(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {
|
2017-03-05 15:44:50 +00:00
|
|
|
const RayShapeSW *ray = static_cast<const RayShapeSW *>(p_shape_A);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector3 from = p_transform_A.origin;
|
2021-01-08 20:07:50 +00:00
|
|
|
Vector3 to = from + p_transform_A.basis.get_axis(2) * (ray->get_length() + p_margin);
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 support_A = to;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Transform ai = p_transform_B.affine_inverse();
|
|
|
|
|
|
|
|
from = ai.xform(from);
|
|
|
|
to = ai.xform(to);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 p, n;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!p_shape_B->intersect_segment(from, to, p, n)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 support_B = p_transform_B.xform(p);
|
2018-02-19 19:59:57 +00:00
|
|
|
if (ray->get_slips_on_slope()) {
|
|
|
|
Vector3 global_n = ai.basis.xform_inv(n).normalized();
|
|
|
|
support_B = support_A + (support_B - support_A).length() * global_n;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_result_callback) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_swap_result) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_result_callback(support_B, support_A, p_userdata);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_result_callback(support_A, support_B, p_userdata);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct _ConcaveCollisionInfo {
|
|
|
|
const Transform *transform_A;
|
|
|
|
const ShapeSW *shape_A;
|
|
|
|
const Transform *transform_B;
|
|
|
|
CollisionSolverSW::CallbackResult result_callback;
|
|
|
|
void *userdata;
|
|
|
|
bool swap_result;
|
|
|
|
bool collided;
|
|
|
|
int aabb_tests;
|
|
|
|
int collisions;
|
2014-09-03 02:13:40 +00:00
|
|
|
bool tested;
|
2017-02-13 23:25:05 +00:00
|
|
|
real_t margin_A;
|
|
|
|
real_t margin_B;
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 close_A, close_B;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2021-05-08 01:31:41 +00:00
|
|
|
bool CollisionSolverSW::concave_callback(void *p_userdata, ShapeSW *p_convex) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
cinfo.aabb_tests++;
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, nullptr, cinfo.margin_A, cinfo.margin_B);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!collided) {
|
2021-05-08 01:31:41 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cinfo.collided = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
cinfo.collisions++;
|
2021-05-08 01:31:41 +00:00
|
|
|
|
|
|
|
// Stop at first collision if contacts are not needed.
|
|
|
|
return !cinfo.result_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool CollisionSolverSW::solve_concave(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_ConcaveCollisionInfo cinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
cinfo.transform_A = &p_transform_A;
|
|
|
|
cinfo.shape_A = p_shape_A;
|
|
|
|
cinfo.transform_B = &p_transform_B;
|
|
|
|
cinfo.result_callback = p_result_callback;
|
|
|
|
cinfo.userdata = p_userdata;
|
|
|
|
cinfo.swap_result = p_swap_result;
|
|
|
|
cinfo.collided = false;
|
|
|
|
cinfo.collisions = 0;
|
|
|
|
cinfo.margin_A = p_margin_A;
|
|
|
|
cinfo.margin_B = p_margin_B;
|
|
|
|
|
|
|
|
cinfo.aabb_tests = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Transform rel_transform = p_transform_A;
|
2017-03-05 15:44:50 +00:00
|
|
|
rel_transform.origin -= p_transform_B.origin;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//quickly compute a local AABB
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB local_aabb;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
Vector3 axis(p_transform_B.basis.get_axis(i));
|
|
|
|
real_t axis_scale = 1.0 / axis.length();
|
|
|
|
axis *= axis_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t smin, smax;
|
|
|
|
p_shape_A->project_range(axis, rel_transform, smin, smax);
|
|
|
|
smin -= p_margin_A;
|
|
|
|
smax += p_margin_A;
|
|
|
|
smin *= axis_scale;
|
|
|
|
smax *= axis_scale;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
local_aabb.position[i] = smin;
|
2017-03-05 15:44:50 +00:00
|
|
|
local_aabb.size[i] = smax - smin;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
concave_B->cull(local_aabb, concave_callback, &cinfo);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return cinfo.collided;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool CollisionSolverSW::solve_static(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, Vector3 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
PhysicsServer::ShapeType type_A = p_shape_A->get_type();
|
|
|
|
PhysicsServer::ShapeType type_B = p_shape_B->get_type();
|
|
|
|
bool concave_A = p_shape_A->is_concave();
|
|
|
|
bool concave_B = p_shape_B->is_concave();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
bool swap = false;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (type_A > type_B) {
|
|
|
|
SWAP(type_A, type_B);
|
|
|
|
SWAP(concave_A, concave_B);
|
|
|
|
swap = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (type_A == PhysicsServer::SHAPE_PLANE) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type_B == PhysicsServer::SHAPE_PLANE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (type_B == PhysicsServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return solve_static_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return solve_static_plane(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (type_A == PhysicsServer::SHAPE_RAY) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type_B == PhysicsServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (swap) {
|
2021-01-08 20:07:50 +00:00
|
|
|
return solve_ray(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, p_margin_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2021-01-08 20:07:50 +00:00
|
|
|
return solve_ray(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, p_margin_A);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (concave_B) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (concave_A) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!swap) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return solve_concave(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, p_margin_A, p_margin_B);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return solve_concave(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, p_margin_A, p_margin_B);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return collision_solver(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A, p_margin_B);
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 01:31:41 +00:00
|
|
|
bool CollisionSolverSW::concave_distance_callback(void *p_userdata, ShapeSW *p_convex) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
|
2014-09-03 02:13:40 +00:00
|
|
|
cinfo.aabb_tests++;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 close_A, close_B;
|
|
|
|
cinfo.collided = !gjk_epa_calculate_distance(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, close_A, close_B);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (cinfo.collided) {
|
2021-05-08 01:31:41 +00:00
|
|
|
// No need to process any more result.
|
|
|
|
return true;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2021-05-08 01:31:41 +00:00
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
if (!cinfo.tested || close_A.distance_squared_to(close_B) < cinfo.close_A.distance_squared_to(cinfo.close_B)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
cinfo.close_A = close_A;
|
|
|
|
cinfo.close_B = close_B;
|
|
|
|
cinfo.tested = true;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cinfo.collisions++;
|
2021-05-08 01:31:41 +00:00
|
|
|
|
|
|
|
return false;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool CollisionSolverSW::solve_distance_plane(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B) {
|
|
|
|
const PlaneShapeSW *plane = static_cast<const PlaneShapeSW *>(p_shape_A);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
|
2014-12-07 05:04:20 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-12-07 05:04:20 +00:00
|
|
|
Plane p = p_transform_A.xform(plane->get_plane());
|
|
|
|
|
|
|
|
static const int max_supports = 16;
|
|
|
|
Vector3 supports[max_supports];
|
|
|
|
int support_count;
|
2021-01-23 20:32:59 +00:00
|
|
|
ShapeSW::FeatureType support_type;
|
2014-12-07 05:04:20 +00:00
|
|
|
|
2021-01-23 20:32:59 +00:00
|
|
|
p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
|
|
|
|
|
|
|
|
if (support_type == ShapeSW::FEATURE_CIRCLE) {
|
|
|
|
ERR_FAIL_COND_V(support_count != 3, false);
|
|
|
|
|
|
|
|
Vector3 circle_pos = supports[0];
|
|
|
|
Vector3 circle_axis_1 = supports[1] - circle_pos;
|
|
|
|
Vector3 circle_axis_2 = supports[2] - circle_pos;
|
|
|
|
|
|
|
|
// Use 3 equidistant points on the circle.
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
Vector3 vertex_pos = circle_pos;
|
|
|
|
vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
|
|
|
|
vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
|
|
|
|
supports[i] = vertex_pos;
|
|
|
|
}
|
|
|
|
}
|
2014-12-07 05:04:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool collided = false;
|
2014-12-07 05:04:20 +00:00
|
|
|
Vector3 closest;
|
2017-09-01 20:33:39 +00:00
|
|
|
real_t closest_d = 0;
|
2014-12-07 05:04:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < support_count; i++) {
|
|
|
|
supports[i] = p_transform_B.xform(supports[i]);
|
2014-12-07 05:04:20 +00:00
|
|
|
real_t d = p.distance_to(supports[i]);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i == 0 || d < closest_d) {
|
|
|
|
closest = supports[i];
|
|
|
|
closest_d = d;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (d <= 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
collided = true;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-12-07 05:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
r_point_A = p.project(closest);
|
|
|
|
r_point_B = closest;
|
2014-12-07 05:04:20 +00:00
|
|
|
|
|
|
|
return collided;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
bool CollisionSolverSW::solve_distance(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B, const AABB &p_concave_hint, Vector3 *r_sep_axis) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_shape_A->is_concave()) {
|
2014-09-03 02:13:40 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
|
|
|
|
Vector3 a, b;
|
|
|
|
bool col = solve_distance_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, a, b);
|
|
|
|
r_point_A = b;
|
|
|
|
r_point_B = a;
|
2014-12-07 05:04:20 +00:00
|
|
|
return !col;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
} else if (p_shape_B->is_concave()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_shape_A->is_concave()) {
|
2014-09-03 02:13:40 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
_ConcaveCollisionInfo cinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
cinfo.transform_A = &p_transform_A;
|
|
|
|
cinfo.shape_A = p_shape_A;
|
|
|
|
cinfo.transform_B = &p_transform_B;
|
2021-05-04 14:00:45 +00:00
|
|
|
cinfo.result_callback = nullptr;
|
|
|
|
cinfo.userdata = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
cinfo.swap_result = false;
|
|
|
|
cinfo.collided = false;
|
|
|
|
cinfo.collisions = 0;
|
|
|
|
cinfo.aabb_tests = 0;
|
|
|
|
cinfo.tested = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
Transform rel_transform = p_transform_A;
|
2017-03-05 15:44:50 +00:00
|
|
|
rel_transform.origin -= p_transform_B.origin;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
//quickly compute a local AABB
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
bool use_cc_hint = p_concave_hint != AABB();
|
|
|
|
AABB cc_hint_aabb;
|
2014-09-03 02:13:40 +00:00
|
|
|
if (use_cc_hint) {
|
2017-03-05 15:44:50 +00:00
|
|
|
cc_hint_aabb = p_concave_hint;
|
2017-06-06 18:33:51 +00:00
|
|
|
cc_hint_aabb.position -= p_transform_B.origin;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB local_aabb;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
Vector3 axis(p_transform_B.basis.get_axis(i));
|
|
|
|
real_t axis_scale = ((real_t)1.0) / axis.length();
|
|
|
|
axis *= axis_scale;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t smin, smax;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (use_cc_hint) {
|
|
|
|
cc_hint_aabb.project_range_in_plane(Plane(axis, 0), smin, smax);
|
|
|
|
} else {
|
|
|
|
p_shape_A->project_range(axis, rel_transform, smin, smax);
|
|
|
|
}
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
smin *= axis_scale;
|
|
|
|
smax *= axis_scale;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
local_aabb.position[i] = smin;
|
2017-03-05 15:44:50 +00:00
|
|
|
local_aabb.size[i] = smax - smin;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
concave_B->cull(local_aabb, concave_distance_callback, &cinfo);
|
2014-09-03 02:13:40 +00:00
|
|
|
if (!cinfo.collided) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_point_A = cinfo.close_A;
|
|
|
|
r_point_B = cinfo.close_B;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return !cinfo.collided;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return gjk_epa_calculate_distance(p_shape_A, p_transform_A, p_shape_B, p_transform_B, r_point_A, r_point_B); //should pass sepaxis..
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|