2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
2021-10-19 03:24:30 +08:00
|
|
|
/* godot_collision_solver_2d.cpp */
|
2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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-05 07:50:27 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
#include "godot_collision_solver_2d.h"
|
|
|
|
#include "godot_collision_solver_2d_sat.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
#define collision_solver sat_2d_calculate_penetration
|
|
|
|
//#define collision_solver gjk_epa_calculate_penetration
|
|
|
|
|
2022-08-29 23:34:06 +08:00
|
|
|
bool GodotCollisionSolver2D::solve_static_world_boundary(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {
|
2021-10-19 03:24:30 +08:00
|
|
|
const GodotWorldBoundaryShape2D *world_boundary = static_cast<const GodotWorldBoundaryShape2D *>(p_shape_A);
|
2021-09-15 01:52:35 +08:00
|
|
|
if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-09-15 01:52:35 +08:00
|
|
|
Vector2 n = p_transform_A.basis_xform(world_boundary->get_normal()).normalized();
|
|
|
|
Vector2 p = p_transform_A.xform(world_boundary->get_normal() * world_boundary->get_d());
|
2014-02-10 09:10:30 +08:00
|
|
|
real_t d = n.dot(p);
|
|
|
|
|
|
|
|
Vector2 supports[2];
|
|
|
|
int support_count;
|
|
|
|
|
2020-10-03 19:55:12 +08:00
|
|
|
p_shape_B->get_supports(p_transform_B.affine_inverse().basis_xform(-n).normalized(), supports, support_count);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < support_count; i++) {
|
2022-08-29 23:34:06 +08:00
|
|
|
supports[i] += p_margin * supports[i].normalized();
|
2014-02-10 09:10:30 +08:00
|
|
|
supports[i] = p_transform_B.xform(supports[i]);
|
2022-08-29 23:34:06 +08:00
|
|
|
supports[i] += p_motion_B;
|
2014-02-10 09:10:30 +08:00
|
|
|
real_t pd = n.dot(supports[i]);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (pd >= d) {
|
2014-02-10 09:10:30 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
found = true;
|
|
|
|
|
|
|
|
Vector2 support_A = supports[i] - n * (pd - d);
|
|
|
|
|
|
|
|
if (p_result_callback) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_swap_result) {
|
2014-02-10 09:10:30 +08:00
|
|
|
p_result_callback(supports[i], support_A, p_userdata);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2014-02-10 09:10:30 +08:00
|
|
|
p_result_callback(support_A, supports[i], p_userdata);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotCollisionSolver2D::solve_separation_ray(const GodotShape2D *p_shape_A, const Vector2 &p_motion_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *r_sep_axis, real_t p_margin) {
|
|
|
|
const GodotSeparationRayShape2D *ray = static_cast<const GodotSeparationRayShape2D *>(p_shape_A);
|
2021-08-20 02:21:56 +08:00
|
|
|
if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
|
2021-08-18 01:15:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 from = p_transform_A.get_origin();
|
2021-08-18 09:41:21 +08:00
|
|
|
Vector2 to = from + p_transform_A[1] * (ray->get_length() + p_margin);
|
2021-08-18 01:15:11 +08:00
|
|
|
if (p_motion_A != Vector2()) {
|
|
|
|
//not the best but should be enough
|
|
|
|
Vector2 normal = (to - from).normalized();
|
|
|
|
to += normal * MAX(0.0, normal.dot(p_motion_A));
|
|
|
|
}
|
|
|
|
Vector2 support_A = to;
|
|
|
|
|
|
|
|
Transform2D invb = p_transform_B.affine_inverse();
|
|
|
|
from = invb.xform(from);
|
|
|
|
to = invb.xform(to);
|
|
|
|
|
|
|
|
Vector2 p, n;
|
|
|
|
if (!p_shape_B->intersect_segment(from, to, p, n)) {
|
2021-08-18 09:41:21 +08:00
|
|
|
if (r_sep_axis) {
|
|
|
|
*r_sep_axis = p_transform_A[1].normalized();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard contacts when the ray is fully contained inside the shape.
|
|
|
|
if (n == Vector2()) {
|
|
|
|
if (r_sep_axis) {
|
|
|
|
*r_sep_axis = p_transform_A[1].normalized();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard contacts in the wrong direction.
|
|
|
|
if (n.dot(from - to) < CMP_EPSILON) {
|
|
|
|
if (r_sep_axis) {
|
|
|
|
*r_sep_axis = p_transform_A[1].normalized();
|
2021-08-18 01:15:11 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 support_B = p_transform_B.xform(p);
|
2021-08-20 02:02:40 +08:00
|
|
|
if (ray->get_slide_on_slope()) {
|
2021-08-18 01:15:11 +08:00
|
|
|
Vector2 global_n = invb.basis_xform_inv(n).normalized();
|
|
|
|
support_B = support_A + (support_B - support_A).length() * global_n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_result_callback) {
|
|
|
|
if (p_swap_result) {
|
|
|
|
p_result_callback(support_B, support_A, p_userdata);
|
|
|
|
} else {
|
|
|
|
p_result_callback(support_A, support_B, p_userdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
struct _ConcaveCollisionInfo2D {
|
2021-09-14 17:01:49 +08:00
|
|
|
const Transform2D *transform_A = nullptr;
|
2021-10-19 03:24:30 +08:00
|
|
|
const GodotShape2D *shape_A = nullptr;
|
2021-09-14 17:01:49 +08:00
|
|
|
const Transform2D *transform_B = nullptr;
|
2014-02-19 22:57:14 +08:00
|
|
|
Vector2 motion_A;
|
|
|
|
Vector2 motion_B;
|
2021-09-14 17:01:49 +08:00
|
|
|
real_t margin_A = 0.0;
|
|
|
|
real_t margin_B = 0.0;
|
2022-05-02 22:28:25 +08:00
|
|
|
GodotCollisionSolver2D::CallbackResult result_callback = nullptr;
|
2021-09-14 17:01:49 +08:00
|
|
|
void *userdata = nullptr;
|
|
|
|
bool swap_result = false;
|
|
|
|
bool collided = false;
|
|
|
|
int aabb_tests = 0;
|
|
|
|
int collisions = 0;
|
|
|
|
Vector2 *sep_axis = nullptr;
|
2014-02-10 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotCollisionSolver2D::concave_callback(void *p_userdata, GodotShape2D *p_convex) {
|
2022-04-05 18:40:26 +08:00
|
|
|
_ConcaveCollisionInfo2D &cinfo = *(static_cast<_ConcaveCollisionInfo2D *>(p_userdata));
|
2014-02-10 09:10:30 +08:00
|
|
|
cinfo.aabb_tests++;
|
|
|
|
|
2014-02-23 07:28:19 +08:00
|
|
|
bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, cinfo.motion_A, p_convex, *cinfo.transform_B, cinfo.motion_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, cinfo.sep_axis, cinfo.margin_A, cinfo.margin_B);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!collided) {
|
2021-05-08 10:16:04 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
cinfo.collided = true;
|
|
|
|
cinfo.collisions++;
|
2021-05-08 10:16:04 +08:00
|
|
|
|
|
|
|
// Stop at first collision if contacts are not needed.
|
|
|
|
return !cinfo.result_callback;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotCollisionSolver2D::solve_concave(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
|
|
|
|
const GodotConcaveShape2D *concave_B = static_cast<const GodotConcaveShape2D *>(p_shape_B);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_ConcaveCollisionInfo2D cinfo;
|
|
|
|
cinfo.transform_A = &p_transform_A;
|
|
|
|
cinfo.shape_A = p_shape_A;
|
|
|
|
cinfo.transform_B = &p_transform_B;
|
2014-02-19 22:57:14 +08:00
|
|
|
cinfo.motion_A = p_motion_A;
|
2014-02-10 09:10:30 +08:00
|
|
|
cinfo.result_callback = p_result_callback;
|
|
|
|
cinfo.userdata = p_userdata;
|
|
|
|
cinfo.swap_result = p_swap_result;
|
|
|
|
cinfo.collided = false;
|
|
|
|
cinfo.collisions = 0;
|
2021-08-18 09:41:21 +08:00
|
|
|
cinfo.sep_axis = r_sep_axis;
|
2014-02-23 07:28:19 +08:00
|
|
|
cinfo.margin_A = p_margin_A;
|
|
|
|
cinfo.margin_B = p_margin_B;
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
cinfo.aabb_tests = 0;
|
|
|
|
|
2017-01-11 11:52:51 +08:00
|
|
|
Transform2D rel_transform = p_transform_A;
|
2022-04-25 05:59:24 +08:00
|
|
|
rel_transform.columns[2] -= p_transform_B.get_origin();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2024-02-13 21:42:09 +08:00
|
|
|
// Quickly compute a local Rect2.
|
2014-02-10 09:10:30 +08:00
|
|
|
Rect2 local_aabb;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
2022-04-25 05:59:24 +08:00
|
|
|
Vector2 axis(p_transform_B.columns[i]);
|
2017-02-14 07:25:05 +08:00
|
|
|
real_t axis_scale = 1.0 / axis.length();
|
2014-02-10 09:10:30 +08:00
|
|
|
axis *= axis_scale;
|
|
|
|
|
2022-10-02 03:09:22 +08:00
|
|
|
real_t smin = 0.0, smax = 0.0;
|
2014-02-10 09:10:30 +08:00
|
|
|
p_shape_A->project_rangev(axis, rel_transform, smin, smax);
|
|
|
|
smin *= axis_scale;
|
|
|
|
smax *= axis_scale;
|
|
|
|
|
2017-06-04 06:25:13 +08:00
|
|
|
local_aabb.position[i] = smin;
|
2014-02-10 09:10:30 +08:00
|
|
|
local_aabb.size[i] = smax - smin;
|
|
|
|
}
|
2024-02-13 21:42:09 +08:00
|
|
|
// In case of motion, expand the Rect2 in the motion direction.
|
|
|
|
if (p_motion_A != Vector2()) {
|
|
|
|
Rect2 moved_aabb = local_aabb;
|
|
|
|
moved_aabb.position += p_motion_A;
|
|
|
|
local_aabb = local_aabb.merge(moved_aabb);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
concave_B->cull(local_aabb, concave_callback, &cinfo);
|
|
|
|
|
|
|
|
return cinfo.collided;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotCollisionSolver2D::solve(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, Vector2 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
|
2020-03-28 02:21:27 +08:00
|
|
|
PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();
|
|
|
|
PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();
|
2014-02-10 09:10:30 +08:00
|
|
|
bool concave_A = p_shape_A->is_concave();
|
|
|
|
bool concave_B = p_shape_B->is_concave();
|
2014-02-23 07:28:19 +08:00
|
|
|
real_t margin_A = p_margin_A, margin_B = p_margin_B;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
bool swap = false;
|
|
|
|
|
|
|
|
if (type_A > type_B) {
|
|
|
|
SWAP(type_A, type_B);
|
|
|
|
SWAP(concave_A, concave_B);
|
2014-02-23 07:28:19 +08:00
|
|
|
SWAP(margin_A, margin_B);
|
2014-02-10 09:10:30 +08:00
|
|
|
swap = true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 01:52:35 +08:00
|
|
|
if (type_A == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
|
|
|
|
if (type_B == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
|
2022-08-22 22:47:39 +08:00
|
|
|
WARN_PRINT_ONCE("Collisions between world boundaries are not supported.");
|
2014-02-10 09:10:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
2022-08-29 23:34:06 +08:00
|
|
|
return solve_static_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, p_margin_A);
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2022-08-29 23:34:06 +08:00
|
|
|
return solve_static_world_boundary(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, p_margin_B);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 02:21:56 +08:00
|
|
|
} else if (type_A == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
|
|
|
|
if (type_B == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
|
2022-08-22 22:47:39 +08:00
|
|
|
WARN_PRINT_ONCE("Collisions between two rays are not supported.");
|
2021-08-18 01:15:11 +08:00
|
|
|
return false; //no ray-ray
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
2021-08-20 02:21:56 +08:00
|
|
|
return solve_separation_ray(p_shape_B, p_motion_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, r_sep_axis, p_margin_B);
|
2021-08-18 01:15:11 +08:00
|
|
|
} else {
|
2021-08-20 02:21:56 +08:00
|
|
|
return solve_separation_ray(p_shape_A, p_motion_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A);
|
2021-08-18 01:15:11 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
} else if (concave_B) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (concave_A) {
|
2022-08-22 22:47:39 +08:00
|
|
|
WARN_PRINT_ONCE("Collisions between two concave shapes are not supported.");
|
2014-02-10 09:10:30 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!swap) {
|
2021-08-18 09:41:21 +08:00
|
|
|
return solve_concave(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, r_sep_axis, margin_A, margin_B);
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2021-08-18 09:41:21 +08:00
|
|
|
return solve_concave(p_shape_B, p_transform_B, p_motion_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, r_sep_axis, margin_A, margin_B);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
} else {
|
2021-08-18 09:41:21 +08:00
|
|
|
return collision_solver(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, r_sep_axis, margin_A, margin_B);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|