| | | 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 System; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using Gravitas.Colliders; |
| | | 12 | | using Gravitas.CollisionHandling; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Query; |
| | | 15 | | |
| | | 16 | | namespace Gravitas.Queries; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Performs deterministic swept-sphere checks for one prepared segment. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class SweptSphereQueryWorker |
| | | 22 | | { |
| | 1 | 23 | | private static readonly Fixed64 BoundsTolerance = Fixed64.FromFraction(1, 4096); |
| | | 24 | | |
| | 18643 | 25 | | private readonly SwiftList<int> _meshTriangleBuffer = new(); |
| | 18643 | 26 | | private readonly ConvexSweepQueryWorker _finiteAxisConvexSweep = new(); |
| | | 27 | | |
| | | 28 | | private Vector3d _start; |
| | | 29 | | private Vector3d _end; |
| | | 30 | | private Vector3d _sweptBoundsMin; |
| | | 31 | | private Vector3d _sweptBoundsMax; |
| | | 32 | | private Fixed64 _length; |
| | | 33 | | private Fixed64 _radius; |
| | | 34 | | private int _sweepDepth; |
| | | 35 | | |
| | | 36 | | internal int LastMeshTriangleCandidateCount { get; private set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Prepares this worker for a swept sphere from <paramref name="start"/> to <paramref name="end"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | public void Prepare(Vector3d start, Vector3d end, Fixed64 radius) |
| | | 42 | | { |
| | 22048 | 43 | | _start = start; |
| | 22048 | 44 | | _end = end; |
| | 22048 | 45 | | _radius = radius; |
| | | 46 | | |
| | 22048 | 47 | | if (!Vector3d.TrySubtract(end, start, out Vector3d segment) |
| | 22048 | 48 | | || !Vector3d.TryGetMagnitude(segment, out _length)) |
| | | 49 | | { |
| | 2 | 50 | | _length = Fixed64.Zero; |
| | 2 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | 22046 | 54 | | SweepBoundsUtility.CreateSweptSphereBounds( |
| | 22046 | 55 | | start, |
| | 22046 | 56 | | end, |
| | 22046 | 57 | | radius, |
| | 22046 | 58 | | BoundsTolerance, |
| | 22046 | 59 | | out _sweptBoundsMin, |
| | 22046 | 60 | | out _sweptBoundsMax); |
| | 22046 | 61 | | _finiteAxisConvexSweep.PrepareSphereSource( |
| | 22046 | 62 | | start, |
| | 22046 | 63 | | radius, |
| | 22046 | 64 | | segment); |
| | 22046 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Checks a collider against the prepared swept sphere. |
| | | 69 | | /// </summary> |
| | | 70 | | public bool TrySweep( |
| | | 71 | | LSCollider collider, |
| | | 72 | | out Vector3d sphereCenterAtImpact, |
| | | 73 | | out Fixed64 impactDistance) |
| | | 74 | | { |
| | 20902 | 75 | | if (_sweepDepth == 0) |
| | 19607 | 76 | | LastMeshTriangleCandidateCount = 0; |
| | | 77 | | |
| | 20902 | 78 | | _sweepDepth++; |
| | 20902 | 79 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 20902 | 80 | | impactDistance = Fixed64.Zero; |
| | | 81 | | |
| | | 82 | | try |
| | | 83 | | { |
| | 20902 | 84 | | if (_length <= Fixed64.Epsilon |
| | 20902 | 85 | | || !SweepBoundsUtility.OverlapsInclusive(_sweptBoundsMin, _sweptBoundsMax, collider.BoundsMin, collider. |
| | | 86 | | { |
| | 6067 | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | |
| | 14835 | 90 | | bool found = collider switch |
| | 14835 | 91 | | { |
| | 8475 | 92 | | LSSphereCollider sphere => TrySweepSphere(sphere.Center, sphere.ScaledRadius, _radius, out sphereCenterA |
| | 41 | 93 | | LSCapsuleCollider capsule => TrySweepCapsule(capsule, out sphereCenterAtImpact, out impactDistance), |
| | 1169 | 94 | | LSCuboidCollider cuboid => TrySweepCuboid(cuboid, out sphereCenterAtImpact, out impactDistance), |
| | 702 | 95 | | LSCylinderCollider cylinder => TrySweepCylinder(cylinder, out sphereCenterAtImpact, out impactDistance), |
| | 687 | 96 | | LSConeCollider cone => TrySweepCone(cone, out sphereCenterAtImpact, out impactDistance), |
| | 2526 | 97 | | LSMeshCollider mesh => TrySweepMesh(mesh, out sphereCenterAtImpact, out impactDistance), |
| | 1230 | 98 | | LSCompoundCollider compound => TrySweepCompound(compound, out sphereCenterAtImpact, out impactDistance), |
| | 5 | 99 | | _ => false |
| | 14835 | 100 | | }; |
| | | 101 | | |
| | 14835 | 102 | | if (!found) |
| | | 103 | | { |
| | 766 | 104 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 766 | 105 | | impactDistance = Fixed64.Zero; |
| | | 106 | | } |
| | | 107 | | |
| | 14835 | 108 | | return found; |
| | | 109 | | } |
| | | 110 | | finally |
| | | 111 | | { |
| | 20902 | 112 | | _sweepDepth--; |
| | 20902 | 113 | | } |
| | 20902 | 114 | | } |
| | | 115 | | |
| | | 116 | | private bool TrySweepSphere( |
| | | 117 | | Vector3d center, |
| | | 118 | | Fixed64 radius, |
| | | 119 | | Fixed64 radiusExpansion, |
| | | 120 | | out Vector3d sphereCenterAtImpact, |
| | | 121 | | out Fixed64 impactDistance) => |
| | 8475 | 122 | | TrySweepSphere( |
| | 8475 | 123 | | _start, |
| | 8475 | 124 | | _end, |
| | 8475 | 125 | | _length, |
| | 8475 | 126 | | center, |
| | 8475 | 127 | | radius, |
| | 8475 | 128 | | radiusExpansion, |
| | 8475 | 129 | | out sphereCenterAtImpact, |
| | 8475 | 130 | | out impactDistance); |
| | | 131 | | |
| | | 132 | | private static bool TrySweepSphere( |
| | | 133 | | Vector3d start, |
| | | 134 | | Vector3d end, |
| | | 135 | | Fixed64 length, |
| | | 136 | | Vector3d center, |
| | | 137 | | Fixed64 radius, |
| | | 138 | | Fixed64 radiusExpansion, |
| | | 139 | | out Vector3d sphereCenterAtImpact, |
| | | 140 | | out Fixed64 impactDistance) |
| | | 141 | | { |
| | 15039 | 142 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 15039 | 143 | | impactDistance = Fixed64.Zero; |
| | | 144 | | |
| | 15039 | 145 | | var query = new FixedSegment(start, end); |
| | 15039 | 146 | | if (!query.TryGetSphereIntersectionDistanceInterval( |
| | 15039 | 147 | | new FixedBoundSphere(center, radius), |
| | 15039 | 148 | | radiusExpansion, |
| | 15039 | 149 | | length, |
| | 15039 | 150 | | out Fixed64 distance, |
| | 15039 | 151 | | out _, |
| | 15039 | 152 | | out _, |
| | 15039 | 153 | | out _)) |
| | 7060 | 154 | | return false; |
| | | 155 | | |
| | 7979 | 156 | | impactDistance = distance; |
| | 7979 | 157 | | sphereCenterAtImpact = query.GetPointAtDistance(distance, length); |
| | 7979 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private bool TrySweepCapsule( |
| | | 162 | | LSCapsuleCollider capsule, |
| | | 163 | | out Vector3d sphereCenterAtImpact, |
| | | 164 | | out Fixed64 impactDistance) |
| | | 165 | | { |
| | 41 | 166 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 41 | 167 | | impactDistance = Fixed64.Zero; |
| | | 168 | | |
| | 41 | 169 | | var query = new FixedSegment(_start, _end); |
| | 41 | 170 | | if (!query.TryGetCapsuleIntersectionDistanceInterval( |
| | 41 | 171 | | capsule.Center, |
| | 41 | 172 | | capsule.Rotation, |
| | 41 | 173 | | capsule.AxisLength, |
| | 41 | 174 | | capsule.ScaledRadius, |
| | 41 | 175 | | _radius, |
| | 41 | 176 | | _length, |
| | 41 | 177 | | out Fixed64 entry, |
| | 41 | 178 | | out _, |
| | 41 | 179 | | out _, |
| | 41 | 180 | | out _)) |
| | | 181 | | { |
| | 2 | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | 39 | 185 | | BuildFiniteAxisSweepHit(entry, out sphereCenterAtImpact, out impactDistance); |
| | 39 | 186 | | return true; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | private bool TrySweepCylinder( |
| | | 190 | | LSCylinderCollider cylinder, |
| | | 191 | | out Vector3d sphereCenterAtImpact, |
| | | 192 | | out Fixed64 impactDistance) => |
| | 702 | 193 | | TrySweepSupportMapped( |
| | 702 | 194 | | cylinder, |
| | 702 | 195 | | out sphereCenterAtImpact, |
| | 702 | 196 | | out impactDistance); |
| | | 197 | | |
| | | 198 | | private bool TrySweepCone( |
| | | 199 | | LSConeCollider cone, |
| | | 200 | | out Vector3d sphereCenterAtImpact, |
| | | 201 | | out Fixed64 impactDistance) => |
| | 687 | 202 | | TrySweepSupportMapped( |
| | 687 | 203 | | cone, |
| | 687 | 204 | | out sphereCenterAtImpact, |
| | 687 | 205 | | out impactDistance); |
| | | 206 | | |
| | | 207 | | private bool TrySweepSupportMapped( |
| | | 208 | | LSCollider target, |
| | | 209 | | out Vector3d sphereCenterAtImpact, |
| | | 210 | | out Fixed64 impactDistance) |
| | | 211 | | { |
| | 1389 | 212 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 1389 | 213 | | impactDistance = Fixed64.Zero; |
| | 1389 | 214 | | bool found = _finiteAxisConvexSweep.TrySweepPreparedSource( |
| | 1389 | 215 | | target, |
| | 1389 | 216 | | out Physics3DHit hit); |
| | 1389 | 217 | | LastMeshTriangleCandidateCount += _finiteAxisConvexSweep.LastMeshTriangleCandidateCount; |
| | 1389 | 218 | | if (!found) |
| | | 219 | | { |
| | 12 | 220 | | return false; |
| | | 221 | | } |
| | | 222 | | |
| | 1377 | 223 | | Fixed64 distance = _length - hit.Distance <= BoundsTolerance |
| | 1377 | 224 | | ? _length |
| | 1377 | 225 | | : hit.Distance; |
| | 1377 | 226 | | BuildFiniteAxisSweepHit( |
| | 1377 | 227 | | distance, |
| | 1377 | 228 | | out sphereCenterAtImpact, |
| | 1377 | 229 | | out impactDistance); |
| | 1377 | 230 | | return true; |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | private bool TrySweepCuboid( |
| | | 234 | | LSCuboidCollider cuboid, |
| | | 235 | | out Vector3d sphereCenterAtImpact, |
| | | 236 | | out Fixed64 impactDistance) |
| | | 237 | | { |
| | 1169 | 238 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 1169 | 239 | | impactDistance = Fixed64.Zero; |
| | | 240 | | |
| | 1169 | 241 | | var query = new FixedSegment(_start, _end); |
| | 1169 | 242 | | if (!query.TryGetSweptSphereOrientedBoxIntersectionDistance( |
| | 1169 | 243 | | cuboid.OrientedBox, |
| | 1169 | 244 | | _radius, |
| | 1169 | 245 | | _length, |
| | 1169 | 246 | | out Fixed64 entry)) |
| | | 247 | | { |
| | 20 | 248 | | return false; |
| | | 249 | | } |
| | | 250 | | |
| | 1149 | 251 | | BuildFiniteAxisSweepHit(entry, out sphereCenterAtImpact, out impactDistance); |
| | 1149 | 252 | | return true; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | internal bool TrySweepMeshForContinuousCollision(LSMeshCollider mesh, |
| | | 256 | | ContinuousSurfaceTriangleAdmission admit, out Physics3DHit hit) |
| | | 257 | | { |
| | 342 | 258 | | LastMeshTriangleCandidateCount = 0; |
| | 342 | 259 | | return TrySweepMesh(mesh, out _, out _, out hit, admit); |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | private bool TrySweepMesh( |
| | | 263 | | LSMeshCollider mesh, |
| | | 264 | | out Vector3d sphereCenterAtImpact, |
| | 2526 | 265 | | out Fixed64 impactDistance) => TrySweepMesh(mesh, out sphereCenterAtImpact, out impactDistance, out _, null); |
| | | 266 | | |
| | | 267 | | private bool TrySweepMesh(LSMeshCollider mesh, out Vector3d sphereCenterAtImpact, out Fixed64 impactDistance, |
| | | 268 | | out Physics3DHit admittedHit, ContinuousSurfaceTriangleAdmission? admit) |
| | | 269 | | { |
| | 2868 | 270 | | admittedHit = default; |
| | 2868 | 271 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 2868 | 272 | | impactDistance = Fixed64.MaxValue; |
| | | 273 | | |
| | 2868 | 274 | | CreateSweepBounds(_start, _end, _radius, out Vector3d min, out Vector3d max); |
| | 2868 | 275 | | mesh.GetTrianglesInBounds(new FixedBoundVolume(min, max), _meshTriangleBuffer); |
| | 2868 | 276 | | LastMeshTriangleCandidateCount += _meshTriangleBuffer.Count; |
| | | 277 | | |
| | 2868 | 278 | | var startAnchor = new FixedPointAnchor( |
| | 2868 | 279 | | _start, |
| | 2868 | 280 | | FixedQuaternion.Identity, |
| | 2868 | 281 | | Vector3d.Zero); |
| | 2868 | 282 | | var endAnchor = new FixedPointAnchor( |
| | 2868 | 283 | | _end, |
| | 2868 | 284 | | FixedQuaternion.Identity, |
| | 2868 | 285 | | Vector3d.Zero); |
| | 2868 | 286 | | bool rigid = mesh.Mesh.GeometryFrame.IsRigid; |
| | 2868 | 287 | | Vector3d chartOrigin = rigid ? mesh.Mesh.GeometryFrame.RigidOrigin : mesh.Mesh.Origin; |
| | 2868 | 288 | | FixedQuaternion chartRotation = rigid ? mesh.Mesh.GeometryFrame.Rotation : FixedQuaternion.Identity; |
| | 2868 | 289 | | if (!startAnchor.TryGetLocalPointIn( |
| | 2868 | 290 | | chartOrigin, |
| | 2868 | 291 | | chartRotation, |
| | 2868 | 292 | | out Vector3d localStart) |
| | 2868 | 293 | | || !endAnchor.TryGetLocalPointIn( |
| | 2868 | 294 | | chartOrigin, |
| | 2868 | 295 | | chartRotation, |
| | 2868 | 296 | | out Vector3d localEnd)) |
| | | 297 | | { |
| | 2 | 298 | | return false; |
| | | 299 | | } |
| | | 300 | | |
| | 2866 | 301 | | ReadOnlySpan<int> corners = default; |
| | 2866 | 302 | | if (admit == null && _meshTriangleBuffer.Count > 2 |
| | 2866 | 303 | | && _meshTriangleBuffer.Count == mesh.Mesh.TriangleCount && mesh.Mesh.HasSingleConvexCoplanarPatch) |
| | | 304 | | { |
| | 69 | 305 | | CreateSweepBounds(_start, _start, _radius, out Vector3d initialMin, out Vector3d initialMax); |
| | 69 | 306 | | if (!SweepBoundsUtility.OverlapsInclusive(initialMin, initialMax, mesh.Bounds.Min, mesh.Bounds.Max)) |
| | 60 | 307 | | corners = mesh.Mesh.GetConvexCoplanarPatchCornerVertexIndices(0); |
| | | 308 | | } |
| | | 309 | | |
| | 2866 | 310 | | bool found = false; |
| | 2866 | 311 | | int triangleCount = corners.IsEmpty ? _meshTriangleBuffer.Count : corners.Length - 2; |
| | 17340 | 312 | | for (int i = 0; i < triangleCount; i++) |
| | | 313 | | { |
| | 5804 | 314 | | int triangleIndex = corners.IsEmpty ? _meshTriangleBuffer[i] : -1; |
| | | 315 | | Vector3d first, second, third, normal; |
| | 5804 | 316 | | if (corners.IsEmpty) |
| | | 317 | | { |
| | 5684 | 318 | | mesh.Mesh.GetGeometryTriangleVertices(triangleIndex, out first, out second, out third); |
| | 5684 | 319 | | normal = mesh.Mesh.GetScaledLocalFaceNormal(triangleIndex); |
| | | 320 | | } |
| | | 321 | | else |
| | | 322 | | { |
| | | 323 | | // The certified corner fan covers the same finite surface; CCD keeps authored identities. |
| | 120 | 324 | | var vertices = mesh.Mesh.GeometryVertices; |
| | 120 | 325 | | first = vertices[corners[0]]; |
| | 120 | 326 | | second = vertices[corners[i + 1]]; |
| | 120 | 327 | | third = vertices[corners[i + 2]]; |
| | 120 | 328 | | normal = mesh.Mesh.GetScaledLocalFaceNormal(0); |
| | | 329 | | } |
| | | 330 | | bool triangleFound; |
| | | 331 | | Fixed64 triangleDistance; |
| | 5804 | 332 | | if (!rigid) |
| | | 333 | | { |
| | 304 | 334 | | var authoredTriangle = new FixedTriangle(first, second, third); |
| | | 335 | | // Retain the analytic finite roots whenever the physical |
| | | 336 | | // triangle is exactly representable in this translated chart. |
| | | 337 | | // A failed exact division must never round its input geometry. |
| | 304 | 338 | | if (!mesh.Mesh.GeometryFrame.GetPoint(first).TryGetExactLocalPointIn(chartOrigin, chartRotation, out fir |
| | 304 | 339 | | || !mesh.Mesh.GeometryFrame.GetPoint(second).TryGetExactLocalPointIn(chartOrigin, chartRotation, out |
| | 304 | 340 | | || !mesh.Mesh.GeometryFrame.GetPoint(third).TryGetExactLocalPointIn(chartOrigin, chartRotation, out |
| | | 341 | | { |
| | 63 | 342 | | triangleFound = WideTriangleRelations.TryGetSweptSphereDistance(authoredTriangle, |
| | 63 | 343 | | mesh.Mesh.GeometryFrame, new FixedSegment(_start, _end), _length, _radius, |
| | 63 | 344 | | out triangleDistance); |
| | 63 | 345 | | goto SelectTriangle; |
| | | 346 | | } |
| | 241 | 347 | | normal = WideTriangleRelations.GetScaledTriangleLocalNormal(authoredTriangle, mesh.Mesh.GeometryFrame) |
| | 241 | 348 | | .Transform(mesh.Mesh.GeometryFrame.GeometryBasis).GetNormal(false); |
| | | 349 | | } |
| | 5741 | 350 | | triangleFound = TrySweepTriangle( |
| | 5741 | 351 | | first, |
| | 5741 | 352 | | second, |
| | 5741 | 353 | | third, |
| | 5741 | 354 | | normal, |
| | 5741 | 355 | | localStart, |
| | 5741 | 356 | | localEnd, |
| | 5741 | 357 | | exactFaceProjection: !corners.IsEmpty, |
| | 5741 | 358 | | out _, out triangleDistance); |
| | | 359 | | SelectTriangle: |
| | 5804 | 360 | | if (!triangleFound || triangleDistance >= impactDistance) continue; |
| | 2835 | 361 | | Physics3DHit candidate = default; |
| | 2835 | 362 | | if (admit != null) |
| | | 363 | | { |
| | 365 | 364 | | Vector3d center = new FixedSegment(_start, _end).GetPointAtDistance(triangleDistance, _length); |
| | 365 | 365 | | mesh.Mesh.GetGeometryTriangleVertices(triangleIndex, out var a, out var b, out var c); |
| | | 366 | | // A finite closest point stays in the admitted small physical |
| | | 367 | | // mesh envelope. Its bounds center supplies a nearby chart even |
| | | 368 | | // at clipped scalar faces; the authored origin may be far away. |
| | 365 | 369 | | bool represented = WideTriangleRelations.TryGetClosestPointAnchor(new(a, b, c), mesh.Mesh.GeometryFrame, |
| | 365 | 370 | | center, mesh.Bounds.Center, out FixedPointAnchor anchor); |
| | | 371 | | System.Diagnostics.Debug.Assert(represented); |
| | 365 | 372 | | RationalPoint3d point = RationalPoint3d.FromAnchor(anchor); |
| | 365 | 373 | | WideDirection3d axis = WideTriangleRelations.GetScaledPointDifference(RationalPoint3d.FromScalarPoint(ce |
| | 365 | 374 | | point, WideArithmetic.TruncateSigned320By64(point.Denominator)); |
| | 365 | 375 | | RationalPoint3d centerPoint = RationalPoint3d.FromScalarPoint(center); |
| | 365 | 376 | | if (axis.IsZero || WideTriangleRelations.ContainsProjection(new(a, b, c), mesh.Mesh.GeometryFrame, cente |
| | | 377 | | { |
| | | 378 | | // A face projection has its exact plane normal even when |
| | | 379 | | // rounding the final point moves it onto an authored seam. |
| | 314 | 380 | | axis = WideTriangleRelations.GetScaledTriangleLocalNormal(new(a, b, c), mesh.Mesh.GeometryFrame) |
| | 314 | 381 | | .Transform(mesh.Mesh.GeometryFrame.GeometryBasis); |
| | 314 | 382 | | var sweep = new WideAxis3(Signed320.ExtendValue(WideArithmetic.Difference(_end.X, _start.X)), |
| | 314 | 383 | | Signed320.ExtendValue(WideArithmetic.Difference(_end.Y, _start.Y)), |
| | 314 | 384 | | Signed320.ExtendValue(WideArithmetic.Difference(_end.Z, _start.Z))); |
| | 314 | 385 | | int side = WideDirection3d.Dot(axis, WideTriangleRelations.GetScaledPointDifference(centerPoint, |
| | 314 | 386 | | mesh.Mesh.GeometryFrame.GetPoint(a), mesh.Mesh.GeometryFrame.RotationDenominator)).Sign; |
| | 317 | 387 | | if (side < 0 || side == 0 && WideDirection3d.Dot(axis, sweep).Sign > 0) axis = -axis; |
| | | 388 | | } |
| | 365 | 389 | | candidate = new(mesh, new ContactAnchor(anchor), axis.GetNormal(false), triangleDistance, (_end - _start |
| | 365 | 390 | | if (!admit(triangleIndex, ref candidate, axis)) continue; |
| | | 391 | | } |
| | 2637 | 392 | | found = true; |
| | 2637 | 393 | | impactDistance = triangleDistance; |
| | 2637 | 394 | | admittedHit = candidate; |
| | | 395 | | } |
| | | 396 | | |
| | 2866 | 397 | | if (found) |
| | | 398 | | { |
| | 2588 | 399 | | sphereCenterAtImpact = new FixedSegment( |
| | 2588 | 400 | | _start, |
| | 2588 | 401 | | _end).GetPointAtDistance( |
| | 2588 | 402 | | impactDistance, |
| | 2588 | 403 | | _length); |
| | | 404 | | } |
| | 2866 | 405 | | return found; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | private bool TrySweepCompound( |
| | | 409 | | LSCompoundCollider compound, |
| | | 410 | | out Vector3d sphereCenterAtImpact, |
| | | 411 | | out Fixed64 impactDistance) |
| | | 412 | | { |
| | 1230 | 413 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 1230 | 414 | | impactDistance = Fixed64.MaxValue; |
| | | 415 | | |
| | 1230 | 416 | | bool found = false; |
| | 5050 | 417 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 418 | | { |
| | 1295 | 419 | | found |= TryKeepEarlierSweep( |
| | 1295 | 420 | | TrySweep(compound.GetPartCollider(i), out Vector3d partHit, out Fixed64 partDistance), |
| | 1295 | 421 | | partHit, |
| | 1295 | 422 | | partDistance, |
| | 1295 | 423 | | ref sphereCenterAtImpact, |
| | 1295 | 424 | | ref impactDistance); |
| | | 425 | | } |
| | | 426 | | |
| | 1230 | 427 | | return found; |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | private bool TrySweepTriangle( |
| | | 431 | | Vector3d first, |
| | | 432 | | Vector3d second, |
| | | 433 | | Vector3d third, |
| | | 434 | | Vector3d normal, |
| | | 435 | | Vector3d start, |
| | | 436 | | Vector3d end, |
| | | 437 | | bool exactFaceProjection, |
| | | 438 | | out Vector3d sphereCenterAtImpact, |
| | | 439 | | out Fixed64 impactDistance) |
| | | 440 | | { |
| | 5741 | 441 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 5741 | 442 | | impactDistance = Fixed64.MaxValue; |
| | | 443 | | |
| | 5741 | 444 | | var triangle = new FixedTriangle(first, second, third); |
| | 5741 | 445 | | Vector3d closestAtStart = triangle.ClosestPoint(start); |
| | 5741 | 446 | | if (Vector3d.DistanceSquared(start, closestAtStart) <= _radius * _radius) |
| | | 447 | | { |
| | 3553 | 448 | | sphereCenterAtImpact = start; |
| | 3553 | 449 | | impactDistance = Fixed64.Zero; |
| | 3553 | 450 | | return true; |
| | | 451 | | } |
| | | 452 | | |
| | 2188 | 453 | | bool found = false; |
| | 2188 | 454 | | found |= TryKeepEarlierSweep( |
| | 2188 | 455 | | TrySweepTriangleFace(triangle, normal, _radius, start, end, exactFaceProjection, out Vector3d frontHit, out |
| | 2188 | 456 | | frontHit, |
| | 2188 | 457 | | frontDistance, |
| | 2188 | 458 | | ref sphereCenterAtImpact, |
| | 2188 | 459 | | ref impactDistance); |
| | 2188 | 460 | | found |= TryKeepEarlierSweep( |
| | 2188 | 461 | | TrySweepTriangleFace(triangle, normal, -_radius, start, end, exactFaceProjection, out Vector3d backHit, out |
| | 2188 | 462 | | backHit, |
| | 2188 | 463 | | backDistance, |
| | 2188 | 464 | | ref sphereCenterAtImpact, |
| | 2188 | 465 | | ref impactDistance); |
| | 2188 | 466 | | found |= TryKeepEarlierSweep( |
| | 2188 | 467 | | TrySweepTriangleEdge(first, second, start, end, out Vector3d firstEdgeHit, out Fixed64 firstEdgeDistance), |
| | 2188 | 468 | | firstEdgeHit, |
| | 2188 | 469 | | firstEdgeDistance, |
| | 2188 | 470 | | ref sphereCenterAtImpact, |
| | 2188 | 471 | | ref impactDistance); |
| | 2188 | 472 | | found |= TryKeepEarlierSweep( |
| | 2188 | 473 | | TrySweepTriangleEdge(second, third, start, end, out Vector3d secondEdgeHit, out Fixed64 secondEdgeDistance), |
| | 2188 | 474 | | secondEdgeHit, |
| | 2188 | 475 | | secondEdgeDistance, |
| | 2188 | 476 | | ref sphereCenterAtImpact, |
| | 2188 | 477 | | ref impactDistance); |
| | 2188 | 478 | | found |= TryKeepEarlierSweep( |
| | 2188 | 479 | | TrySweepTriangleEdge(third, first, start, end, out Vector3d thirdEdgeHit, out Fixed64 thirdEdgeDistance), |
| | 2188 | 480 | | thirdEdgeHit, |
| | 2188 | 481 | | thirdEdgeDistance, |
| | 2188 | 482 | | ref sphereCenterAtImpact, |
| | 2188 | 483 | | ref impactDistance); |
| | 2188 | 484 | | found |= TryKeepEarlierSweep( |
| | 2188 | 485 | | TrySweepSphere(start, end, _length, first, Fixed64.Zero, _radius, out Vector3d firstVertexHit, out Fixed64 f |
| | 2188 | 486 | | firstVertexHit, |
| | 2188 | 487 | | firstVertexDistance, |
| | 2188 | 488 | | ref sphereCenterAtImpact, |
| | 2188 | 489 | | ref impactDistance); |
| | 2188 | 490 | | found |= TryKeepEarlierSweep( |
| | 2188 | 491 | | TrySweepSphere(start, end, _length, second, Fixed64.Zero, _radius, out Vector3d secondVertexHit, out Fixed64 |
| | 2188 | 492 | | secondVertexHit, |
| | 2188 | 493 | | secondVertexDistance, |
| | 2188 | 494 | | ref sphereCenterAtImpact, |
| | 2188 | 495 | | ref impactDistance); |
| | 2188 | 496 | | found |= TryKeepEarlierSweep( |
| | 2188 | 497 | | TrySweepSphere(start, end, _length, third, Fixed64.Zero, _radius, out Vector3d thirdVertexHit, out Fixed64 t |
| | 2188 | 498 | | thirdVertexHit, |
| | 2188 | 499 | | thirdVertexDistance, |
| | 2188 | 500 | | ref sphereCenterAtImpact, |
| | 2188 | 501 | | ref impactDistance); |
| | | 502 | | |
| | 2188 | 503 | | return found; |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | private bool TrySweepTriangleFace( |
| | | 507 | | FixedTriangle triangle, |
| | | 508 | | Vector3d normal, |
| | | 509 | | Fixed64 signedRadius, |
| | | 510 | | Vector3d start, |
| | | 511 | | Vector3d end, |
| | | 512 | | bool exactFaceProjection, |
| | | 513 | | out Vector3d sphereCenterAtImpact, |
| | | 514 | | out Fixed64 impactDistance) |
| | | 515 | | { |
| | 4376 | 516 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 4376 | 517 | | impactDistance = Fixed64.Zero; |
| | | 518 | | |
| | 4376 | 519 | | Fixed64 denominator = Vector3d.Dot(end - start, normal); |
| | 4376 | 520 | | if (denominator == Fixed64.Zero) |
| | 1006 | 521 | | return false; |
| | | 522 | | |
| | 3370 | 523 | | Fixed64 signedStartDistance = Vector3d.Dot(start - triangle.A, normal); |
| | | 524 | | // Keep plane entry and the final center on the same authored chord; |
| | | 525 | | // a rounded unit direction can move a grazing trajectory across a face. |
| | 3370 | 526 | | if (!Fixed64.TryMultiplyDivide(signedRadius - signedStartDistance, _length, |
| | 3370 | 527 | | denominator, out Fixed64 distance) |
| | 3370 | 528 | | || distance < Fixed64.Zero || distance > _length) |
| | 1864 | 529 | | return false; |
| | | 530 | | |
| | 1506 | 531 | | Vector3d center = new FixedSegment(start, end).GetPointAtDistance(distance, _length); |
| | 1506 | 532 | | Vector3d pointOnPlane = center - normal * signedRadius; |
| | | 533 | | // Retriangulation must retain admitted micro faces; authored fallback keeps its original predicate. |
| | 1506 | 534 | | bool contains = exactFaceProjection |
| | 1506 | 535 | | ? WideTriangleRelations.ContainsProjection(triangle, Vector3d.Zero, FixedQuaternion.Identity, |
| | 1506 | 536 | | new FixedPointAnchor(pointOnPlane, FixedQuaternion.Identity, Vector3d.Zero)) |
| | 1506 | 537 | | : triangle.ContainsProjection(pointOnPlane); |
| | 1506 | 538 | | if (!contains) |
| | 361 | 539 | | return false; |
| | | 540 | | |
| | 1145 | 541 | | sphereCenterAtImpact = center; |
| | 1145 | 542 | | impactDistance = distance; |
| | 1145 | 543 | | return true; |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | private bool TrySweepTriangleEdge( |
| | | 547 | | Vector3d edgeStart, |
| | | 548 | | Vector3d edgeEnd, |
| | | 549 | | Vector3d start, |
| | | 550 | | Vector3d end, |
| | | 551 | | out Vector3d sphereCenterAtImpact, |
| | | 552 | | out Fixed64 impactDistance) |
| | | 553 | | { |
| | 6564 | 554 | | sphereCenterAtImpact = Vector3d.Zero; |
| | 6564 | 555 | | impactDistance = Fixed64.MaxValue; |
| | | 556 | | |
| | 6564 | 557 | | var query = new FixedSegment(start, end); |
| | 6564 | 558 | | var edgeAxis = new FixedSegment(edgeStart, edgeEnd); |
| | 6564 | 559 | | if (!query.TryGetFiniteCylinderIntersectionDistanceInterval( |
| | 6564 | 560 | | edgeAxis, |
| | 6564 | 561 | | Fixed64.Zero, |
| | 6564 | 562 | | _radius, |
| | 6564 | 563 | | _length, |
| | 6564 | 564 | | out Fixed64 entry, |
| | 6564 | 565 | | out _, |
| | 6564 | 566 | | out _, |
| | 6564 | 567 | | out _)) |
| | | 568 | | { |
| | 5701 | 569 | | return false; |
| | | 570 | | } |
| | | 571 | | |
| | 863 | 572 | | impactDistance = entry; |
| | 863 | 573 | | sphereCenterAtImpact = query.GetPointAtDistance(entry, _length); |
| | 863 | 574 | | return true; |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | private void BuildFiniteAxisSweepHit( |
| | | 578 | | Fixed64 distance, |
| | | 579 | | out Vector3d sphereCenterAtImpact, |
| | | 580 | | out Fixed64 impactDistance) |
| | | 581 | | { |
| | 2565 | 582 | | impactDistance = distance; |
| | 2565 | 583 | | sphereCenterAtImpact = new FixedSegment(_start, _end) |
| | 2565 | 584 | | .GetPointAtDistance(distance, _length); |
| | 2565 | 585 | | } |
| | | 586 | | |
| | | 587 | | private static bool TryKeepEarlierSweep( |
| | | 588 | | bool candidateFound, |
| | | 589 | | Vector3d candidateCenter, |
| | | 590 | | Fixed64 candidateDistance, |
| | | 591 | | ref Vector3d sphereCenterAtImpact, |
| | | 592 | | ref Fixed64 impactDistance) |
| | | 593 | | { |
| | 18799 | 594 | | if (!candidateFound || candidateDistance >= impactDistance) |
| | 16210 | 595 | | return false; |
| | | 596 | | |
| | 2589 | 597 | | sphereCenterAtImpact = candidateCenter; |
| | 2589 | 598 | | impactDistance = candidateDistance; |
| | 2589 | 599 | | return true; |
| | | 600 | | } |
| | | 601 | | |
| | | 602 | | private static void CreateSweepBounds(Vector3d start, Vector3d end, Fixed64 radius, out Vector3d min, out Vector3d m |
| | | 603 | | { |
| | 2937 | 604 | | Vector3d radiusExtents = Vector3d.One * radius; |
| | 2937 | 605 | | min = Vector3d.Min(start, end) - radiusExtents; |
| | 2937 | 606 | | max = Vector3d.Max(start, end) + radiusExtents; |
| | 2937 | 607 | | } |
| | | 608 | | } |