| 1 | /**************************************************************************/ |
| 2 | /* jolt_physics_direct_space_state_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 "jolt_physics_direct_space_state_3d.h" |
| 32 | |
| 33 | #include "../jolt_physics_server_3d.h" |
| 34 | #include "../jolt_project_settings.h" |
| 35 | #include "../misc/jolt_math_funcs.h" |
| 36 | #include "../misc/jolt_type_conversions.h" |
| 37 | #include "../objects/jolt_area_3d.h" |
| 38 | #include "../objects/jolt_body_3d.h" |
| 39 | #include "../objects/jolt_object_3d.h" |
| 40 | #include "../shapes/jolt_custom_motion_shape.h" |
| 41 | #include "../shapes/jolt_shape_3d.h" |
| 42 | #include "jolt_motion_filter_3d.h" |
| 43 | #include "jolt_query_collectors.h" |
| 44 | #include "jolt_query_filter_3d.h" |
| 45 | #include "jolt_space_3d.h" |
| 46 | |
| 47 | #include "Jolt/Geometry/GJKClosestPoint.h" |
| 48 | #include "Jolt/Physics/Body/Body.h" |
| 49 | #include "Jolt/Physics/Body/BodyFilter.h" |
| 50 | #include "Jolt/Physics/Collision/BroadPhase/BroadPhaseQuery.h" |
| 51 | #include "Jolt/Physics/Collision/CastResult.h" |
| 52 | #include "Jolt/Physics/Collision/CollidePointResult.h" |
| 53 | #include "Jolt/Physics/Collision/NarrowPhaseQuery.h" |
| 54 | #include "Jolt/Physics/Collision/RayCast.h" |
| 55 | #include "Jolt/Physics/Collision/Shape/MeshShape.h" |
| 56 | #include "Jolt/Physics/PhysicsSystem.h" |
| 57 | |
| 58 | bool JoltPhysicsDirectSpaceState3D::_cast_motion_impl(const JPH::Shape &p_jolt_shape, const Transform3D &p_transform_com, const Vector3 &p_scale, const Vector3 &p_motion, bool p_use_edge_removal, bool p_ignore_overlaps, const JPH::CollideShapeSettings &p_settings, const JPH::BroadPhaseLayerFilter &p_broad_phase_layer_filter, const JPH::ObjectLayerFilter &p_object_layer_filter, const JPH::BodyFilter &p_body_filter, const JPH::ShapeFilter &p_shape_filter, real_t &r_closest_safe, real_t &r_closest_unsafe) const { |
| 59 | r_closest_safe = 1.0f; |
| 60 | r_closest_unsafe = 1.0f; |
| 61 | |
| 62 | ERR_FAIL_COND_V_MSG(p_jolt_shape.GetType() != JPH::EShapeType::Convex, false, "Shape-casting with non-convex shapes is not supported."); |
| 63 | |
| 64 | const float motion_length = (float)p_motion.length(); |
| 65 | |
| 66 | if (p_ignore_overlaps && motion_length == 0.0f) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | const JPH::RMat44 transform_com = to_jolt_r(p_transform_com); |
| 71 | const JPH::Vec3 scale = to_jolt(p_scale); |
| 72 | const JPH::Vec3 motion = to_jolt(p_motion); |
| 73 | const JPH::Vec3 motion_local = transform_com.Multiply3x3Transposed(motion); |
| 74 | |
| 75 | JPH::AABox aabb = p_jolt_shape.GetWorldSpaceBounds(transform_com, scale); |
| 76 | JPH::AABox aabb_translated = aabb; |
| 77 | aabb_translated.Translate(motion); |
| 78 | aabb.Encapsulate(aabb_translated); |
| 79 | |
| 80 | JoltQueryCollectorAnyMulti<JPH::CollideShapeBodyCollector, 1024> aabb_collector; |
| 81 | space->get_broad_phase_query().CollideAABox(aabb, aabb_collector, p_broad_phase_layer_filter, p_object_layer_filter); |
| 82 | |
| 83 | if (!aabb_collector.had_hit()) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | const JPH::RVec3 base_offset = transform_com.GetTranslation(); |
| 88 | |
| 89 | JoltCustomMotionShape motion_shape(static_cast<const JPH::ConvexShape &>(p_jolt_shape)); |
| 90 | |
| 91 | auto collides = [&](const JPH::Body &p_other_body, float p_fraction) { |
| 92 | motion_shape.set_motion(motion_local * p_fraction); |
| 93 | |
| 94 | const JPH::TransformedShape other_shape = p_other_body.GetTransformedShape(); |
| 95 | |
| 96 | JoltQueryCollectorAny<JPH::CollideShapeCollector> collector; |
| 97 | |
| 98 | if (p_use_edge_removal) { |
| 99 | JPH::CollideShapeSettings eier_settings = p_settings; |
| 100 | eier_settings.mActiveEdgeMode = JPH::EActiveEdgeMode::CollideWithAll; |
| 101 | eier_settings.mCollectFacesMode = JPH::ECollectFacesMode::CollectFaces; |
| 102 | |
| 103 | JPH::InternalEdgeRemovingCollector eier_collector(collector); |
| 104 | other_shape.CollideShape(&motion_shape, scale, transform_com, eier_settings, base_offset, eier_collector, p_shape_filter); |
| 105 | eier_collector.Flush(); |
| 106 | } else { |
| 107 | other_shape.CollideShape(&motion_shape, scale, transform_com, p_settings, base_offset, collector, p_shape_filter); |
| 108 | } |
| 109 | |
| 110 | return collector.had_hit(); |
| 111 | }; |
| 112 | |
| 113 | // Figure out the number of steps we need in our binary search in order to achieve millimeter precision, within reason. |
| 114 | const int step_count = CLAMP(int(std::log(1000.0f * motion_length) / (float)Math::LN2), 4, 16); |
| 115 | |
| 116 | bool collided = false; |
| 117 | |
| 118 | for (int i = 0; i < aabb_collector.get_hit_count(); ++i) { |
| 119 | const JPH::BodyID other_jolt_id = aabb_collector.get_hit(i); |
| 120 | if (!p_body_filter.ShouldCollide(other_jolt_id)) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | const JPH::Body *other_jolt_body = space->try_get_jolt_body(other_jolt_id); |
| 125 | if (!p_body_filter.ShouldCollideLocked(*other_jolt_body)) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (!collides(*other_jolt_body, 1.0f)) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if (p_ignore_overlaps && collides(*other_jolt_body, 0.0f)) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | float lo = 0.0f; |
| 138 | float hi = 1.0f; |
| 139 | float coeff = 0.5f; |
| 140 | |
| 141 | for (int j = 0; j < step_count; ++j) { |
| 142 | const float fraction = lo + (hi - lo) * coeff; |
| 143 | |
| 144 | if (collides(*other_jolt_body, fraction)) { |
| 145 | collided = true; |
| 146 | |
| 147 | hi = fraction; |
| 148 | |
| 149 | if (j == 0 || lo > 0.0f) { |
| 150 | coeff = 0.5f; |
| 151 | } else { |
| 152 | coeff = 0.25f; |
| 153 | } |
| 154 | } else { |
| 155 | lo = fraction; |
| 156 | |
| 157 | if (j == 0 || hi < 1.0f) { |
| 158 | coeff = 0.5f; |
| 159 | } else { |
| 160 | coeff = 0.75f; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (lo < r_closest_safe) { |
| 166 | r_closest_safe = lo; |
| 167 | r_closest_unsafe = hi; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return collided; |
| 172 | } |
| 173 | |
| 174 | bool JoltPhysicsDirectSpaceState3D::_body_motion_recover(const JoltBody3D &p_body, const Transform3D &p_transform, float p_margin, const HashSet<RID> &p_excluded_bodies, const HashSet<ObjectID> &p_excluded_objects, Vector3 &r_recovery) const { |
| 175 | const JPH::Shape *jolt_shape = p_body.get_jolt_shape(); |
| 176 | |
| 177 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 178 | Transform3D transform_com = p_transform.translated_local(com_scaled); |
| 179 | |
| 180 | JPH::CollideShapeSettings settings; |
| 181 | settings.mMaxSeparationDistance = p_margin; |
| 182 | |
| 183 | const Vector3 &base_offset = transform_com.origin; |
| 184 | |
| 185 | const JoltMotionFilter3D motion_filter(p_body, p_excluded_bodies, p_excluded_objects); |
| 186 | JoltQueryCollectorAnyMulti<JPH::CollideShapeCollector, 32> collector; |
| 187 | |
| 188 | bool recovered = false; |
| 189 | |
| 190 | for (int i = 0; i < JoltProjectSettings::motion_query_recovery_iterations; ++i) { |
| 191 | collector.reset(); |
| 192 | |
| 193 | _collide_shape_kinematics(jolt_shape, JPH::Vec3::sOne(), to_jolt_r(transform_com), settings, to_jolt_r(base_offset), collector, motion_filter, motion_filter, motion_filter, motion_filter); |
| 194 | |
| 195 | if (!collector.had_hit()) { |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | const int hit_count = collector.get_hit_count(); |
| 200 | |
| 201 | float combined_priority = 0.0; |
| 202 | |
| 203 | for (int j = 0; j < hit_count; j++) { |
| 204 | const JPH::CollideShapeResult &hit = collector.get_hit(j); |
| 205 | const JoltBody3D *other_body = space->try_get_body(hit.mBodyID2); |
| 206 | ERR_CONTINUE(other_body == nullptr); |
| 207 | |
| 208 | combined_priority += other_body->get_collision_priority(); |
| 209 | } |
| 210 | |
| 211 | const float average_priority = MAX(combined_priority / (float)hit_count, (float)CMP_EPSILON); |
| 212 | |
| 213 | recovered = true; |
| 214 | |
| 215 | Vector3 recovery; |
| 216 | |
| 217 | for (int j = 0; j < hit_count; ++j) { |
| 218 | const JPH::CollideShapeResult &hit = collector.get_hit(j); |
| 219 | |
| 220 | const Vector3 penetration_axis = to_godot(hit.mPenetrationAxis.Normalized()); |
| 221 | const Vector3 margin_offset = penetration_axis * p_margin; |
| 222 | |
| 223 | const Vector3 point_on_1 = base_offset + to_godot(hit.mContactPointOn1) + margin_offset; |
| 224 | const Vector3 point_on_2 = base_offset + to_godot(hit.mContactPointOn2); |
| 225 | |
| 226 | const real_t distance_to_1 = penetration_axis.dot(point_on_1 + recovery); |
| 227 | const real_t distance_to_2 = penetration_axis.dot(point_on_2); |
| 228 | |
| 229 | const float penetration_depth = float(distance_to_1 - distance_to_2); |
| 230 | |
| 231 | if (penetration_depth <= 0.0f) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | const JoltBody3D *other_body = space->try_get_body(hit.mBodyID2); |
| 236 | ERR_CONTINUE(other_body == nullptr); |
| 237 | |
| 238 | const float recovery_distance = penetration_depth * JoltProjectSettings::motion_query_recovery_amount; |
| 239 | const float other_priority = other_body->get_collision_priority(); |
| 240 | const float other_priority_normalized = other_priority / average_priority; |
| 241 | const float scaled_recovery_distance = recovery_distance * other_priority_normalized; |
| 242 | |
| 243 | recovery -= penetration_axis * scaled_recovery_distance; |
| 244 | } |
| 245 | |
| 246 | if (recovery == Vector3()) { |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | r_recovery += recovery; |
| 251 | transform_com.origin += recovery; |
| 252 | } |
| 253 | |
| 254 | return recovered; |
| 255 | } |
| 256 | |
| 257 | bool JoltPhysicsDirectSpaceState3D::_body_motion_cast(const JoltBody3D &p_body, const Transform3D &p_transform, const Vector3 &p_scale, const Vector3 &p_motion, bool p_collide_separation_ray, const HashSet<RID> &p_excluded_bodies, const HashSet<ObjectID> &p_excluded_objects, real_t &r_safe_fraction, real_t &r_unsafe_fraction) const { |
| 258 | const Transform3D body_transform = p_transform.scaled_local(p_scale); |
| 259 | |
| 260 | const JPH::CollideShapeSettings settings; |
| 261 | const JoltMotionFilter3D motion_filter(p_body, p_excluded_bodies, p_excluded_objects, p_collide_separation_ray); |
| 262 | |
| 263 | bool collided = false; |
| 264 | |
| 265 | for (int i = 0; i < p_body.get_shape_count(); ++i) { |
| 266 | if (p_body.is_shape_disabled(i)) { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | JoltShape3D *shape = p_body.get_shape(i); |
| 271 | |
| 272 | if (!shape->is_convex()) { |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | const JPH::ShapeRefC jolt_shape = shape->try_build(); |
| 277 | if (unlikely(jolt_shape == nullptr)) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 282 | const Transform3D transform_local = p_body.get_shape_transform_scaled(i); |
| 283 | const Transform3D transform_com_local = transform_local.translated_local(com_scaled); |
| 284 | Transform3D transform_com = body_transform * transform_com_local; |
| 285 | |
| 286 | Vector3 scale; |
| 287 | JoltMath::decompose(transform_com, scale); |
| 288 | JOLT_ENSURE_SCALE_VALID(jolt_shape, scale, "body_test_motion was passed an invalid transform along with body '%s'. This results in invalid scaling for shape at index %d."); |
| 289 | |
| 290 | real_t shape_safe_fraction = 1.0; |
| 291 | real_t shape_unsafe_fraction = 1.0; |
| 292 | |
| 293 | collided |= _cast_motion_impl(*jolt_shape, transform_com, scale, p_motion, JoltProjectSettings::use_enhanced_internal_edge_removal_for_motion_queries, false, settings, motion_filter, motion_filter, motion_filter, motion_filter, shape_safe_fraction, shape_unsafe_fraction); |
| 294 | |
| 295 | r_safe_fraction = MIN(r_safe_fraction, shape_safe_fraction); |
| 296 | r_unsafe_fraction = MIN(r_unsafe_fraction, shape_unsafe_fraction); |
| 297 | } |
| 298 | |
| 299 | return collided; |
| 300 | } |
| 301 | |
| 302 | bool JoltPhysicsDirectSpaceState3D::_body_motion_collide(const JoltBody3D &p_body, const Transform3D &p_transform, const Vector3 &p_motion, float p_margin, int p_max_collisions, const HashSet<RID> &p_excluded_bodies, const HashSet<ObjectID> &p_excluded_objects, PhysicsServer3D::MotionResult *p_result) const { |
| 303 | if (p_max_collisions == 0) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | const JPH::Shape *jolt_shape = p_body.get_jolt_shape(); |
| 308 | |
| 309 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 310 | const Transform3D transform_com = p_transform.translated_local(com_scaled); |
| 311 | |
| 312 | JPH::CollideShapeSettings settings; |
| 313 | settings.mCollectFacesMode = JPH::ECollectFacesMode::CollectFaces; |
| 314 | settings.mMaxSeparationDistance = p_margin; |
| 315 | |
| 316 | const Vector3 &base_offset = transform_com.origin; |
| 317 | |
| 318 | const JoltMotionFilter3D motion_filter(p_body, p_excluded_bodies, p_excluded_objects); |
| 319 | JoltQueryCollectorClosestMulti<JPH::CollideShapeCollector, 32> collector(p_max_collisions); |
| 320 | _collide_shape_kinematics(jolt_shape, JPH::Vec3::sOne(), to_jolt_r(transform_com), settings, to_jolt_r(base_offset), collector, motion_filter, motion_filter, motion_filter, motion_filter); |
| 321 | |
| 322 | if (!collector.had_hit() || p_result == nullptr) { |
| 323 | return collector.had_hit(); |
| 324 | } |
| 325 | |
| 326 | int count = 0; |
| 327 | |
| 328 | for (int i = 0; i < collector.get_hit_count(); ++i) { |
| 329 | const JPH::CollideShapeResult &hit = collector.get_hit(i); |
| 330 | |
| 331 | const float penetration_depth = hit.mPenetrationDepth + p_margin; |
| 332 | |
| 333 | if (penetration_depth <= 0.0f) { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | const Vector3 normal = to_godot(-hit.mPenetrationAxis.Normalized()); |
| 338 | |
| 339 | if (p_motion.length_squared() > 0) { |
| 340 | const Vector3 direction = p_motion.normalized(); |
| 341 | |
| 342 | if (direction.dot(normal) >= -CMP_EPSILON) { |
| 343 | continue; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | JPH::ContactPoints contact_points1; |
| 348 | JPH::ContactPoints contact_points2; |
| 349 | |
| 350 | if (p_max_collisions > 1) { |
| 351 | _generate_manifold(hit, contact_points1, contact_points2 JPH_IF_DEBUG_RENDERER(, to_jolt_r(base_offset))); |
| 352 | } else { |
| 353 | contact_points2.push_back(hit.mContactPointOn2); |
| 354 | } |
| 355 | |
| 356 | const JoltShapedObject3D *collider = space->try_get_shaped(hit.mBodyID2); |
| 357 | ERR_FAIL_NULL_V(collider, false); |
| 358 | |
| 359 | const int local_shape = p_body.find_shape_index(hit.mSubShapeID1); |
| 360 | ERR_FAIL_COND_V(local_shape == -1, false); |
| 361 | |
| 362 | const int collider_shape = collider->find_shape_index(hit.mSubShapeID2); |
| 363 | ERR_FAIL_COND_V(collider_shape == -1, false); |
| 364 | |
| 365 | for (JPH::Vec3 contact_point : contact_points2) { |
| 366 | const Vector3 position = base_offset + to_godot(contact_point); |
| 367 | |
| 368 | PhysicsServer3D::MotionCollision &collision = p_result->collisions[count++]; |
| 369 | |
| 370 | collision.position = position; |
| 371 | collision.normal = normal; |
| 372 | collision.collider_velocity = collider->get_velocity_at_position(position); |
| 373 | collision.collider_angular_velocity = collider->get_angular_velocity(); |
| 374 | collision.depth = penetration_depth; |
| 375 | collision.local_shape = local_shape; |
| 376 | collision.collider_id = collider->get_instance_id(); |
| 377 | collision.collider = collider->get_rid(); |
| 378 | collision.collider_shape = collider_shape; |
| 379 | |
| 380 | if (count == p_max_collisions) { |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (count == p_max_collisions) { |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | p_result->collision_count = count; |
| 391 | |
| 392 | return count > 0; |
| 393 | } |
| 394 | |
| 395 | int JoltPhysicsDirectSpaceState3D::_try_get_face_index(const JPH::Body &p_body, const JPH::SubShapeID &p_sub_shape_id) { |
| 396 | if (!JoltProjectSettings::enable_ray_cast_face_index) { |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | const JPH::Shape *root_shape = p_body.GetShape(); |
| 401 | JPH::SubShapeID sub_shape_id_remainder; |
| 402 | const JPH::Shape *leaf_shape = root_shape->GetLeafShape(p_sub_shape_id, sub_shape_id_remainder); |
| 403 | |
| 404 | if (leaf_shape->GetType() != JPH::EShapeType::Mesh) { |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | const JPH::MeshShape *mesh_shape = static_cast<const JPH::MeshShape *>(leaf_shape); |
| 409 | return (int)mesh_shape->GetTriangleUserData(sub_shape_id_remainder); |
| 410 | } |
| 411 | |
| 412 | void JoltPhysicsDirectSpaceState3D::_generate_manifold(const JPH::CollideShapeResult &p_hit, JPH::ContactPoints &r_contact_points1, JPH::ContactPoints &r_contact_points2 JPH_IF_DEBUG_RENDERER(, JPH::RVec3Arg p_center_of_mass)) const { |
| 413 | const JPH::PhysicsSystem &physics_system = space->get_physics_system(); |
| 414 | const JPH::PhysicsSettings &physics_settings = physics_system.GetPhysicsSettings(); |
| 415 | const JPH::Vec3 penetration_axis = p_hit.mPenetrationAxis.Normalized(); |
| 416 | |
| 417 | JPH::ManifoldBetweenTwoFaces(p_hit.mContactPointOn1, p_hit.mContactPointOn2, penetration_axis, physics_settings.mManifoldTolerance, p_hit.mShape1Face, p_hit.mShape2Face, r_contact_points1, r_contact_points2 JPH_IF_DEBUG_RENDERER(, p_center_of_mass)); |
| 418 | |
| 419 | if (r_contact_points1.size() > 4) { |
| 420 | JPH::PruneContactPoints(penetration_axis, r_contact_points1, r_contact_points2 JPH_IF_DEBUG_RENDERER(, p_center_of_mass)); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void JoltPhysicsDirectSpaceState3D::_collide_shape_queries( |
| 425 | const JPH::Shape *p_shape, |
| 426 | JPH::Vec3Arg p_scale, |
| 427 | JPH::RMat44Arg p_transform_com, |
| 428 | const JPH::CollideShapeSettings &p_settings, |
| 429 | JPH::RVec3Arg p_base_offset, |
| 430 | JPH::CollideShapeCollector &p_collector, |
| 431 | const JPH::BroadPhaseLayerFilter &p_broad_phase_layer_filter, |
| 432 | const JPH::ObjectLayerFilter &p_object_layer_filter, |
| 433 | const JPH::BodyFilter &p_body_filter, |
| 434 | const JPH::ShapeFilter &p_shape_filter) const { |
| 435 | if (JoltProjectSettings::use_enhanced_internal_edge_removal_for_queries) { |
| 436 | space->get_narrow_phase_query().CollideShapeWithInternalEdgeRemoval(p_shape, p_scale, p_transform_com, p_settings, p_base_offset, p_collector, p_broad_phase_layer_filter, p_object_layer_filter, p_body_filter, p_shape_filter); |
| 437 | } else { |
| 438 | space->get_narrow_phase_query().CollideShape(p_shape, p_scale, p_transform_com, p_settings, p_base_offset, p_collector, p_broad_phase_layer_filter, p_object_layer_filter, p_body_filter, p_shape_filter); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void JoltPhysicsDirectSpaceState3D::_collide_shape_kinematics( |
| 443 | const JPH::Shape *p_shape, |
| 444 | JPH::Vec3Arg p_scale, |
| 445 | JPH::RMat44Arg p_transform_com, |
| 446 | const JPH::CollideShapeSettings &p_settings, |
| 447 | JPH::RVec3Arg p_base_offset, |
| 448 | JPH::CollideShapeCollector &p_collector, |
| 449 | const JPH::BroadPhaseLayerFilter &p_broad_phase_layer_filter, |
| 450 | const JPH::ObjectLayerFilter &p_object_layer_filter, |
| 451 | const JPH::BodyFilter &p_body_filter, |
| 452 | const JPH::ShapeFilter &p_shape_filter) const { |
| 453 | if (JoltProjectSettings::use_enhanced_internal_edge_removal_for_motion_queries) { |
| 454 | space->get_narrow_phase_query().CollideShapeWithInternalEdgeRemoval(p_shape, p_scale, p_transform_com, p_settings, p_base_offset, p_collector, p_broad_phase_layer_filter, p_object_layer_filter, p_body_filter, p_shape_filter); |
| 455 | } else { |
| 456 | space->get_narrow_phase_query().CollideShape(p_shape, p_scale, p_transform_com, p_settings, p_base_offset, p_collector, p_broad_phase_layer_filter, p_object_layer_filter, p_body_filter, p_shape_filter); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | JoltPhysicsDirectSpaceState3D::JoltPhysicsDirectSpaceState3D(JoltSpace3D *p_space) : |
| 461 | space(p_space) { |
| 462 | } |
| 463 | |
| 464 | bool JoltPhysicsDirectSpaceState3D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) { |
| 465 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "intersect_ray must not be called while the physics space is being stepped."); |
| 466 | |
| 467 | space->flush_pending_objects(); |
| 468 | |
| 469 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude, p_parameters.pick_ray); |
| 470 | |
| 471 | const JPH::RVec3 from = to_jolt_r(p_parameters.from); |
| 472 | const JPH::RVec3 to = to_jolt_r(p_parameters.to); |
| 473 | const JPH::Vec3 vector = JPH::Vec3(to - from); |
| 474 | const JPH::RRayCast ray(from, vector); |
| 475 | |
| 476 | const JPH::EBackFaceMode back_face_mode = p_parameters.hit_back_faces ? JPH::EBackFaceMode::CollideWithBackFaces : JPH::EBackFaceMode::IgnoreBackFaces; |
| 477 | |
| 478 | JPH::RayCastSettings settings; |
| 479 | settings.mTreatConvexAsSolid = p_parameters.hit_from_inside; |
| 480 | settings.mBackFaceModeTriangles = back_face_mode; |
| 481 | |
| 482 | JoltQueryCollectorClosest<JPH::CastRayCollector> collector; |
| 483 | space->get_narrow_phase_query().CastRay(ray, settings, collector, query_filter, query_filter, query_filter); |
| 484 | |
| 485 | if (!collector.had_hit()) { |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | const JPH::RayCastResult &hit = collector.get_hit(); |
| 490 | |
| 491 | const JPH::BodyID &body_id = hit.mBodyID; |
| 492 | const JPH::SubShapeID &sub_shape_id = hit.mSubShapeID2; |
| 493 | |
| 494 | const JoltObject3D *object = space->try_get_object(body_id); |
| 495 | ERR_FAIL_NULL_V(object, false); |
| 496 | |
| 497 | const JPH::RVec3 position = ray.GetPointOnRay(hit.mFraction); |
| 498 | |
| 499 | JPH::Vec3 normal = JPH::Vec3::sZero(); |
| 500 | |
| 501 | if (!p_parameters.hit_from_inside || hit.mFraction > 0.0f) { |
| 502 | normal = object->get_jolt_body()->GetWorldSpaceSurfaceNormal(sub_shape_id, position); |
| 503 | |
| 504 | // If we got a back-face normal we need to flip it. |
| 505 | if (normal.Dot(vector) > 0) { |
| 506 | normal = -normal; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | r_result.position = to_godot(position); |
| 511 | r_result.normal = to_godot(normal); |
| 512 | r_result.rid = object->get_rid(); |
| 513 | r_result.collider_id = object->get_instance_id(); |
| 514 | r_result.collider = object->get_instance(); |
| 515 | r_result.shape = 0; |
| 516 | |
| 517 | if (const JoltShapedObject3D *shaped_object = object->as_shaped()) { |
| 518 | const int shape_index = shaped_object->find_shape_index(sub_shape_id); |
| 519 | ERR_FAIL_COND_V(shape_index == -1, false); |
| 520 | r_result.shape = shape_index; |
| 521 | r_result.face_index = _try_get_face_index(*object->get_jolt_body(), sub_shape_id); |
| 522 | } |
| 523 | |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | int JoltPhysicsDirectSpaceState3D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) { |
| 528 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "intersect_point must not be called while the physics space is being stepped."); |
| 529 | |
| 530 | if (p_result_max == 0) { |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | space->flush_pending_objects(); |
| 535 | |
| 536 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude); |
| 537 | JoltQueryCollectorAnyMulti<JPH::CollidePointCollector, 32> collector(p_result_max); |
| 538 | space->get_narrow_phase_query().CollidePoint(to_jolt_r(p_parameters.position), collector, query_filter, query_filter, query_filter); |
| 539 | |
| 540 | const int hit_count = collector.get_hit_count(); |
| 541 | |
| 542 | for (int i = 0; i < hit_count; ++i) { |
| 543 | const JPH::CollidePointResult &hit = collector.get_hit(i); |
| 544 | const JoltObject3D *object = space->try_get_object(hit.mBodyID); |
| 545 | ERR_FAIL_NULL_V(object, 0); |
| 546 | |
| 547 | ShapeResult &result = *r_results++; |
| 548 | |
| 549 | result.shape = 0; |
| 550 | |
| 551 | if (const JoltShapedObject3D *shaped_object = object->as_shaped()) { |
| 552 | const int shape_index = shaped_object->find_shape_index(hit.mSubShapeID2); |
| 553 | ERR_FAIL_COND_V(shape_index == -1, 0); |
| 554 | result.shape = shape_index; |
| 555 | } |
| 556 | |
| 557 | result.rid = object->get_rid(); |
| 558 | result.collider_id = object->get_instance_id(); |
| 559 | result.collider = object->get_instance(); |
| 560 | } |
| 561 | |
| 562 | return hit_count; |
| 563 | } |
| 564 | |
| 565 | int JoltPhysicsDirectSpaceState3D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) { |
| 566 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "intersect_shape must not be called while the physics space is being stepped."); |
| 567 | |
| 568 | if (p_result_max == 0) { |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | space->flush_pending_objects(); |
| 573 | |
| 574 | JoltShape3D *shape = JoltPhysicsServer3D::get_singleton()->get_shape(p_parameters.shape_rid); |
| 575 | ERR_FAIL_NULL_V(shape, 0); |
| 576 | |
| 577 | const JPH::ShapeRefC jolt_shape = shape->try_build(); |
| 578 | ERR_FAIL_NULL_V(jolt_shape, 0); |
| 579 | |
| 580 | Transform3D transform = p_parameters.transform; |
| 581 | JOLT_ENSURE_SCALE_NOT_ZERO(transform, "intersect_shape was passed an invalid transform."); |
| 582 | |
| 583 | Vector3 scale; |
| 584 | JoltMath::decompose(transform, scale); |
| 585 | JOLT_ENSURE_SCALE_VALID(jolt_shape, scale, "intersect_shape was passed an invalid transform."); |
| 586 | |
| 587 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 588 | const Transform3D transform_com = transform.translated_local(com_scaled); |
| 589 | |
| 590 | JPH::CollideShapeSettings settings; |
| 591 | settings.mMaxSeparationDistance = (float)p_parameters.margin; |
| 592 | |
| 593 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude); |
| 594 | JoltQueryCollectorAnyMulti<JPH::CollideShapeCollector, 32> collector(p_result_max); |
| 595 | _collide_shape_queries(jolt_shape, to_jolt(scale), to_jolt_r(transform_com), settings, to_jolt_r(transform_com.origin), collector, query_filter, query_filter, query_filter); |
| 596 | |
| 597 | const int hit_count = collector.get_hit_count(); |
| 598 | |
| 599 | for (int i = 0; i < hit_count; ++i) { |
| 600 | const JPH::CollideShapeResult &hit = collector.get_hit(i); |
| 601 | const JoltObject3D *object = space->try_get_object(hit.mBodyID2); |
| 602 | ERR_FAIL_NULL_V(object, 0); |
| 603 | |
| 604 | ShapeResult &result = *r_results++; |
| 605 | |
| 606 | result.shape = 0; |
| 607 | |
| 608 | if (const JoltShapedObject3D *shaped_object = object->as_shaped()) { |
| 609 | const int shape_index = shaped_object->find_shape_index(hit.mSubShapeID2); |
| 610 | ERR_FAIL_COND_V(shape_index == -1, 0); |
| 611 | result.shape = shape_index; |
| 612 | } |
| 613 | |
| 614 | result.rid = object->get_rid(); |
| 615 | result.collider_id = object->get_instance_id(); |
| 616 | result.collider = object->get_instance(); |
| 617 | } |
| 618 | |
| 619 | return hit_count; |
| 620 | } |
| 621 | |
| 622 | bool JoltPhysicsDirectSpaceState3D::cast_motion(const ShapeParameters &p_parameters, real_t &r_closest_safe, real_t &r_closest_unsafe, ShapeRestInfo *r_info) { |
| 623 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "cast_motion must not be called while the physics space is being stepped."); |
| 624 | ERR_FAIL_COND_V_MSG(r_info != nullptr, false, "Providing rest info as part of cast_motion is not supported when using Jolt Physics."); |
| 625 | |
| 626 | space->flush_pending_objects(); |
| 627 | |
| 628 | JoltShape3D *shape = JoltPhysicsServer3D::get_singleton()->get_shape(p_parameters.shape_rid); |
| 629 | ERR_FAIL_NULL_V(shape, false); |
| 630 | |
| 631 | const JPH::ShapeRefC jolt_shape = shape->try_build(); |
| 632 | ERR_FAIL_NULL_V(jolt_shape, false); |
| 633 | |
| 634 | Transform3D transform = p_parameters.transform; |
| 635 | JOLT_ENSURE_SCALE_NOT_ZERO(transform, "cast_motion (maybe from ShapeCast3D?) was passed an invalid transform."); |
| 636 | |
| 637 | Vector3 scale; |
| 638 | JoltMath::decompose(transform, scale); |
| 639 | JOLT_ENSURE_SCALE_VALID(jolt_shape, scale, "cast_motion (maybe from ShapeCast3D?) was passed an invalid transform."); |
| 640 | |
| 641 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 642 | Transform3D transform_com = transform.translated_local(com_scaled); |
| 643 | |
| 644 | JPH::CollideShapeSettings settings; |
| 645 | settings.mMaxSeparationDistance = (float)p_parameters.margin; |
| 646 | |
| 647 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude); |
| 648 | _cast_motion_impl(*jolt_shape, transform_com, scale, p_parameters.motion, JoltProjectSettings::use_enhanced_internal_edge_removal_for_queries, true, settings, query_filter, query_filter, query_filter, JPH::ShapeFilter(), r_closest_safe, r_closest_unsafe); |
| 649 | |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | bool JoltPhysicsDirectSpaceState3D::collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) { |
| 654 | r_result_count = 0; |
| 655 | |
| 656 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "collide_shape must not be called while the physics space is being stepped."); |
| 657 | |
| 658 | if (p_result_max == 0) { |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | space->flush_pending_objects(); |
| 663 | |
| 664 | JoltShape3D *shape = JoltPhysicsServer3D::get_singleton()->get_shape(p_parameters.shape_rid); |
| 665 | ERR_FAIL_NULL_V(shape, false); |
| 666 | |
| 667 | const JPH::ShapeRefC jolt_shape = shape->try_build(); |
| 668 | ERR_FAIL_NULL_V(jolt_shape, false); |
| 669 | |
| 670 | Transform3D transform = p_parameters.transform; |
| 671 | JOLT_ENSURE_SCALE_NOT_ZERO(transform, "collide_shape was passed an invalid transform."); |
| 672 | |
| 673 | Vector3 scale; |
| 674 | JoltMath::decompose(transform, scale); |
| 675 | JOLT_ENSURE_SCALE_VALID(jolt_shape, scale, "collide_shape was passed an invalid transform."); |
| 676 | |
| 677 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 678 | const Transform3D transform_com = transform.translated_local(com_scaled); |
| 679 | |
| 680 | JPH::CollideShapeSettings settings; |
| 681 | settings.mCollectFacesMode = JPH::ECollectFacesMode::CollectFaces; |
| 682 | settings.mMaxSeparationDistance = (float)p_parameters.margin; |
| 683 | |
| 684 | const Vector3 &base_offset = transform_com.origin; |
| 685 | |
| 686 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude); |
| 687 | JoltQueryCollectorAnyMulti<JPH::CollideShapeCollector, 32> collector(p_result_max); |
| 688 | _collide_shape_queries(jolt_shape, to_jolt(scale), to_jolt_r(transform_com), settings, to_jolt_r(base_offset), collector, query_filter, query_filter, query_filter); |
| 689 | |
| 690 | if (!collector.had_hit()) { |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | const int max_points = p_result_max * 2; |
| 695 | |
| 696 | int point_count = 0; |
| 697 | |
| 698 | for (int i = 0; i < collector.get_hit_count(); ++i) { |
| 699 | const JPH::CollideShapeResult &hit = collector.get_hit(i); |
| 700 | |
| 701 | const Vector3 penetration_axis = to_godot(hit.mPenetrationAxis.Normalized()); |
| 702 | const Vector3 margin_offset = penetration_axis * (float)p_parameters.margin; |
| 703 | |
| 704 | JPH::ContactPoints contact_points1; |
| 705 | JPH::ContactPoints contact_points2; |
| 706 | |
| 707 | _generate_manifold(hit, contact_points1, contact_points2 JPH_IF_DEBUG_RENDERER(, to_jolt_r(base_offset))); |
| 708 | |
| 709 | for (JPH::uint j = 0; j < contact_points1.size(); ++j) { |
| 710 | r_results[point_count++] = base_offset + to_godot(contact_points1[j]) + margin_offset; |
| 711 | r_results[point_count++] = base_offset + to_godot(contact_points2[j]); |
| 712 | |
| 713 | if (point_count >= max_points) { |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (point_count >= max_points) { |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | r_result_count = point_count / 2; |
| 724 | |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | bool JoltPhysicsDirectSpaceState3D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) { |
| 729 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "get_rest_info must not be called while the physics space is being stepped."); |
| 730 | |
| 731 | space->flush_pending_objects(); |
| 732 | |
| 733 | JoltShape3D *shape = JoltPhysicsServer3D::get_singleton()->get_shape(p_parameters.shape_rid); |
| 734 | ERR_FAIL_NULL_V(shape, false); |
| 735 | |
| 736 | const JPH::ShapeRefC jolt_shape = shape->try_build(); |
| 737 | ERR_FAIL_NULL_V(jolt_shape, false); |
| 738 | |
| 739 | Transform3D transform = p_parameters.transform; |
| 740 | JOLT_ENSURE_SCALE_NOT_ZERO(transform, "get_rest_info (maybe from ShapeCast3D?) was passed an invalid transform."); |
| 741 | |
| 742 | Vector3 scale; |
| 743 | JoltMath::decompose(transform, scale); |
| 744 | JOLT_ENSURE_SCALE_VALID(jolt_shape, scale, "get_rest_info (maybe from ShapeCast3D?) was passed an invalid transform."); |
| 745 | |
| 746 | const Vector3 com_scaled = to_godot(jolt_shape->GetCenterOfMass()); |
| 747 | const Transform3D transform_com = transform.translated_local(com_scaled); |
| 748 | |
| 749 | JPH::CollideShapeSettings settings; |
| 750 | settings.mMaxSeparationDistance = (float)p_parameters.margin; |
| 751 | |
| 752 | const Vector3 &base_offset = transform_com.origin; |
| 753 | |
| 754 | const JoltQueryFilter3D query_filter(*this, p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas, p_parameters.exclude); |
| 755 | JoltQueryCollectorClosest<JPH::CollideShapeCollector> collector; |
| 756 | _collide_shape_queries(jolt_shape, to_jolt(scale), to_jolt_r(transform_com), settings, to_jolt_r(base_offset), collector, query_filter, query_filter, query_filter); |
| 757 | |
| 758 | if (!collector.had_hit()) { |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | const JPH::CollideShapeResult &hit = collector.get_hit(); |
| 763 | const JoltObject3D *object = space->try_get_object(hit.mBodyID2); |
| 764 | ERR_FAIL_NULL_V(object, false); |
| 765 | |
| 766 | r_info->shape = 0; |
| 767 | |
| 768 | if (const JoltShapedObject3D *shaped_object = object->as_shaped()) { |
| 769 | const int shape_index = shaped_object->find_shape_index(hit.mSubShapeID2); |
| 770 | ERR_FAIL_COND_V(shape_index == -1, false); |
| 771 | r_info->shape = shape_index; |
| 772 | } |
| 773 | |
| 774 | const Vector3 hit_point = base_offset + to_godot(hit.mContactPointOn2); |
| 775 | |
| 776 | r_info->point = hit_point; |
| 777 | r_info->normal = to_godot(-hit.mPenetrationAxis.Normalized()); |
| 778 | r_info->rid = object->get_rid(); |
| 779 | r_info->collider_id = object->get_instance_id(); |
| 780 | r_info->linear_velocity = object->get_velocity_at_position(hit_point); |
| 781 | |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | Vector3 JoltPhysicsDirectSpaceState3D::get_closest_point_to_object_volume(RID p_object, Vector3 p_point) const { |
| 786 | ERR_FAIL_COND_V_MSG(space->is_stepping(), Vector3(), "get_closest_point_to_object_volume must not be called while the physics space is being stepped."); |
| 787 | |
| 788 | space->flush_pending_objects(); |
| 789 | |
| 790 | JoltPhysicsServer3D *physics_server = JoltPhysicsServer3D::get_singleton(); |
| 791 | JoltObject3D *object = physics_server->get_area(p_object); |
| 792 | |
| 793 | if (object == nullptr) { |
| 794 | object = physics_server->get_body(p_object); |
| 795 | } |
| 796 | |
| 797 | ERR_FAIL_NULL_V(object, Vector3()); |
| 798 | ERR_FAIL_COND_V(object->get_space() != space, Vector3()); |
| 799 | |
| 800 | JoltQueryCollectorAll<JPH::TransformedShapeCollector, 32> collector; |
| 801 | const JPH::TransformedShape root_shape = object->get_jolt_body()->GetTransformedShape(); |
| 802 | root_shape.CollectTransformedShapes(object->get_jolt_body()->GetWorldSpaceBounds(), collector); |
| 803 | |
| 804 | const JPH::RVec3 point = to_jolt_r(p_point); |
| 805 | |
| 806 | float closest_distance_sq = FLT_MAX; |
| 807 | JPH::RVec3 closest_point = JPH::RVec3::sZero(); |
| 808 | |
| 809 | bool found_point = false; |
| 810 | |
| 811 | for (int i = 0; i < collector.get_hit_count(); ++i) { |
| 812 | const JPH::TransformedShape &shape_transformed = collector.get_hit(i); |
| 813 | const JPH::Shape &shape = *shape_transformed.mShape; |
| 814 | |
| 815 | if (shape.GetType() != JPH::EShapeType::Convex) { |
| 816 | continue; |
| 817 | } |
| 818 | |
| 819 | const JPH::ConvexShape &shape_convex = static_cast<const JPH::ConvexShape &>(shape); |
| 820 | |
| 821 | JPH::GJKClosestPoint gjk; |
| 822 | |
| 823 | JPH::ConvexShape::SupportBuffer shape_support_buffer; |
| 824 | const JPH::ConvexShape::Support *shape_support = shape_convex.GetSupportFunction(JPH::ConvexShape::ESupportMode::IncludeConvexRadius, shape_support_buffer, shape_transformed.GetShapeScale()); |
| 825 | |
| 826 | const JPH::RMat44 shape_rotation = JPH::RMat44::sRotation(shape_transformed.mShapeRotation); |
| 827 | const JPH::Vec3 shape_com = shape_rotation.Multiply3x3(shape.GetCenterOfMass()); |
| 828 | const JPH::RVec3 shape_pos = shape_transformed.mShapePositionCOM - JPH::RVec3(shape_com); |
| 829 | const JPH::RMat44 shape_xform = shape_rotation.PostTranslated(shape_pos); |
| 830 | const JPH::RMat44 shape_xform_inv = shape_xform.InversedRotationTranslation(); |
| 831 | |
| 832 | JPH::PointConvexSupport point_support; |
| 833 | point_support.mPoint = JPH::Vec3(shape_xform_inv * point); |
| 834 | |
| 835 | JPH::Vec3 separating_axis = JPH::Vec3::sAxisX(); |
| 836 | JPH::Vec3 point_on_a = JPH::Vec3::sZero(); |
| 837 | JPH::Vec3 point_on_b = JPH::Vec3::sZero(); |
| 838 | |
| 839 | const float distance_sq = gjk.GetClosestPoints(*shape_support, point_support, JPH::cDefaultCollisionTolerance, FLT_MAX, separating_axis, point_on_a, point_on_b); |
| 840 | |
| 841 | if (distance_sq == 0.0f) { |
| 842 | closest_point = point; |
| 843 | found_point = true; |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | if (distance_sq < closest_distance_sq) { |
| 848 | closest_distance_sq = distance_sq; |
| 849 | closest_point = shape_xform * point_on_a; |
| 850 | found_point = true; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if (found_point) { |
| 855 | return to_godot(closest_point); |
| 856 | } else { |
| 857 | return to_godot(object->get_jolt_body()->GetPosition()); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | bool JoltPhysicsDirectSpaceState3D::body_test_motion(const JoltBody3D &p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result) const { |
| 862 | ERR_FAIL_COND_V_MSG(space->is_stepping(), false, "body_test_motion (maybe from move_and_slide?) must not be called while the physics space is being stepped."); |
| 863 | |
| 864 | if (!p_body.in_space()) { |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | space->flush_pending_objects(); |
| 869 | |
| 870 | const float margin = MAX((float)p_parameters.margin, 0.0001f); |
| 871 | const int max_collisions = MIN(p_parameters.max_collisions, 32); |
| 872 | |
| 873 | Transform3D transform = p_parameters.from; |
| 874 | JOLT_ENSURE_SCALE_NOT_ZERO(transform, vformat("body_test_motion (maybe from move_and_slide?) was passed an invalid transform along with body '%s'.", p_body.to_string())); |
| 875 | |
| 876 | Vector3 scale; |
| 877 | JoltMath::decompose(transform, scale); |
| 878 | |
| 879 | Vector3 recovery; |
| 880 | const bool recovered = _body_motion_recover(p_body, transform, margin, p_parameters.exclude_bodies, p_parameters.exclude_objects, recovery); |
| 881 | |
| 882 | transform.origin += recovery; |
| 883 | |
| 884 | real_t safe_fraction = 1.0; |
| 885 | real_t unsafe_fraction = 1.0; |
| 886 | |
| 887 | const bool hit = _body_motion_cast(p_body, transform, scale, p_parameters.motion, p_parameters.collide_separation_ray, p_parameters.exclude_bodies, p_parameters.exclude_objects, safe_fraction, unsafe_fraction); |
| 888 | |
| 889 | bool collided = false; |
| 890 | |
| 891 | if (hit || (recovered && p_parameters.recovery_as_collision)) { |
| 892 | collided = _body_motion_collide(p_body, transform.translated(p_parameters.motion * unsafe_fraction), p_parameters.motion, margin, max_collisions, p_parameters.exclude_bodies, p_parameters.exclude_objects, r_result); |
| 893 | } |
| 894 | |
| 895 | if (r_result == nullptr) { |
| 896 | return collided; |
| 897 | } |
| 898 | |
| 899 | if (collided) { |
| 900 | const PhysicsServer3D::MotionCollision &deepest = r_result->collisions[0]; |
| 901 | |
| 902 | r_result->travel = recovery + p_parameters.motion * safe_fraction; |
| 903 | r_result->remainder = p_parameters.motion - p_parameters.motion * safe_fraction; |
| 904 | r_result->collision_depth = deepest.depth; |
| 905 | r_result->collision_safe_fraction = safe_fraction; |
| 906 | r_result->collision_unsafe_fraction = unsafe_fraction; |
| 907 | } else { |
| 908 | r_result->travel = recovery + p_parameters.motion; |
| 909 | r_result->remainder = Vector3(); |
| 910 | r_result->collision_depth = 0.0f; |
| 911 | r_result->collision_safe_fraction = 1.0f; |
| 912 | r_result->collision_unsafe_fraction = 1.0f; |
| 913 | r_result->collision_count = 0; |
| 914 | } |
| 915 | |
| 916 | return collided; |
| 917 | } |