1/**************************************************************************/
2/* godot_collision_solver_2d.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "godot_collision_solver_2d.h"
32#include "godot_collision_solver_2d_sat.h"
33
34#define collision_solver sat_2d_calculate_penetration
35//#define collision_solver gjk_epa_calculate_penetration
36
37bool 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) {
38 const GodotWorldBoundaryShape2D *world_boundary = static_cast<const GodotWorldBoundaryShape2D *>(p_shape_A);
39 if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
40 return false;
41 }
42
43 Vector2 n = p_transform_A.basis_xform(world_boundary->get_normal()).normalized();
44 Vector2 p = p_transform_A.xform(world_boundary->get_normal() * world_boundary->get_d());
45 real_t d = n.dot(p);
46
47 Vector2 supports[2];
48 int support_count;
49
50 p_shape_B->get_supports(p_transform_B.affine_inverse().basis_xform(-n).normalized(), supports, support_count);
51
52 bool found = false;
53
54 for (int i = 0; i < support_count; i++) {
55 supports[i] += p_margin * supports[i].normalized();
56 supports[i] = p_transform_B.xform(supports[i]);
57 supports[i] += p_motion_B;
58 real_t pd = n.dot(supports[i]);
59 if (pd >= d) {
60 continue;
61 }
62 found = true;
63
64 Vector2 support_A = supports[i] - n * (pd - d);
65
66 if (p_result_callback) {
67 if (p_swap_result) {
68 p_result_callback(supports[i], support_A, p_userdata);
69 } else {
70 p_result_callback(support_A, supports[i], p_userdata);
71 }
72 }
73 }
74
75 return found;
76}
77
78bool 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) {
79 const GodotSeparationRayShape2D *ray = static_cast<const GodotSeparationRayShape2D *>(p_shape_A);
80 if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
81 return false;
82 }
83
84 Vector2 from = p_transform_A.get_origin();
85 Vector2 to = from + p_transform_A[1] * (ray->get_length() + p_margin);
86 if (p_motion_A != Vector2()) {
87 //not the best but should be enough
88 Vector2 normal = (to - from).normalized();
89 to += normal * MAX(0.0, normal.dot(p_motion_A));
90 }
91 Vector2 support_A = to;
92
93 Transform2D invb = p_transform_B.affine_inverse();
94 from = invb.xform(from);
95 to = invb.xform(to);
96
97 Vector2 p, n;
98 if (!p_shape_B->intersect_segment(from, to, p, n)) {
99 if (r_sep_axis) {
100 *r_sep_axis = p_transform_A[1].normalized();
101 }
102 return false;
103 }
104
105 // Discard contacts when the ray is fully contained inside the shape.
106 if (n == Vector2()) {
107 if (r_sep_axis) {
108 *r_sep_axis = p_transform_A[1].normalized();
109 }
110 return false;
111 }
112
113 // Discard contacts in the wrong direction.
114 if (n.dot(from - to) < CMP_EPSILON) {
115 if (r_sep_axis) {
116 *r_sep_axis = p_transform_A[1].normalized();
117 }
118 return false;
119 }
120
121 Vector2 support_B = p_transform_B.xform(p);
122 if (ray->get_slide_on_slope()) {
123 Vector2 global_n = invb.basis_xform_inv(n).normalized();
124 support_B = support_A + (support_B - support_A).length() * global_n;
125 }
126
127 if (p_result_callback) {
128 if (p_swap_result) {
129 p_result_callback(support_B, support_A, p_userdata);
130 } else {
131 p_result_callback(support_A, support_B, p_userdata);
132 }
133 }
134 return true;
135}
136
137struct _ConcaveCollisionInfo2D {
138 const Transform2D *transform_A = nullptr;
139 const GodotShape2D *shape_A = nullptr;
140 const Transform2D *transform_B = nullptr;
141 Vector2 motion_A;
142 Vector2 motion_B;
143 real_t margin_A = 0.0;
144 real_t margin_B = 0.0;
145 GodotCollisionSolver2D::CallbackResult result_callback = nullptr;
146 void *userdata = nullptr;
147 bool swap_result = false;
148 bool collided = false;
149 int aabb_tests = 0;
150 int collisions = 0;
151 Vector2 *sep_axis = nullptr;
152};
153
154bool GodotCollisionSolver2D::concave_callback(void *p_userdata, GodotShape2D *p_convex) {
155 _ConcaveCollisionInfo2D &cinfo = *(static_cast<_ConcaveCollisionInfo2D *>(p_userdata));
156 cinfo.aabb_tests++;
157
158 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);
159 if (!collided) {
160 return false;
161 }
162
163 cinfo.collided = true;
164 cinfo.collisions++;
165
166 // Stop at first collision if contacts are not needed.
167 return !cinfo.result_callback;
168}
169
170bool 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) {
171 const GodotConcaveShape2D *concave_B = static_cast<const GodotConcaveShape2D *>(p_shape_B);
172
173 _ConcaveCollisionInfo2D cinfo;
174 cinfo.transform_A = &p_transform_A;
175 cinfo.shape_A = p_shape_A;
176 cinfo.transform_B = &p_transform_B;
177 cinfo.motion_A = p_motion_A;
178 cinfo.result_callback = p_result_callback;
179 cinfo.userdata = p_userdata;
180 cinfo.swap_result = p_swap_result;
181 cinfo.collided = false;
182 cinfo.collisions = 0;
183 cinfo.sep_axis = r_sep_axis;
184 cinfo.margin_A = p_margin_A;
185 cinfo.margin_B = p_margin_B;
186
187 cinfo.aabb_tests = 0;
188
189 Transform2D rel_transform = p_transform_A;
190 rel_transform.columns[2] -= p_transform_B.get_origin();
191
192 //quickly compute a local Rect2
193
194 Rect2 local_aabb;
195 for (int i = 0; i < 2; i++) {
196 Vector2 axis(p_transform_B.columns[i]);
197 real_t axis_scale = 1.0 / axis.length();
198 axis *= axis_scale;
199
200 real_t smin = 0.0, smax = 0.0;
201 p_shape_A->project_rangev(axis, rel_transform, smin, smax);
202 smin *= axis_scale;
203 smax *= axis_scale;
204
205 local_aabb.position[i] = smin;
206 local_aabb.size[i] = smax - smin;
207 }
208
209 concave_B->cull(local_aabb, concave_callback, &cinfo);
210
211 return cinfo.collided;
212}
213
214bool 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) {
215 PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();
216 PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();
217 bool concave_A = p_shape_A->is_concave();
218 bool concave_B = p_shape_B->is_concave();
219 real_t margin_A = p_margin_A, margin_B = p_margin_B;
220
221 bool swap = false;
222
223 if (type_A > type_B) {
224 SWAP(type_A, type_B);
225 SWAP(concave_A, concave_B);
226 SWAP(margin_A, margin_B);
227 swap = true;
228 }
229
230 if (type_A == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
231 if (type_B == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
232 WARN_PRINT_ONCE("Collisions between world boundaries are not supported.");
233 return false;
234 }
235
236 if (swap) {
237 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);
238 } else {
239 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);
240 }
241
242 } else if (type_A == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
243 if (type_B == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
244 WARN_PRINT_ONCE("Collisions between two rays are not supported.");
245 return false; //no ray-ray
246 }
247
248 if (swap) {
249 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);
250 } else {
251 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);
252 }
253
254 } else if (concave_B) {
255 if (concave_A) {
256 WARN_PRINT_ONCE("Collisions between two concave shapes are not supported.");
257 return false;
258 }
259
260 if (!swap) {
261 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);
262 } else {
263 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);
264 }
265
266 } else {
267 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);
268 }
269}
270