| 1 | /**************************************************************************/ |
| 2 | /* godot_space_3d.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_space_3d.h" |
| 32 | |
| 33 | #include "godot_collision_solver_3d.h" |
| 34 | #include "godot_physics_server_3d.h" |
| 35 | |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "godot_area_pair_3d.h" |
| 38 | #include "godot_body_pair_3d.h" |
| 39 | |
| 40 | #define TEST_MOTION_MARGIN_MIN_VALUE 0.0001 |
| 41 | #define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05 |
| 42 | |
| 43 | _FORCE_INLINE_ static bool _can_collide_with(GodotCollisionObject3D *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) { |
| 44 | if (!(p_object->get_collision_layer() & p_collision_mask)) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if (p_object->get_type() == GodotCollisionObject3D::TYPE_AREA && !p_collide_with_areas) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (p_object->get_type() == GodotCollisionObject3D::TYPE_BODY && !p_collide_with_bodies) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (p_object->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY && !p_collide_with_bodies) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | int GodotPhysicsDirectSpaceState3D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) { |
| 64 | ERR_FAIL_COND_V(space->locked, false); |
| 65 | int amount = space->broadphase->cull_point(p_parameters.position, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 66 | int cc = 0; |
| 67 | |
| 68 | //Transform3D ai = p_xform.affine_inverse(); |
| 69 | |
| 70 | for (int i = 0; i < amount; i++) { |
| 71 | if (cc >= p_result_max) { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | //area can't be picked by ray (default) |
| 80 | |
| 81 | if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) { |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 86 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 87 | |
| 88 | Transform3D inv_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx); |
| 89 | inv_xform.affine_invert(); |
| 90 | |
| 91 | if (!col_obj->get_shape(shape_idx)->intersect_point(inv_xform.xform(p_parameters.position))) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | r_results[cc].collider_id = col_obj->get_instance_id(); |
| 96 | if (r_results[cc].collider_id.is_valid()) { |
| 97 | r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id); |
| 98 | } else { |
| 99 | r_results[cc].collider = nullptr; |
| 100 | } |
| 101 | r_results[cc].rid = col_obj->get_self(); |
| 102 | r_results[cc].shape = shape_idx; |
| 103 | |
| 104 | cc++; |
| 105 | } |
| 106 | |
| 107 | return cc; |
| 108 | } |
| 109 | |
| 110 | bool GodotPhysicsDirectSpaceState3D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) { |
| 111 | ERR_FAIL_COND_V(space->locked, false); |
| 112 | |
| 113 | Vector3 begin, end; |
| 114 | Vector3 normal; |
| 115 | begin = p_parameters.from; |
| 116 | end = p_parameters.to; |
| 117 | normal = (end - begin).normalized(); |
| 118 | |
| 119 | int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 120 | |
| 121 | //todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision |
| 122 | |
| 123 | bool collided = false; |
| 124 | Vector3 res_point, res_normal; |
| 125 | int res_face_index = -1; |
| 126 | int res_shape = -1; |
| 127 | const GodotCollisionObject3D *res_obj = nullptr; |
| 128 | real_t min_d = 1e10; |
| 129 | |
| 130 | for (int i = 0; i < amount; i++) { |
| 131 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (p_parameters.pick_ray && !(space->intersection_query_results[i]->is_ray_pickable())) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 144 | |
| 145 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 146 | Transform3D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform(); |
| 147 | |
| 148 | Vector3 local_from = inv_xform.xform(begin); |
| 149 | Vector3 local_to = inv_xform.xform(end); |
| 150 | |
| 151 | const GodotShape3D *shape = col_obj->get_shape(shape_idx); |
| 152 | |
| 153 | Vector3 shape_point, shape_normal; |
| 154 | int shape_face_index = -1; |
| 155 | |
| 156 | if (shape->intersect_point(local_from)) { |
| 157 | if (p_parameters.hit_from_inside) { |
| 158 | // Hit shape at starting point. |
| 159 | min_d = 0; |
| 160 | res_point = begin; |
| 161 | res_normal = Vector3(); |
| 162 | res_shape = shape_idx; |
| 163 | res_obj = col_obj; |
| 164 | collided = true; |
| 165 | break; |
| 166 | } else { |
| 167 | // Ignore shape when starting inside. |
| 168 | continue; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal, shape_face_index, p_parameters.hit_back_faces)) { |
| 173 | Transform3D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx); |
| 174 | shape_point = xform.xform(shape_point); |
| 175 | |
| 176 | real_t ld = normal.dot(shape_point); |
| 177 | |
| 178 | if (ld < min_d) { |
| 179 | min_d = ld; |
| 180 | res_point = shape_point; |
| 181 | res_normal = inv_xform.basis.xform_inv(shape_normal).normalized(); |
| 182 | res_face_index = shape_face_index; |
| 183 | res_shape = shape_idx; |
| 184 | res_obj = col_obj; |
| 185 | collided = true; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (!collided) { |
| 191 | return false; |
| 192 | } |
| 193 | ERR_FAIL_NULL_V(res_obj, false); // Shouldn't happen but silences warning. |
| 194 | |
| 195 | r_result.collider_id = res_obj->get_instance_id(); |
| 196 | if (r_result.collider_id.is_valid()) { |
| 197 | r_result.collider = ObjectDB::get_instance(r_result.collider_id); |
| 198 | } else { |
| 199 | r_result.collider = nullptr; |
| 200 | } |
| 201 | r_result.normal = res_normal; |
| 202 | r_result.face_index = res_face_index; |
| 203 | r_result.position = res_point; |
| 204 | r_result.rid = res_obj->get_self(); |
| 205 | r_result.shape = res_shape; |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | int GodotPhysicsDirectSpaceState3D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) { |
| 211 | if (p_result_max <= 0) { |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid); |
| 216 | ERR_FAIL_NULL_V(shape, 0); |
| 217 | |
| 218 | AABB aabb = p_parameters.transform.xform(shape->get_aabb()); |
| 219 | |
| 220 | int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 221 | |
| 222 | int cc = 0; |
| 223 | |
| 224 | //Transform3D ai = p_xform.affine_inverse(); |
| 225 | |
| 226 | for (int i = 0; i < amount; i++) { |
| 227 | if (cc >= p_result_max) { |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | //area can't be picked by ray (default) |
| 236 | |
| 237 | if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) { |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 242 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 243 | |
| 244 | if (!GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), nullptr, nullptr, nullptr, p_parameters.margin, 0)) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | if (r_results) { |
| 249 | r_results[cc].collider_id = col_obj->get_instance_id(); |
| 250 | if (r_results[cc].collider_id.is_valid()) { |
| 251 | r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id); |
| 252 | } else { |
| 253 | r_results[cc].collider = nullptr; |
| 254 | } |
| 255 | r_results[cc].rid = col_obj->get_self(); |
| 256 | r_results[cc].shape = shape_idx; |
| 257 | } |
| 258 | |
| 259 | cc++; |
| 260 | } |
| 261 | |
| 262 | return cc; |
| 263 | } |
| 264 | |
| 265 | bool GodotPhysicsDirectSpaceState3D::cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info) { |
| 266 | GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid); |
| 267 | ERR_FAIL_NULL_V(shape, false); |
| 268 | |
| 269 | AABB aabb = p_parameters.transform.xform(shape->get_aabb()); |
| 270 | aabb = aabb.merge(AABB(aabb.position + p_parameters.motion, aabb.size)); //motion |
| 271 | aabb = aabb.grow(p_parameters.margin); |
| 272 | |
| 273 | int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 274 | |
| 275 | real_t best_safe = 1; |
| 276 | real_t best_unsafe = 1; |
| 277 | |
| 278 | Transform3D xform_inv = p_parameters.transform.affine_inverse(); |
| 279 | GodotMotionShape3D mshape; |
| 280 | mshape.shape = shape; |
| 281 | mshape.motion = xform_inv.basis.xform(p_parameters.motion); |
| 282 | |
| 283 | bool best_first = true; |
| 284 | |
| 285 | Vector3 motion_normal = p_parameters.motion.normalized(); |
| 286 | |
| 287 | Vector3 closest_A, closest_B; |
| 288 | |
| 289 | for (int i = 0; i < amount; i++) { |
| 290 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) { |
| 295 | continue; //ignore excluded |
| 296 | } |
| 297 | |
| 298 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 299 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 300 | |
| 301 | Vector3 point_A, point_B; |
| 302 | Vector3 sep_axis = motion_normal; |
| 303 | |
| 304 | Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx); |
| 305 | //test initial overlap, does it collide if going all the way? |
| 306 | if (GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | //test initial overlap, ignore objects it's inside of. |
| 311 | sep_axis = motion_normal; |
| 312 | |
| 313 | if (!GodotCollisionSolver3D::solve_distance(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) { |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | //just do kinematic solving |
| 318 | real_t low = 0.0; |
| 319 | real_t hi = 1.0; |
| 320 | real_t fraction_coeff = 0.5; |
| 321 | for (int j = 0; j < 8; j++) { //steps should be customizable.. |
| 322 | real_t fraction = low + (hi - low) * fraction_coeff; |
| 323 | |
| 324 | mshape.motion = xform_inv.basis.xform(p_parameters.motion * fraction); |
| 325 | |
| 326 | Vector3 lA, lB; |
| 327 | Vector3 sep = motion_normal; //important optimization for this to work fast enough |
| 328 | bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep); |
| 329 | |
| 330 | if (collided) { |
| 331 | hi = fraction; |
| 332 | if ((j == 0) || (low > 0.0)) { // Did it not collide before? |
| 333 | // When alternating or first iteration, use dichotomy. |
| 334 | fraction_coeff = 0.5; |
| 335 | } else { |
| 336 | // When colliding again, converge faster towards low fraction |
| 337 | // for more accurate results with long motions that collide near the start. |
| 338 | fraction_coeff = 0.25; |
| 339 | } |
| 340 | } else { |
| 341 | point_A = lA; |
| 342 | point_B = lB; |
| 343 | low = fraction; |
| 344 | if ((j == 0) || (hi < 1.0)) { // Did it collide before? |
| 345 | // When alternating or first iteration, use dichotomy. |
| 346 | fraction_coeff = 0.5; |
| 347 | } else { |
| 348 | // When not colliding again, converge faster towards high fraction |
| 349 | // for more accurate results with long motions that collide near the end. |
| 350 | fraction_coeff = 0.75; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (low < best_safe) { |
| 356 | best_first = true; //force reset |
| 357 | best_safe = low; |
| 358 | best_unsafe = hi; |
| 359 | } |
| 360 | |
| 361 | if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) { |
| 362 | closest_A = point_A; |
| 363 | closest_B = point_B; |
| 364 | r_info->collider_id = col_obj->get_instance_id(); |
| 365 | r_info->rid = col_obj->get_self(); |
| 366 | r_info->shape = shape_idx; |
| 367 | r_info->point = closest_B; |
| 368 | r_info->normal = (closest_A - closest_B).normalized(); |
| 369 | best_first = false; |
| 370 | if (col_obj->get_type() == GodotCollisionObject3D::TYPE_BODY) { |
| 371 | const GodotBody3D *body = static_cast<const GodotBody3D *>(col_obj); |
| 372 | Vector3 rel_vec = closest_B - (body->get_transform().origin + body->get_center_of_mass()); |
| 373 | r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | p_closest_safe = best_safe; |
| 379 | p_closest_unsafe = best_unsafe; |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | bool GodotPhysicsDirectSpaceState3D::collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) { |
| 385 | if (p_result_max <= 0) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid); |
| 390 | ERR_FAIL_NULL_V(shape, false); |
| 391 | |
| 392 | AABB aabb = p_parameters.transform.xform(shape->get_aabb()); |
| 393 | aabb = aabb.grow(p_parameters.margin); |
| 394 | |
| 395 | int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 396 | |
| 397 | bool collided = false; |
| 398 | r_result_count = 0; |
| 399 | |
| 400 | GodotPhysicsServer3D::CollCbkData cbk; |
| 401 | cbk.max = p_result_max; |
| 402 | cbk.amount = 0; |
| 403 | cbk.ptr = r_results; |
| 404 | GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk; |
| 405 | |
| 406 | GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk; |
| 407 | |
| 408 | for (int i = 0; i < amount; i++) { |
| 409 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 414 | |
| 415 | if (p_parameters.exclude.has(col_obj->get_self())) { |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 420 | |
| 421 | if (GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_parameters.margin)) { |
| 422 | collided = true; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | r_result_count = cbk.amount; |
| 427 | |
| 428 | return collided; |
| 429 | } |
| 430 | |
| 431 | struct _RestResultData { |
| 432 | const GodotCollisionObject3D *object = nullptr; |
| 433 | int local_shape = 0; |
| 434 | int shape = 0; |
| 435 | Vector3 contact; |
| 436 | Vector3 normal; |
| 437 | real_t len = 0.0; |
| 438 | }; |
| 439 | |
| 440 | struct _RestCallbackData { |
| 441 | const GodotCollisionObject3D *object = nullptr; |
| 442 | int local_shape = 0; |
| 443 | int shape = 0; |
| 444 | |
| 445 | real_t min_allowed_depth = 0.0; |
| 446 | |
| 447 | _RestResultData best_result; |
| 448 | |
| 449 | int max_results = 0; |
| 450 | int result_count = 0; |
| 451 | _RestResultData *other_results = nullptr; |
| 452 | }; |
| 453 | |
| 454 | static void _rest_cbk_result(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) { |
| 455 | _RestCallbackData *rd = static_cast<_RestCallbackData *>(p_userdata); |
| 456 | |
| 457 | Vector3 contact_rel = p_point_B - p_point_A; |
| 458 | real_t len = contact_rel.length(); |
| 459 | if (len < rd->min_allowed_depth) { |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | bool is_best_result = (len > rd->best_result.len); |
| 464 | |
| 465 | if (rd->other_results && rd->result_count > 0) { |
| 466 | // Consider as new result by default. |
| 467 | int prev_result_count = rd->result_count++; |
| 468 | |
| 469 | int result_index = 0; |
| 470 | real_t tested_len = is_best_result ? rd->best_result.len : len; |
| 471 | for (; result_index < prev_result_count - 1; ++result_index) { |
| 472 | if (tested_len > rd->other_results[result_index].len) { |
| 473 | // Reusing a previous result. |
| 474 | rd->result_count--; |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (result_index < rd->max_results - 1) { |
| 480 | _RestResultData &result = rd->other_results[result_index]; |
| 481 | |
| 482 | if (is_best_result) { |
| 483 | // Keep the previous best result as separate result. |
| 484 | result = rd->best_result; |
| 485 | } else { |
| 486 | // Keep this result as separate result. |
| 487 | result.len = len; |
| 488 | result.contact = p_point_B; |
| 489 | result.normal = normal; |
| 490 | result.object = rd->object; |
| 491 | result.shape = rd->shape; |
| 492 | result.local_shape = rd->local_shape; |
| 493 | } |
| 494 | } else { |
| 495 | // Discarding this result. |
| 496 | rd->result_count--; |
| 497 | } |
| 498 | } else if (is_best_result) { |
| 499 | rd->result_count = 1; |
| 500 | } |
| 501 | |
| 502 | if (!is_best_result) { |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | rd->best_result.len = len; |
| 507 | rd->best_result.contact = p_point_B; |
| 508 | rd->best_result.normal = normal; |
| 509 | rd->best_result.object = rd->object; |
| 510 | rd->best_result.shape = rd->shape; |
| 511 | rd->best_result.local_shape = rd->local_shape; |
| 512 | } |
| 513 | |
| 514 | bool GodotPhysicsDirectSpaceState3D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) { |
| 515 | GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid); |
| 516 | ERR_FAIL_NULL_V(shape, false); |
| 517 | |
| 518 | real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE); |
| 519 | |
| 520 | AABB aabb = p_parameters.transform.xform(shape->get_aabb()); |
| 521 | aabb = aabb.grow(margin); |
| 522 | |
| 523 | int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results); |
| 524 | |
| 525 | _RestCallbackData rcd; |
| 526 | |
| 527 | // Allowed depth can't be lower than motion length, in order to handle contacts at low speed. |
| 528 | real_t motion_length = p_parameters.motion.length(); |
| 529 | real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR; |
| 530 | rcd.min_allowed_depth = MIN(motion_length, min_contact_depth); |
| 531 | |
| 532 | for (int i = 0; i < amount; i++) { |
| 533 | if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) { |
| 534 | continue; |
| 535 | } |
| 536 | |
| 537 | const GodotCollisionObject3D *col_obj = space->intersection_query_results[i]; |
| 538 | |
| 539 | if (p_parameters.exclude.has(col_obj->get_self())) { |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | int shape_idx = space->intersection_query_subindex_results[i]; |
| 544 | |
| 545 | rcd.object = col_obj; |
| 546 | rcd.shape = shape_idx; |
| 547 | bool sc = GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin); |
| 548 | if (!sc) { |
| 549 | continue; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (rcd.best_result.len == 0 || !rcd.best_result.object) { |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | r_info->collider_id = rcd.best_result.object->get_instance_id(); |
| 558 | r_info->shape = rcd.best_result.shape; |
| 559 | r_info->normal = rcd.best_result.normal; |
| 560 | r_info->point = rcd.best_result.contact; |
| 561 | r_info->rid = rcd.best_result.object->get_self(); |
| 562 | if (rcd.best_result.object->get_type() == GodotCollisionObject3D::TYPE_BODY) { |
| 563 | const GodotBody3D *body = static_cast<const GodotBody3D *>(rcd.best_result.object); |
| 564 | Vector3 rel_vec = rcd.best_result.contact - (body->get_transform().origin + body->get_center_of_mass()); |
| 565 | r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec); |
| 566 | |
| 567 | } else { |
| 568 | r_info->linear_velocity = Vector3(); |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | Vector3 GodotPhysicsDirectSpaceState3D::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const { |
| 575 | GodotCollisionObject3D *obj = GodotPhysicsServer3D::godot_singleton->area_owner.get_or_null(p_object); |
| 576 | if (!obj) { |
| 577 | obj = GodotPhysicsServer3D::godot_singleton->body_owner.get_or_null(p_object); |
| 578 | } |
| 579 | ERR_FAIL_NULL_V(obj, Vector3()); |
| 580 | |
| 581 | ERR_FAIL_COND_V(obj->get_space() != space, Vector3()); |
| 582 | |
| 583 | real_t min_distance = 1e20; |
| 584 | Vector3 min_point; |
| 585 | |
| 586 | bool shapes_found = false; |
| 587 | |
| 588 | for (int i = 0; i < obj->get_shape_count(); i++) { |
| 589 | if (obj->is_shape_disabled(i)) { |
| 590 | continue; |
| 591 | } |
| 592 | |
| 593 | Transform3D shape_xform = obj->get_transform() * obj->get_shape_transform(i); |
| 594 | GodotShape3D *shape = obj->get_shape(i); |
| 595 | |
| 596 | Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point)); |
| 597 | point = shape_xform.xform(point); |
| 598 | |
| 599 | real_t dist = point.distance_to(p_point); |
| 600 | if (dist < min_distance) { |
| 601 | min_distance = dist; |
| 602 | min_point = point; |
| 603 | } |
| 604 | shapes_found = true; |
| 605 | } |
| 606 | |
| 607 | if (!shapes_found) { |
| 608 | return obj->get_transform().origin; //no shapes found, use distance to origin. |
| 609 | } else { |
| 610 | return min_point; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | GodotPhysicsDirectSpaceState3D::GodotPhysicsDirectSpaceState3D() { |
| 615 | space = nullptr; |
| 616 | } |
| 617 | |
| 618 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 619 | |
| 620 | int GodotSpace3D::_cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb) { |
| 621 | int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results); |
| 622 | |
| 623 | for (int i = 0; i < amount; i++) { |
| 624 | bool keep = true; |
| 625 | |
| 626 | if (intersection_query_results[i] == p_body) { |
| 627 | keep = false; |
| 628 | } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_AREA) { |
| 629 | keep = false; |
| 630 | } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY) { |
| 631 | keep = false; |
| 632 | } else if (!p_body->collides_with(static_cast<GodotBody3D *>(intersection_query_results[i]))) { |
| 633 | keep = false; |
| 634 | } else if (static_cast<GodotBody3D *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) { |
| 635 | keep = false; |
| 636 | } |
| 637 | |
| 638 | if (!keep) { |
| 639 | if (i < amount - 1) { |
| 640 | SWAP(intersection_query_results[i], intersection_query_results[amount - 1]); |
| 641 | SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]); |
| 642 | } |
| 643 | |
| 644 | amount--; |
| 645 | i--; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return amount; |
| 650 | } |
| 651 | |
| 652 | bool GodotSpace3D::test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result) { |
| 653 | //give me back regular physics engine logic |
| 654 | //this is madness |
| 655 | //and most people using this function will think |
| 656 | //what it does is simpler than using physics |
| 657 | //this took about a week to get right.. |
| 658 | //but is it right? who knows at this point.. |
| 659 | |
| 660 | ERR_FAIL_COND_V(p_parameters.max_collisions < 0 || p_parameters.max_collisions > PhysicsServer3D::MotionResult::MAX_COLLISIONS, false); |
| 661 | |
| 662 | if (r_result) { |
| 663 | *r_result = PhysicsServer3D::MotionResult(); |
| 664 | } |
| 665 | |
| 666 | AABB body_aabb; |
| 667 | bool shapes_found = false; |
| 668 | |
| 669 | for (int i = 0; i < p_body->get_shape_count(); i++) { |
| 670 | if (p_body->is_shape_disabled(i)) { |
| 671 | continue; |
| 672 | } |
| 673 | |
| 674 | if (!shapes_found) { |
| 675 | body_aabb = p_body->get_shape_aabb(i); |
| 676 | shapes_found = true; |
| 677 | } else { |
| 678 | body_aabb = body_aabb.merge(p_body->get_shape_aabb(i)); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (!shapes_found) { |
| 683 | if (r_result) { |
| 684 | r_result->travel = p_parameters.motion; |
| 685 | } |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE); |
| 691 | |
| 692 | // Undo the currently transform the physics server is aware of and apply the provided one |
| 693 | body_aabb = p_parameters.from.xform(p_body->get_inv_transform().xform(body_aabb)); |
| 694 | body_aabb = body_aabb.grow(margin); |
| 695 | |
| 696 | real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR; |
| 697 | |
| 698 | real_t motion_length = p_parameters.motion.length(); |
| 699 | Vector3 motion_normal = p_parameters.motion / motion_length; |
| 700 | |
| 701 | Transform3D body_transform = p_parameters.from; |
| 702 | |
| 703 | bool recovered = false; |
| 704 | |
| 705 | { |
| 706 | //STEP 1, FREE BODY IF STUCK |
| 707 | |
| 708 | const int max_results = 32; |
| 709 | int recover_attempts = 4; |
| 710 | Vector3 sr[max_results * 2]; |
| 711 | real_t priorities[max_results]; |
| 712 | |
| 713 | do { |
| 714 | GodotPhysicsServer3D::CollCbkData cbk; |
| 715 | cbk.max = max_results; |
| 716 | cbk.amount = 0; |
| 717 | cbk.ptr = sr; |
| 718 | |
| 719 | GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk; |
| 720 | GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk; |
| 721 | int priority_amount = 0; |
| 722 | |
| 723 | bool collided = false; |
| 724 | |
| 725 | int amount = _cull_aabb_for_body(p_body, body_aabb); |
| 726 | |
| 727 | for (int j = 0; j < p_body->get_shape_count(); j++) { |
| 728 | if (p_body->is_shape_disabled(j)) { |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j); |
| 733 | GodotShape3D *body_shape = p_body->get_shape(j); |
| 734 | |
| 735 | for (int i = 0; i < amount; i++) { |
| 736 | const GodotCollisionObject3D *col_obj = intersection_query_results[i]; |
| 737 | if (p_parameters.exclude_bodies.has(col_obj->get_self())) { |
| 738 | continue; |
| 739 | } |
| 740 | if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) { |
| 741 | continue; |
| 742 | } |
| 743 | |
| 744 | int shape_idx = intersection_query_subindex_results[i]; |
| 745 | |
| 746 | if (GodotCollisionSolver3D::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, margin)) { |
| 747 | collided = cbk.amount > 0; |
| 748 | } |
| 749 | while (cbk.amount > priority_amount) { |
| 750 | priorities[priority_amount] = col_obj->get_collision_priority(); |
| 751 | priority_amount++; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | if (!collided) { |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | real_t inv_total_weight = 0.0; |
| 761 | for (int i = 0; i < cbk.amount; i++) { |
| 762 | inv_total_weight += priorities[i]; |
| 763 | } |
| 764 | inv_total_weight = Math::is_zero_approx(inv_total_weight) ? 1.0 : (real_t)cbk.amount / inv_total_weight; |
| 765 | |
| 766 | recovered = true; |
| 767 | |
| 768 | Vector3 recover_motion; |
| 769 | for (int i = 0; i < cbk.amount; i++) { |
| 770 | Vector3 a = sr[i * 2 + 0]; |
| 771 | Vector3 b = sr[i * 2 + 1]; |
| 772 | |
| 773 | // Compute plane on b towards a. |
| 774 | Vector3 n = (a - b).normalized(); |
| 775 | real_t d = n.dot(b); |
| 776 | |
| 777 | // Compute depth on recovered motion. |
| 778 | real_t depth = n.dot(a + recover_motion) - d; |
| 779 | if (depth > min_contact_depth + CMP_EPSILON) { |
| 780 | // Only recover if there is penetration. |
| 781 | recover_motion -= n * (depth - min_contact_depth) * 0.4 * priorities[i] * inv_total_weight; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (recover_motion == Vector3()) { |
| 786 | collided = false; |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | body_transform.origin += recover_motion; |
| 791 | body_aabb.position += recover_motion; |
| 792 | |
| 793 | recover_attempts--; |
| 794 | |
| 795 | } while (recover_attempts); |
| 796 | } |
| 797 | |
| 798 | real_t safe = 1.0; |
| 799 | real_t unsafe = 1.0; |
| 800 | int best_shape = -1; |
| 801 | |
| 802 | { |
| 803 | // STEP 2 ATTEMPT MOTION |
| 804 | |
| 805 | AABB motion_aabb = body_aabb; |
| 806 | motion_aabb.position += p_parameters.motion; |
| 807 | motion_aabb = motion_aabb.merge(body_aabb); |
| 808 | |
| 809 | int amount = _cull_aabb_for_body(p_body, motion_aabb); |
| 810 | |
| 811 | for (int j = 0; j < p_body->get_shape_count(); j++) { |
| 812 | if (p_body->is_shape_disabled(j)) { |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | GodotShape3D *body_shape = p_body->get_shape(j); |
| 817 | |
| 818 | // Colliding separation rays allows to properly snap to the ground, |
| 819 | // otherwise it's not needed in regular motion. |
| 820 | if (!p_parameters.collide_separation_ray && (body_shape->get_type() == PhysicsServer3D::SHAPE_SEPARATION_RAY)) { |
| 821 | // When slide on slope is on, separation ray shape acts like a regular shape. |
| 822 | if (!static_cast<GodotSeparationRayShape3D *>(body_shape)->get_slide_on_slope()) { |
| 823 | continue; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j); |
| 828 | |
| 829 | Transform3D body_shape_xform_inv = body_shape_xform.affine_inverse(); |
| 830 | GodotMotionShape3D mshape; |
| 831 | mshape.shape = body_shape; |
| 832 | mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion); |
| 833 | |
| 834 | bool stuck = false; |
| 835 | |
| 836 | real_t best_safe = 1; |
| 837 | real_t best_unsafe = 1; |
| 838 | |
| 839 | for (int i = 0; i < amount; i++) { |
| 840 | const GodotCollisionObject3D *col_obj = intersection_query_results[i]; |
| 841 | if (p_parameters.exclude_bodies.has(col_obj->get_self())) { |
| 842 | continue; |
| 843 | } |
| 844 | if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) { |
| 845 | continue; |
| 846 | } |
| 847 | |
| 848 | int shape_idx = intersection_query_subindex_results[i]; |
| 849 | |
| 850 | //test initial overlap, does it collide if going all the way? |
| 851 | Vector3 point_A, point_B; |
| 852 | Vector3 sep_axis = motion_normal; |
| 853 | |
| 854 | Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx); |
| 855 | //test initial overlap, does it collide if going all the way? |
| 856 | if (GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) { |
| 857 | continue; |
| 858 | } |
| 859 | sep_axis = motion_normal; |
| 860 | |
| 861 | if (!GodotCollisionSolver3D::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) { |
| 862 | stuck = true; |
| 863 | break; |
| 864 | } |
| 865 | |
| 866 | //just do kinematic solving |
| 867 | real_t low = 0.0; |
| 868 | real_t hi = 1.0; |
| 869 | real_t fraction_coeff = 0.5; |
| 870 | for (int k = 0; k < 8; k++) { //steps should be customizable.. |
| 871 | real_t fraction = low + (hi - low) * fraction_coeff; |
| 872 | |
| 873 | mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion * fraction); |
| 874 | |
| 875 | Vector3 lA, lB; |
| 876 | Vector3 sep = motion_normal; //important optimization for this to work fast enough |
| 877 | bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep); |
| 878 | |
| 879 | if (collided) { |
| 880 | hi = fraction; |
| 881 | if ((k == 0) || (low > 0.0)) { // Did it not collide before? |
| 882 | // When alternating or first iteration, use dichotomy. |
| 883 | fraction_coeff = 0.5; |
| 884 | } else { |
| 885 | // When colliding again, converge faster towards low fraction |
| 886 | // for more accurate results with long motions that collide near the start. |
| 887 | fraction_coeff = 0.25; |
| 888 | } |
| 889 | } else { |
| 890 | point_A = lA; |
| 891 | point_B = lB; |
| 892 | low = fraction; |
| 893 | if ((k == 0) || (hi < 1.0)) { // Did it collide before? |
| 894 | // When alternating or first iteration, use dichotomy. |
| 895 | fraction_coeff = 0.5; |
| 896 | } else { |
| 897 | // When not colliding again, converge faster towards high fraction |
| 898 | // for more accurate results with long motions that collide near the end. |
| 899 | fraction_coeff = 0.75; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | if (low < best_safe) { |
| 905 | best_safe = low; |
| 906 | best_unsafe = hi; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (stuck) { |
| 911 | safe = 0; |
| 912 | unsafe = 0; |
| 913 | best_shape = j; //sadly it's the best |
| 914 | break; |
| 915 | } |
| 916 | if (best_safe == 1.0) { |
| 917 | continue; |
| 918 | } |
| 919 | if (best_safe < safe) { |
| 920 | safe = best_safe; |
| 921 | unsafe = best_unsafe; |
| 922 | best_shape = j; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | bool collided = false; |
| 928 | if ((p_parameters.recovery_as_collision && recovered) || (safe < 1)) { |
| 929 | if (safe >= 1) { |
| 930 | best_shape = -1; //no best shape with cast, reset to -1 |
| 931 | } |
| 932 | |
| 933 | //it collided, let's get the rest info in unsafe advance |
| 934 | Transform3D ugt = body_transform; |
| 935 | ugt.origin += p_parameters.motion * unsafe; |
| 936 | |
| 937 | _RestResultData results[PhysicsServer3D::MotionResult::MAX_COLLISIONS]; |
| 938 | |
| 939 | _RestCallbackData rcd; |
| 940 | if (p_parameters.max_collisions > 1) { |
| 941 | rcd.max_results = p_parameters.max_collisions; |
| 942 | rcd.other_results = results; |
| 943 | } |
| 944 | |
| 945 | // Allowed depth can't be lower than motion length, in order to handle contacts at low speed. |
| 946 | rcd.min_allowed_depth = MIN(motion_length, min_contact_depth); |
| 947 | |
| 948 | body_aabb.position += p_parameters.motion * unsafe; |
| 949 | int amount = _cull_aabb_for_body(p_body, body_aabb); |
| 950 | |
| 951 | int from_shape = best_shape != -1 ? best_shape : 0; |
| 952 | int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count(); |
| 953 | |
| 954 | for (int j = from_shape; j < to_shape; j++) { |
| 955 | if (p_body->is_shape_disabled(j)) { |
| 956 | continue; |
| 957 | } |
| 958 | |
| 959 | Transform3D body_shape_xform = ugt * p_body->get_shape_transform(j); |
| 960 | GodotShape3D *body_shape = p_body->get_shape(j); |
| 961 | |
| 962 | for (int i = 0; i < amount; i++) { |
| 963 | const GodotCollisionObject3D *col_obj = intersection_query_results[i]; |
| 964 | if (p_parameters.exclude_bodies.has(col_obj->get_self())) { |
| 965 | continue; |
| 966 | } |
| 967 | if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) { |
| 968 | continue; |
| 969 | } |
| 970 | |
| 971 | int shape_idx = intersection_query_subindex_results[i]; |
| 972 | |
| 973 | rcd.object = col_obj; |
| 974 | rcd.shape = shape_idx; |
| 975 | rcd.local_shape = j; |
| 976 | bool sc = GodotCollisionSolver3D::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin); |
| 977 | if (!sc) { |
| 978 | continue; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (rcd.result_count > 0) { |
| 984 | if (r_result) { |
| 985 | for (int collision_index = 0; collision_index < rcd.result_count; ++collision_index) { |
| 986 | const _RestResultData &result = (collision_index > 0) ? rcd.other_results[collision_index - 1] : rcd.best_result; |
| 987 | |
| 988 | PhysicsServer3D::MotionCollision &collision = r_result->collisions[collision_index]; |
| 989 | |
| 990 | collision.collider = result.object->get_self(); |
| 991 | collision.collider_id = result.object->get_instance_id(); |
| 992 | collision.collider_shape = result.shape; |
| 993 | collision.local_shape = result.local_shape; |
| 994 | collision.normal = result.normal; |
| 995 | collision.position = result.contact; |
| 996 | collision.depth = result.len; |
| 997 | |
| 998 | const GodotBody3D *body = static_cast<const GodotBody3D *>(result.object); |
| 999 | |
| 1000 | Vector3 rel_vec = result.contact - (body->get_transform().origin + body->get_center_of_mass()); |
| 1001 | collision.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec); |
| 1002 | collision.collider_angular_velocity = body->get_angular_velocity(); |
| 1003 | } |
| 1004 | |
| 1005 | r_result->travel = safe * p_parameters.motion; |
| 1006 | r_result->remainder = p_parameters.motion - safe * p_parameters.motion; |
| 1007 | r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin()); |
| 1008 | |
| 1009 | r_result->collision_safe_fraction = safe; |
| 1010 | r_result->collision_unsafe_fraction = unsafe; |
| 1011 | |
| 1012 | r_result->collision_count = rcd.result_count; |
| 1013 | r_result->collision_depth = rcd.best_result.len; |
| 1014 | } |
| 1015 | |
| 1016 | collided = true; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | if (!collided && r_result) { |
| 1021 | r_result->travel = p_parameters.motion; |
| 1022 | r_result->remainder = Vector3(); |
| 1023 | r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin()); |
| 1024 | |
| 1025 | r_result->collision_safe_fraction = 1.0; |
| 1026 | r_result->collision_unsafe_fraction = 1.0; |
| 1027 | r_result->collision_depth = 0.0; |
| 1028 | } |
| 1029 | |
| 1030 | return collided; |
| 1031 | } |
| 1032 | |
| 1033 | // Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree. |
| 1034 | void *GodotSpace3D::_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self) { |
| 1035 | GodotCollisionObject3D::Type type_A = A->get_type(); |
| 1036 | GodotCollisionObject3D::Type type_B = B->get_type(); |
| 1037 | if (type_A > type_B) { |
| 1038 | SWAP(A, B); |
| 1039 | SWAP(p_subindex_A, p_subindex_B); |
| 1040 | SWAP(type_A, type_B); |
| 1041 | } |
| 1042 | |
| 1043 | GodotSpace3D *self = static_cast<GodotSpace3D *>(p_self); |
| 1044 | |
| 1045 | self->collision_pairs++; |
| 1046 | |
| 1047 | if (type_A == GodotCollisionObject3D::TYPE_AREA) { |
| 1048 | GodotArea3D *area = static_cast<GodotArea3D *>(A); |
| 1049 | if (type_B == GodotCollisionObject3D::TYPE_AREA) { |
| 1050 | GodotArea3D *area_b = static_cast<GodotArea3D *>(B); |
| 1051 | GodotArea2Pair3D *area2_pair = memnew(GodotArea2Pair3D(area_b, p_subindex_B, area, p_subindex_A)); |
| 1052 | return area2_pair; |
| 1053 | } else if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) { |
| 1054 | GodotSoftBody3D *softbody = static_cast<GodotSoftBody3D *>(B); |
| 1055 | GodotAreaSoftBodyPair3D *soft_area_pair = memnew(GodotAreaSoftBodyPair3D(softbody, p_subindex_B, area, p_subindex_A)); |
| 1056 | return soft_area_pair; |
| 1057 | } else { |
| 1058 | GodotBody3D *body = static_cast<GodotBody3D *>(B); |
| 1059 | GodotAreaPair3D *area_pair = memnew(GodotAreaPair3D(body, p_subindex_B, area, p_subindex_A)); |
| 1060 | return area_pair; |
| 1061 | } |
| 1062 | } else if (type_A == GodotCollisionObject3D::TYPE_BODY) { |
| 1063 | if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) { |
| 1064 | GodotBodySoftBodyPair3D *soft_pair = memnew(GodotBodySoftBodyPair3D(static_cast<GodotBody3D *>(A), p_subindex_A, static_cast<GodotSoftBody3D *>(B))); |
| 1065 | return soft_pair; |
| 1066 | } else { |
| 1067 | GodotBodyPair3D *b = memnew(GodotBodyPair3D(static_cast<GodotBody3D *>(A), p_subindex_A, static_cast<GodotBody3D *>(B), p_subindex_B)); |
| 1068 | return b; |
| 1069 | } |
| 1070 | } else { |
| 1071 | // Soft Body/Soft Body, not supported. |
| 1072 | } |
| 1073 | |
| 1074 | return nullptr; |
| 1075 | } |
| 1076 | |
| 1077 | void GodotSpace3D::_broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self) { |
| 1078 | if (!p_data) { |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | GodotSpace3D *self = static_cast<GodotSpace3D *>(p_self); |
| 1083 | self->collision_pairs--; |
| 1084 | GodotConstraint3D *c = static_cast<GodotConstraint3D *>(p_data); |
| 1085 | memdelete(c); |
| 1086 | } |
| 1087 | |
| 1088 | const SelfList<GodotBody3D>::List &GodotSpace3D::get_active_body_list() const { |
| 1089 | return active_list; |
| 1090 | } |
| 1091 | |
| 1092 | void GodotSpace3D::body_add_to_active_list(SelfList<GodotBody3D> *p_body) { |
| 1093 | active_list.add(p_body); |
| 1094 | } |
| 1095 | |
| 1096 | void GodotSpace3D::body_remove_from_active_list(SelfList<GodotBody3D> *p_body) { |
| 1097 | active_list.remove(p_body); |
| 1098 | } |
| 1099 | |
| 1100 | void GodotSpace3D::body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body) { |
| 1101 | mass_properties_update_list.add(p_body); |
| 1102 | } |
| 1103 | |
| 1104 | void GodotSpace3D::body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body) { |
| 1105 | mass_properties_update_list.remove(p_body); |
| 1106 | } |
| 1107 | |
| 1108 | GodotBroadPhase3D *GodotSpace3D::get_broadphase() { |
| 1109 | return broadphase; |
| 1110 | } |
| 1111 | |
| 1112 | void GodotSpace3D::add_object(GodotCollisionObject3D *p_object) { |
| 1113 | ERR_FAIL_COND(objects.has(p_object)); |
| 1114 | objects.insert(p_object); |
| 1115 | } |
| 1116 | |
| 1117 | void GodotSpace3D::remove_object(GodotCollisionObject3D *p_object) { |
| 1118 | ERR_FAIL_COND(!objects.has(p_object)); |
| 1119 | objects.erase(p_object); |
| 1120 | } |
| 1121 | |
| 1122 | const HashSet<GodotCollisionObject3D *> &GodotSpace3D::get_objects() const { |
| 1123 | return objects; |
| 1124 | } |
| 1125 | |
| 1126 | void GodotSpace3D::body_add_to_state_query_list(SelfList<GodotBody3D> *p_body) { |
| 1127 | state_query_list.add(p_body); |
| 1128 | } |
| 1129 | |
| 1130 | void GodotSpace3D::body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body) { |
| 1131 | state_query_list.remove(p_body); |
| 1132 | } |
| 1133 | |
| 1134 | void GodotSpace3D::area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area) { |
| 1135 | monitor_query_list.add(p_area); |
| 1136 | } |
| 1137 | |
| 1138 | void GodotSpace3D::area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area) { |
| 1139 | monitor_query_list.remove(p_area); |
| 1140 | } |
| 1141 | |
| 1142 | void GodotSpace3D::area_add_to_moved_list(SelfList<GodotArea3D> *p_area) { |
| 1143 | area_moved_list.add(p_area); |
| 1144 | } |
| 1145 | |
| 1146 | void GodotSpace3D::area_remove_from_moved_list(SelfList<GodotArea3D> *p_area) { |
| 1147 | area_moved_list.remove(p_area); |
| 1148 | } |
| 1149 | |
| 1150 | const SelfList<GodotArea3D>::List &GodotSpace3D::get_moved_area_list() const { |
| 1151 | return area_moved_list; |
| 1152 | } |
| 1153 | |
| 1154 | const SelfList<GodotSoftBody3D>::List &GodotSpace3D::get_active_soft_body_list() const { |
| 1155 | return active_soft_body_list; |
| 1156 | } |
| 1157 | |
| 1158 | void GodotSpace3D::soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body) { |
| 1159 | active_soft_body_list.add(p_soft_body); |
| 1160 | } |
| 1161 | |
| 1162 | void GodotSpace3D::soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body) { |
| 1163 | active_soft_body_list.remove(p_soft_body); |
| 1164 | } |
| 1165 | |
| 1166 | void GodotSpace3D::call_queries() { |
| 1167 | while (state_query_list.first()) { |
| 1168 | GodotBody3D *b = state_query_list.first()->self(); |
| 1169 | state_query_list.remove(state_query_list.first()); |
| 1170 | b->call_queries(); |
| 1171 | } |
| 1172 | |
| 1173 | while (monitor_query_list.first()) { |
| 1174 | GodotArea3D *a = monitor_query_list.first()->self(); |
| 1175 | monitor_query_list.remove(monitor_query_list.first()); |
| 1176 | a->call_queries(); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | void GodotSpace3D::setup() { |
| 1181 | contact_debug_count = 0; |
| 1182 | while (mass_properties_update_list.first()) { |
| 1183 | mass_properties_update_list.first()->self()->update_mass_properties(); |
| 1184 | mass_properties_update_list.remove(mass_properties_update_list.first()); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | void GodotSpace3D::update() { |
| 1189 | broadphase->update(); |
| 1190 | } |
| 1191 | |
| 1192 | void GodotSpace3D::set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value) { |
| 1193 | switch (p_param) { |
| 1194 | case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: |
| 1195 | contact_recycle_radius = p_value; |
| 1196 | break; |
| 1197 | case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION: |
| 1198 | contact_max_separation = p_value; |
| 1199 | break; |
| 1200 | case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION: |
| 1201 | contact_max_allowed_penetration = p_value; |
| 1202 | break; |
| 1203 | case PhysicsServer3D::SPACE_PARAM_CONTACT_DEFAULT_BIAS: |
| 1204 | contact_bias = p_value; |
| 1205 | break; |
| 1206 | case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: |
| 1207 | body_linear_velocity_sleep_threshold = p_value; |
| 1208 | break; |
| 1209 | case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: |
| 1210 | body_angular_velocity_sleep_threshold = p_value; |
| 1211 | break; |
| 1212 | case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP: |
| 1213 | body_time_to_sleep = p_value; |
| 1214 | break; |
| 1215 | case PhysicsServer3D::SPACE_PARAM_SOLVER_ITERATIONS: |
| 1216 | solver_iterations = p_value; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | real_t GodotSpace3D::get_param(PhysicsServer3D::SpaceParameter p_param) const { |
| 1222 | switch (p_param) { |
| 1223 | case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: |
| 1224 | return contact_recycle_radius; |
| 1225 | case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION: |
| 1226 | return contact_max_separation; |
| 1227 | case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION: |
| 1228 | return contact_max_allowed_penetration; |
| 1229 | case PhysicsServer3D::SPACE_PARAM_CONTACT_DEFAULT_BIAS: |
| 1230 | return contact_bias; |
| 1231 | case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: |
| 1232 | return body_linear_velocity_sleep_threshold; |
| 1233 | case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: |
| 1234 | return body_angular_velocity_sleep_threshold; |
| 1235 | case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP: |
| 1236 | return body_time_to_sleep; |
| 1237 | case PhysicsServer3D::SPACE_PARAM_SOLVER_ITERATIONS: |
| 1238 | return solver_iterations; |
| 1239 | } |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | void GodotSpace3D::lock() { |
| 1244 | locked = true; |
| 1245 | } |
| 1246 | |
| 1247 | void GodotSpace3D::unlock() { |
| 1248 | locked = false; |
| 1249 | } |
| 1250 | |
| 1251 | bool GodotSpace3D::is_locked() const { |
| 1252 | return locked; |
| 1253 | } |
| 1254 | |
| 1255 | GodotPhysicsDirectSpaceState3D *GodotSpace3D::get_direct_state() { |
| 1256 | return direct_access; |
| 1257 | } |
| 1258 | |
| 1259 | GodotSpace3D::GodotSpace3D() { |
| 1260 | body_linear_velocity_sleep_threshold = GLOBAL_GET("physics/3d/sleep_threshold_linear"); |
| 1261 | body_angular_velocity_sleep_threshold = GLOBAL_GET("physics/3d/sleep_threshold_angular"); |
| 1262 | body_time_to_sleep = GLOBAL_GET("physics/3d/time_before_sleep"); |
| 1263 | solver_iterations = GLOBAL_GET("physics/3d/solver/solver_iterations"); |
| 1264 | contact_recycle_radius = GLOBAL_GET("physics/3d/solver/contact_recycle_radius"); |
| 1265 | contact_max_separation = GLOBAL_GET("physics/3d/solver/contact_max_separation"); |
| 1266 | contact_max_allowed_penetration = GLOBAL_GET("physics/3d/solver/contact_max_allowed_penetration"); |
| 1267 | contact_bias = GLOBAL_GET("physics/3d/solver/default_contact_bias"); |
| 1268 | |
| 1269 | broadphase = GodotBroadPhase3D::create_func(); |
| 1270 | broadphase->set_pair_callback(_broadphase_pair, this); |
| 1271 | broadphase->set_unpair_callback(_broadphase_unpair, this); |
| 1272 | |
| 1273 | direct_access = memnew(GodotPhysicsDirectSpaceState3D); |
| 1274 | direct_access->space = this; |
| 1275 | } |
| 1276 | |
| 1277 | GodotSpace3D::~GodotSpace3D() { |
| 1278 | memdelete(broadphase); |
| 1279 | memdelete(direct_access); |
| 1280 | } |