| | | 1 | | //======================================================================= |
| | | 2 | | // SweptSphereQueryWorker.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Colliders; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | using SwiftCollections.Query; |
| | | 14 | | |
| | | 15 | | namespace Gravitas.Queries; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Performs deterministic swept-sphere checks for one prepared segment. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class SweptSphereQueryWorker |
| | | 21 | | { |
| | 1 | 22 | | private static readonly Fixed64 BoundsTolerance = Fixed64.FromFraction(1, 4096); |
| | | 23 | | |
| | 10201 | 24 | | private readonly SwiftList<int> _meshTriangleBuffer = new(); |
| | 10201 | 25 | | private readonly ConvexSweepQueryWorker _finiteAxisConvexSweep = new(); |
| | | 26 | | |
| | | 27 | | private Vector3d _start; |
| | | 28 | | private Vector3d _end; |
| | | 29 | | private Vector3d _direction; |
| | | 30 | | private Vector3d _sweptBoundsMin; |
| | | 31 | | private Vector3d _sweptBoundsMax; |
| | | 32 | | private Fixed64 _length; |
| | | 33 | | private Fixed64 _lengthSqr; |
| | | 34 | | private Fixed64 _radius; |
| | | 35 | | private int _sweepDepth; |
| | | 36 | | |
| | | 37 | | internal int LastMeshTriangleCandidateCount { get; private set; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Prepares this worker for a swept sphere from <paramref name="start"/> to <paramref name="end"/>. |
| | | 41 | | /// </summary> |
| | | 42 | | public void Prepare(Vector3d start, Vector3d end, Fixed64 radius) |
| | | 43 | | { |
| | 14465 | 44 | | _start = start; |
| | 14465 | 45 | | _end = end; |
| | 14465 | 46 | | _radius = radius; |
| | | 47 | | |
| | 14465 | 48 | | if (!Vector3d.TrySubtract(end, start, out Vector3d segment) |
| | 14465 | 49 | | || !Vector3d.TryGetMagnitude(segment, out _length)) |
| | | 50 | | { |
| | 2 | 51 | | _length = Fixed64.Zero; |
| | 2 | 52 | | _lengthSqr = Fixed64.Zero; |
| | 2 | 53 | | _direction = Vector3d.Zero; |
| | 2 | 54 | | return; |
| | | 55 | | } |
| | | 56 | | |
| | 14463 | 57 | | _lengthSqr = segment.MagnitudeSquared; |
| | 14463 | 58 | | _direction = _length <= Fixed64.Epsilon ? Vector3d.Zero : segment.Normalized; |
| | 14463 | 59 | | SweepBoundsUtility.CreateSweptSphereBounds( |
| | 14463 | 60 | | start, |
| | 14463 | 61 | | end, |
| | 14463 | 62 | | radius, |
| | 14463 | 63 | | BoundsTolerance, |
| | 14463 | 64 | | out _sweptBoundsMin, |
| | 14463 | 65 | | out _sweptBoundsMax); |
| | 14463 | 66 | | _finiteAxisConvexSweep.PrepareSphereSource( |
| | 14463 | 67 | | start, |
| | 14463 | 68 | | radius, |
| | 14463 | 69 | | segment); |
| | 14463 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Checks a collider against the prepared swept sphere. |
| | | 74 | | /// </summary> |
| | | 75 | | public bool TrySweep( |
| | | 76 | | LSCollider collider, |
| | | 77 | | out Vector3d sphereCenterAtImpact, |
| | | 78 | | out Fixed64 impactDistance) |
| | | 79 | | { |
| | 9652 | 80 | | if (_sweepDepth == 0) |
| | 9623 | 81 | | LastMeshTriangleCandidateCount = 0; |
| | | 82 | | |
| | 9652 | 83 | | _sweepDepth++; |
| | 9652 | 84 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 9652 | 85 | | impactDistance = Fixed64.Zero; |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 9652 | 89 | | if (_length <= Fixed64.Epsilon |
| | 9652 | 90 | | || !SweepBoundsUtility.OverlapsInclusive(_sweptBoundsMin, _sweptBoundsMax, collider.BoundsMin, collider. |
| | | 91 | | { |
| | 1924 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | 7728 | 95 | | bool found = collider switch |
| | 7728 | 96 | | { |
| | 6701 | 97 | | LSSphereCollider sphere => TrySweepSphere(sphere.Center, sphere.ScaledRadius, _radius, out sphereCenterA |
| | 24 | 98 | | LSCapsuleCollider capsule => TrySweepCapsule(capsule, out sphereCenterAtImpact, out impactDistance), |
| | 650 | 99 | | LSCuboidCollider cuboid => TrySweepCuboid(cuboid, out sphereCenterAtImpact, out impactDistance), |
| | 34 | 100 | | LSCylinderCollider cylinder => TrySweepCylinder(cylinder, out sphereCenterAtImpact, out impactDistance), |
| | 19 | 101 | | LSConeCollider cone => TrySweepCone(cone, out sphereCenterAtImpact, out impactDistance), |
| | 277 | 102 | | LSMeshCollider mesh => TrySweepMesh(mesh, out sphereCenterAtImpact, out impactDistance), |
| | 18 | 103 | | LSCompoundCollider compound => TrySweepCompound(compound, out sphereCenterAtImpact, out impactDistance), |
| | 5 | 104 | | _ => false |
| | 7728 | 105 | | }; |
| | | 106 | | |
| | 7728 | 107 | | if (!found) |
| | | 108 | | { |
| | 90 | 109 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 90 | 110 | | impactDistance = Fixed64.Zero; |
| | | 111 | | } |
| | | 112 | | |
| | 7728 | 113 | | return found; |
| | | 114 | | } |
| | | 115 | | finally |
| | | 116 | | { |
| | 9652 | 117 | | _sweepDepth--; |
| | 9652 | 118 | | } |
| | 9652 | 119 | | } |
| | | 120 | | |
| | | 121 | | private bool TrySweepSphere( |
| | | 122 | | Vector3d center, |
| | | 123 | | Fixed64 radius, |
| | | 124 | | Fixed64 radiusExpansion, |
| | | 125 | | out Vector3d sphereCenterAtImpact, |
| | | 126 | | out Fixed64 impactDistance) => |
| | 6701 | 127 | | TrySweepSphere( |
| | 6701 | 128 | | _start, |
| | 6701 | 129 | | _end, |
| | 6701 | 130 | | _length, |
| | 6701 | 131 | | center, |
| | 6701 | 132 | | radius, |
| | 6701 | 133 | | radiusExpansion, |
| | 6701 | 134 | | out sphereCenterAtImpact, |
| | 6701 | 135 | | out impactDistance); |
| | | 136 | | |
| | | 137 | | private static bool TrySweepSphere( |
| | | 138 | | Vector3d start, |
| | | 139 | | Vector3d end, |
| | | 140 | | Fixed64 length, |
| | | 141 | | Vector3d center, |
| | | 142 | | Fixed64 radius, |
| | | 143 | | Fixed64 radiusExpansion, |
| | | 144 | | out Vector3d sphereCenterAtImpact, |
| | | 145 | | out Fixed64 impactDistance) |
| | | 146 | | { |
| | 7724 | 147 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 7724 | 148 | | impactDistance = Fixed64.Zero; |
| | | 149 | | |
| | 7724 | 150 | | var query = new FixedSegment(start, end); |
| | 7724 | 151 | | if (!query.TryGetSphereIntersectionDistanceInterval( |
| | 7724 | 152 | | new FixedBoundSphere(center, radius), |
| | 7724 | 153 | | radiusExpansion, |
| | 7724 | 154 | | length, |
| | 7724 | 155 | | out Fixed64 distance, |
| | 7724 | 156 | | out _, |
| | 7724 | 157 | | out _, |
| | 7724 | 158 | | out _)) |
| | 1060 | 159 | | return false; |
| | | 160 | | |
| | 6664 | 161 | | impactDistance = distance; |
| | 6664 | 162 | | sphereCenterAtImpact = query.GetPointAtDistance(distance, length); |
| | 6664 | 163 | | return true; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private bool TrySweepCapsule( |
| | | 167 | | LSCapsuleCollider capsule, |
| | | 168 | | out Vector3d sphereCenterAtImpact, |
| | | 169 | | out Fixed64 impactDistance) |
| | | 170 | | { |
| | 24 | 171 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 24 | 172 | | impactDistance = Fixed64.Zero; |
| | | 173 | | |
| | 24 | 174 | | var query = new FixedSegment(_start, _end); |
| | 24 | 175 | | if (!query.TryGetCapsuleIntersectionDistanceInterval( |
| | 24 | 176 | | capsule.Center, |
| | 24 | 177 | | capsule.Rotation, |
| | 24 | 178 | | capsule.AxisLength, |
| | 24 | 179 | | capsule.ScaledRadius, |
| | 24 | 180 | | _radius, |
| | 24 | 181 | | _length, |
| | 24 | 182 | | out Fixed64 entry, |
| | 24 | 183 | | out _, |
| | 24 | 184 | | out _, |
| | 24 | 185 | | out _)) |
| | | 186 | | { |
| | 2 | 187 | | return false; |
| | | 188 | | } |
| | | 189 | | |
| | 22 | 190 | | BuildFiniteAxisSweepHit(entry, out sphereCenterAtImpact, out impactDistance); |
| | 22 | 191 | | return true; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private bool TrySweepCylinder( |
| | | 195 | | LSCylinderCollider cylinder, |
| | | 196 | | out Vector3d sphereCenterAtImpact, |
| | | 197 | | out Fixed64 impactDistance) => |
| | 34 | 198 | | TrySweepCanonicalFiniteAxis( |
| | 34 | 199 | | cylinder, |
| | 34 | 200 | | out sphereCenterAtImpact, |
| | 34 | 201 | | out impactDistance); |
| | | 202 | | |
| | | 203 | | private bool TrySweepCone( |
| | | 204 | | LSConeCollider cone, |
| | | 205 | | out Vector3d sphereCenterAtImpact, |
| | | 206 | | out Fixed64 impactDistance) => |
| | 19 | 207 | | TrySweepCanonicalFiniteAxis( |
| | 19 | 208 | | cone, |
| | 19 | 209 | | out sphereCenterAtImpact, |
| | 19 | 210 | | out impactDistance); |
| | | 211 | | |
| | | 212 | | private bool TrySweepCanonicalFiniteAxis( |
| | | 213 | | LSCollider target, |
| | | 214 | | out Vector3d sphereCenterAtImpact, |
| | | 215 | | out Fixed64 impactDistance) |
| | | 216 | | { |
| | 53 | 217 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 53 | 218 | | impactDistance = Fixed64.Zero; |
| | 53 | 219 | | if (!_finiteAxisConvexSweep.TrySweepPreparedSource( |
| | 53 | 220 | | target, |
| | 53 | 221 | | out Physics3DHit hit)) |
| | | 222 | | { |
| | 11 | 223 | | return false; |
| | | 224 | | } |
| | | 225 | | |
| | 42 | 226 | | Fixed64 distance = _length - hit.Distance <= BoundsTolerance |
| | 42 | 227 | | ? _length |
| | 42 | 228 | | : hit.Distance; |
| | 42 | 229 | | BuildFiniteAxisSweepHit( |
| | 42 | 230 | | distance, |
| | 42 | 231 | | out sphereCenterAtImpact, |
| | 42 | 232 | | out impactDistance); |
| | 42 | 233 | | return true; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private bool TrySweepCuboid( |
| | | 237 | | LSCuboidCollider cuboid, |
| | | 238 | | out Vector3d sphereCenterAtImpact, |
| | | 239 | | out Fixed64 impactDistance) |
| | | 240 | | { |
| | 650 | 241 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 650 | 242 | | impactDistance = Fixed64.Zero; |
| | | 243 | | |
| | 650 | 244 | | var query = new FixedSegment(_start, _end); |
| | 650 | 245 | | if (!query.TryGetSweptSphereOrientedBoxIntersectionDistance( |
| | 650 | 246 | | cuboid.OrientedBox, |
| | 650 | 247 | | _radius, |
| | 650 | 248 | | _length, |
| | 650 | 249 | | out Fixed64 entry)) |
| | | 250 | | { |
| | 12 | 251 | | return false; |
| | | 252 | | } |
| | | 253 | | |
| | 638 | 254 | | BuildFiniteAxisSweepHit(entry, out sphereCenterAtImpact, out impactDistance); |
| | 638 | 255 | | return true; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private bool TrySweepMesh( |
| | | 259 | | LSMeshCollider mesh, |
| | | 260 | | out Vector3d sphereCenterAtImpact, |
| | | 261 | | out Fixed64 impactDistance) |
| | | 262 | | { |
| | 277 | 263 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 277 | 264 | | impactDistance = Fixed64.MaxValue; |
| | | 265 | | |
| | 277 | 266 | | CreateSweepBounds(_start, _end, _radius, out Vector3d min, out Vector3d max); |
| | 277 | 267 | | mesh.GetTrianglesInBounds(new FixedBoundVolume(min, max), _meshTriangleBuffer); |
| | 277 | 268 | | LastMeshTriangleCandidateCount += _meshTriangleBuffer.Count; |
| | | 269 | | |
| | 277 | 270 | | var startAnchor = new FixedPointAnchor( |
| | 277 | 271 | | _start, |
| | 277 | 272 | | FixedQuaternion.Identity, |
| | 277 | 273 | | Vector3d.Zero); |
| | 277 | 274 | | var endAnchor = new FixedPointAnchor( |
| | 277 | 275 | | _end, |
| | 277 | 276 | | FixedQuaternion.Identity, |
| | 277 | 277 | | Vector3d.Zero); |
| | 277 | 278 | | if (!startAnchor.TryGetLocalPointIn( |
| | 277 | 279 | | mesh.Mesh.Origin, |
| | 277 | 280 | | mesh.Mesh.Rotation, |
| | 277 | 281 | | out Vector3d localStart) |
| | 277 | 282 | | || !endAnchor.TryGetLocalPointIn( |
| | 277 | 283 | | mesh.Mesh.Origin, |
| | 277 | 284 | | mesh.Mesh.Rotation, |
| | 277 | 285 | | out Vector3d localEnd)) |
| | | 286 | | { |
| | 2 | 287 | | return false; |
| | | 288 | | } |
| | | 289 | | |
| | 275 | 290 | | Vector3d localDirection = |
| | 275 | 291 | | mesh.Mesh.Rotation.Inverse().Rotate(_direction); |
| | 275 | 292 | | bool found = false; |
| | 1404 | 293 | | for (int i = 0; i < _meshTriangleBuffer.Count; i++) |
| | | 294 | | { |
| | 427 | 295 | | int triangleIndex = _meshTriangleBuffer[i]; |
| | 427 | 296 | | mesh.Mesh.GetLocalTriangleVertices( |
| | 427 | 297 | | triangleIndex, |
| | 427 | 298 | | out Vector3d first, |
| | 427 | 299 | | out Vector3d second, |
| | 427 | 300 | | out Vector3d third); |
| | 427 | 301 | | Vector3d normal = mesh.Mesh.GetScaledLocalFaceNormal(triangleIndex); |
| | 427 | 302 | | found |= TryKeepEarlierSweep( |
| | 427 | 303 | | TrySweepTriangle( |
| | 427 | 304 | | first, |
| | 427 | 305 | | second, |
| | 427 | 306 | | third, |
| | 427 | 307 | | normal, |
| | 427 | 308 | | localStart, |
| | 427 | 309 | | localEnd, |
| | 427 | 310 | | localDirection, |
| | 427 | 311 | | out Vector3d triangleHit, |
| | 427 | 312 | | out Fixed64 triangleDistance), |
| | 427 | 313 | | triangleHit, |
| | 427 | 314 | | triangleDistance, |
| | 427 | 315 | | ref sphereCenterAtImpact, |
| | 427 | 316 | | ref impactDistance); |
| | | 317 | | } |
| | | 318 | | |
| | 275 | 319 | | if (found) |
| | | 320 | | { |
| | 262 | 321 | | sphereCenterAtImpact = new FixedSegment( |
| | 262 | 322 | | _start, |
| | 262 | 323 | | _end).GetPointAtDistance( |
| | 262 | 324 | | impactDistance, |
| | 262 | 325 | | _length); |
| | | 326 | | } |
| | 275 | 327 | | return found; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private bool TrySweepCompound( |
| | | 331 | | LSCompoundCollider compound, |
| | | 332 | | out Vector3d sphereCenterAtImpact, |
| | | 333 | | out Fixed64 impactDistance) |
| | | 334 | | { |
| | 18 | 335 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 18 | 336 | | impactDistance = Fixed64.MaxValue; |
| | | 337 | | |
| | 18 | 338 | | bool found = false; |
| | 94 | 339 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 340 | | { |
| | 29 | 341 | | found |= TryKeepEarlierSweep( |
| | 29 | 342 | | TrySweep(compound.GetPartCollider(i), out Vector3d partHit, out Fixed64 partDistance), |
| | 29 | 343 | | partHit, |
| | 29 | 344 | | partDistance, |
| | 29 | 345 | | ref sphereCenterAtImpact, |
| | 29 | 346 | | ref impactDistance); |
| | | 347 | | } |
| | | 348 | | |
| | 18 | 349 | | return found; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | private bool TrySweepTriangle( |
| | | 353 | | Vector3d first, |
| | | 354 | | Vector3d second, |
| | | 355 | | Vector3d third, |
| | | 356 | | Vector3d normal, |
| | | 357 | | Vector3d start, |
| | | 358 | | Vector3d end, |
| | | 359 | | Vector3d direction, |
| | | 360 | | out Vector3d sphereCenterAtImpact, |
| | | 361 | | out Fixed64 impactDistance) |
| | | 362 | | { |
| | 427 | 363 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 427 | 364 | | impactDistance = Fixed64.MaxValue; |
| | | 365 | | |
| | 427 | 366 | | var triangle = new FixedTriangle(first, second, third); |
| | 427 | 367 | | Vector3d closestAtStart = triangle.ClosestPoint(start); |
| | 427 | 368 | | if (Vector3d.DistanceSquared(start, closestAtStart) <= _radius * _radius) |
| | | 369 | | { |
| | 86 | 370 | | sphereCenterAtImpact = start; |
| | 86 | 371 | | impactDistance = Fixed64.Zero; |
| | 86 | 372 | | return true; |
| | | 373 | | } |
| | | 374 | | |
| | 341 | 375 | | bool found = false; |
| | 341 | 376 | | found |= TryKeepEarlierSweep( |
| | 341 | 377 | | TrySweepTriangleFace(triangle, normal, _radius, start, direction, out Vector3d frontHit, out Fixed64 frontDi |
| | 341 | 378 | | frontHit, |
| | 341 | 379 | | frontDistance, |
| | 341 | 380 | | ref sphereCenterAtImpact, |
| | 341 | 381 | | ref impactDistance); |
| | 341 | 382 | | found |= TryKeepEarlierSweep( |
| | 341 | 383 | | TrySweepTriangleFace(triangle, normal, -_radius, start, direction, out Vector3d backHit, out Fixed64 backDis |
| | 341 | 384 | | backHit, |
| | 341 | 385 | | backDistance, |
| | 341 | 386 | | ref sphereCenterAtImpact, |
| | 341 | 387 | | ref impactDistance); |
| | 341 | 388 | | found |= TryKeepEarlierSweep( |
| | 341 | 389 | | TrySweepTriangleEdge(first, second, start, end, out Vector3d firstEdgeHit, out Fixed64 firstEdgeDistance), |
| | 341 | 390 | | firstEdgeHit, |
| | 341 | 391 | | firstEdgeDistance, |
| | 341 | 392 | | ref sphereCenterAtImpact, |
| | 341 | 393 | | ref impactDistance); |
| | 341 | 394 | | found |= TryKeepEarlierSweep( |
| | 341 | 395 | | TrySweepTriangleEdge(second, third, start, end, out Vector3d secondEdgeHit, out Fixed64 secondEdgeDistance), |
| | 341 | 396 | | secondEdgeHit, |
| | 341 | 397 | | secondEdgeDistance, |
| | 341 | 398 | | ref sphereCenterAtImpact, |
| | 341 | 399 | | ref impactDistance); |
| | 341 | 400 | | found |= TryKeepEarlierSweep( |
| | 341 | 401 | | TrySweepTriangleEdge(third, first, start, end, out Vector3d thirdEdgeHit, out Fixed64 thirdEdgeDistance), |
| | 341 | 402 | | thirdEdgeHit, |
| | 341 | 403 | | thirdEdgeDistance, |
| | 341 | 404 | | ref sphereCenterAtImpact, |
| | 341 | 405 | | ref impactDistance); |
| | 341 | 406 | | found |= TryKeepEarlierSweep( |
| | 341 | 407 | | TrySweepSphere(start, end, _length, first, Fixed64.Zero, _radius, out Vector3d firstVertexHit, out Fixed64 f |
| | 341 | 408 | | firstVertexHit, |
| | 341 | 409 | | firstVertexDistance, |
| | 341 | 410 | | ref sphereCenterAtImpact, |
| | 341 | 411 | | ref impactDistance); |
| | 341 | 412 | | found |= TryKeepEarlierSweep( |
| | 341 | 413 | | TrySweepSphere(start, end, _length, second, Fixed64.Zero, _radius, out Vector3d secondVertexHit, out Fixed64 |
| | 341 | 414 | | secondVertexHit, |
| | 341 | 415 | | secondVertexDistance, |
| | 341 | 416 | | ref sphereCenterAtImpact, |
| | 341 | 417 | | ref impactDistance); |
| | 341 | 418 | | found |= TryKeepEarlierSweep( |
| | 341 | 419 | | TrySweepSphere(start, end, _length, third, Fixed64.Zero, _radius, out Vector3d thirdVertexHit, out Fixed64 t |
| | 341 | 420 | | thirdVertexHit, |
| | 341 | 421 | | thirdVertexDistance, |
| | 341 | 422 | | ref sphereCenterAtImpact, |
| | 341 | 423 | | ref impactDistance); |
| | | 424 | | |
| | 341 | 425 | | return found; |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | private bool TrySweepTriangleFace( |
| | | 429 | | FixedTriangle triangle, |
| | | 430 | | Vector3d normal, |
| | | 431 | | Fixed64 signedRadius, |
| | | 432 | | Vector3d start, |
| | | 433 | | Vector3d direction, |
| | | 434 | | out Vector3d sphereCenterAtImpact, |
| | | 435 | | out Fixed64 impactDistance) |
| | | 436 | | { |
| | 682 | 437 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 682 | 438 | | impactDistance = Fixed64.Zero; |
| | | 439 | | |
| | 682 | 440 | | Fixed64 denominator = Vector3d.Dot(direction, normal); |
| | 682 | 441 | | if (denominator.Abs() <= Fixed64.Epsilon) |
| | 126 | 442 | | return false; |
| | | 443 | | |
| | 556 | 444 | | Fixed64 signedStartDistance = Vector3d.Dot(start - triangle.A, normal); |
| | 556 | 445 | | Fixed64 distance = (signedRadius - signedStartDistance) / denominator; |
| | 556 | 446 | | if (distance < Fixed64.Zero || distance > _length) |
| | 45 | 447 | | return false; |
| | | 448 | | |
| | 511 | 449 | | Vector3d center = start + direction * distance; |
| | 511 | 450 | | Vector3d pointOnPlane = center - normal * signedRadius; |
| | 511 | 451 | | if (!triangle.ContainsProjection(pointOnPlane)) |
| | 17 | 452 | | return false; |
| | | 453 | | |
| | 494 | 454 | | sphereCenterAtImpact = center; |
| | 494 | 455 | | impactDistance = distance; |
| | 494 | 456 | | return true; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | private bool TrySweepTriangleEdge( |
| | | 460 | | Vector3d edgeStart, |
| | | 461 | | Vector3d edgeEnd, |
| | | 462 | | Vector3d start, |
| | | 463 | | Vector3d end, |
| | | 464 | | out Vector3d sphereCenterAtImpact, |
| | | 465 | | out Fixed64 impactDistance) |
| | | 466 | | { |
| | 1023 | 467 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 1023 | 468 | | impactDistance = Fixed64.MaxValue; |
| | | 469 | | |
| | 1023 | 470 | | var query = new FixedSegment(start, end); |
| | 1023 | 471 | | var edgeAxis = new FixedSegment(edgeStart, edgeEnd); |
| | 1023 | 472 | | if (!query.TryGetFiniteCylinderIntersectionDistanceInterval( |
| | 1023 | 473 | | edgeAxis, |
| | 1023 | 474 | | Fixed64.Zero, |
| | 1023 | 475 | | _radius, |
| | 1023 | 476 | | _length, |
| | 1023 | 477 | | out Fixed64 entry, |
| | 1023 | 478 | | out _, |
| | 1023 | 479 | | out _, |
| | 1023 | 480 | | out _)) |
| | | 481 | | { |
| | 886 | 482 | | return false; |
| | | 483 | | } |
| | | 484 | | |
| | 137 | 485 | | impactDistance = entry; |
| | 137 | 486 | | sphereCenterAtImpact = query.GetPointAtDistance(entry, _length); |
| | 137 | 487 | | return true; |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | private void BuildFiniteAxisSweepHit( |
| | | 491 | | Fixed64 distance, |
| | | 492 | | out Vector3d sphereCenterAtImpact, |
| | | 493 | | out Fixed64 impactDistance) |
| | | 494 | | { |
| | 702 | 495 | | impactDistance = distance; |
| | 702 | 496 | | sphereCenterAtImpact = new FixedSegment(_start, _end) |
| | 702 | 497 | | .GetPointAtDistance(distance, _length); |
| | 702 | 498 | | } |
| | | 499 | | |
| | | 500 | | private static bool TryKeepEarlierSweep( |
| | | 501 | | bool candidateFound, |
| | | 502 | | Vector3d candidateCenter, |
| | | 503 | | Fixed64 candidateDistance, |
| | | 504 | | ref Vector3d sphereCenterAtImpact, |
| | | 505 | | ref Fixed64 impactDistance) |
| | | 506 | | { |
| | 3184 | 507 | | if (!candidateFound || candidateDistance >= impactDistance) |
| | 2375 | 508 | | return false; |
| | | 509 | | |
| | 809 | 510 | | sphereCenterAtImpact = candidateCenter; |
| | 809 | 511 | | impactDistance = candidateDistance; |
| | 809 | 512 | | return true; |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | private static void CreateSweepBounds(Vector3d start, Vector3d end, Fixed64 radius, out Vector3d min, out Vector3d m |
| | | 516 | | { |
| | 277 | 517 | | Vector3d radiusExtents = Vector3d.One * radius; |
| | 277 | 518 | | min = Vector3d.Min(start, end) - radiusExtents; |
| | 277 | 519 | | max = Vector3d.Max(start, end) + radiusExtents; |
| | 277 | 520 | | } |
| | | 521 | | } |