| | | 1 | | //======================================================================= |
| | | 2 | | // ContinuousCollisionMath.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 System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | internal static class ContinuousCollisionMath |
| | | 15 | | { |
| | | 16 | | public const int RotationalIntervalMaxDepth = 12; |
| | | 17 | | public const int RotationalIntervalNodeBudget = 64; |
| | | 18 | | |
| | | 19 | | public enum IntervalSearchStatus : byte |
| | | 20 | | { |
| | | 21 | | CertifiedNoHit, |
| | | 22 | | ExactHit, |
| | | 23 | | Unresolved, |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | // Pose reconstruction uses normalized fixed-point rotations. Cover both |
| | | 27 | | // absolute operation rounding and its radius-scaled positional effect; |
| | | 28 | | // failure to represent either term keeps the interval unresolved. |
| | 1 | 29 | | private static readonly Fixed64 RotationalMotionUncertainty = Fixed64.Epsilon * 64; |
| | 1 | 30 | | private static readonly Fixed64 RotationalRelativeUncertainty = |
| | 1 | 31 | | FixedMath.CanonicalSinCosErrorBound * 16; |
| | 1 | 32 | | private static readonly Fixed64 ClosestFeatureRelativeUncertainty = |
| | 1 | 33 | | Fixed64.MinIncrement * 128; |
| | | 34 | | |
| | | 35 | | public readonly struct RotationalInterval |
| | | 36 | | { |
| | | 37 | | public RotationalInterval(Fixed64 lowerTime, Fixed64 upperTime, int depth) |
| | | 38 | | { |
| | 10424 | 39 | | LowerTime = lowerTime; |
| | 10424 | 40 | | UpperTime = upperTime; |
| | 10424 | 41 | | Depth = depth; |
| | 10424 | 42 | | } |
| | | 43 | | |
| | | 44 | | public Fixed64 LowerTime { get; } |
| | | 45 | | |
| | | 46 | | public Fixed64 UpperTime { get; } |
| | | 47 | | |
| | | 48 | | public int Depth { get; } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public static bool TryResolveRotationalSearchLimit( |
| | | 52 | | RotationalInterval interval, |
| | | 53 | | int processedNodeCount, |
| | | 54 | | bool hasWitness, |
| | | 55 | | Fixed64 witnessTime, |
| | | 56 | | out Fixed64 safeTime, |
| | | 57 | | out Fixed64 contactTime, |
| | | 58 | | out bool retainsWitness) |
| | | 59 | | { |
| | 6973 | 60 | | bool nodeBudgetExhausted = processedNodeCount >= RotationalIntervalNodeBudget; |
| | 6973 | 61 | | if (!nodeBudgetExhausted && interval.Depth < RotationalIntervalMaxDepth) |
| | | 62 | | { |
| | 6491 | 63 | | safeTime = default; |
| | 6491 | 64 | | contactTime = default; |
| | 6491 | 65 | | retainsWitness = false; |
| | 6491 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 482 | 69 | | retainsWitness = hasWitness; |
| | 482 | 70 | | contactTime = hasWitness ? witnessTime : interval.LowerTime; |
| | | 71 | | // Max depth defines the accepted temporal resolution, so its same-target |
| | | 72 | | // witness may resolve this leaf only when it brackets the leaf itself. A |
| | | 73 | | // global witness from a later interval cannot certify the unresolved gap. |
| | | 74 | | // Hard node-budget exhaustion has no convergence guarantee either. |
| | 482 | 75 | | bool witnessIsBracketed = hasWitness |
| | 482 | 76 | | && witnessTime >= interval.LowerTime |
| | 482 | 77 | | && witnessTime <= interval.UpperTime; |
| | 482 | 78 | | safeTime = witnessIsBracketed && !nodeBudgetExhausted |
| | 482 | 79 | | ? witnessTime |
| | 482 | 80 | | : interval.LowerTime; |
| | 482 | 81 | | return true; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public static bool ShouldContinueRotationalArbiter( |
| | | 85 | | Vector2d displacement, |
| | | 86 | | Fixed64 angularDistance) => |
| | 3 | 87 | | angularDistance > Fixed64.Epsilon |
| | 3 | 88 | | || displacement.MagnitudeSquared > Fixed64.Epsilon; |
| | | 89 | | |
| | | 90 | | public static bool TryResolveRotationalIntervalMotionBound( |
| | | 91 | | Vector2d displacement, |
| | | 92 | | Fixed64 angularDistance, |
| | | 93 | | Fixed64 pivotRadius, |
| | | 94 | | Fixed64 intervalSpan, |
| | | 95 | | out Fixed64 motionBound) |
| | | 96 | | { |
| | 7124 | 97 | | Vector2d halfIntervalDisplacement = displacement * (intervalSpan * Fixed64.Half); |
| | 7124 | 98 | | bool linearResolved = Vector2d.TryGetMagnitude( |
| | 7124 | 99 | | halfIntervalDisplacement, |
| | 7124 | 100 | | out Fixed64 linearMotion); |
| | 7124 | 101 | | bool angularResolved = Fixed64.TryMultiplyDivide( |
| | 7124 | 102 | | angularDistance, |
| | 7124 | 103 | | pivotRadius, |
| | 7124 | 104 | | intervalSpan, |
| | 7124 | 105 | | Fixed64.Two, |
| | 7124 | 106 | | out Fixed64 angularMotion); |
| | 7124 | 107 | | bool poseResolved = Fixed64.TryMultiplyDivide( |
| | 7124 | 108 | | pivotRadius, |
| | 7124 | 109 | | RotationalRelativeUncertainty, |
| | 7124 | 110 | | Fixed64.One, |
| | 7124 | 111 | | out Fixed64 poseUncertainty); |
| | 7124 | 112 | | bool combined = Fixed64.TryAdd(linearMotion, angularMotion, out motionBound) |
| | 7124 | 113 | | & Fixed64.TryAdd(motionBound, poseUncertainty, out motionBound) |
| | 7124 | 114 | | & Fixed64.TryAdd(motionBound, RotationalMotionUncertainty, out motionBound); |
| | 7124 | 115 | | if (!(linearResolved & angularResolved & poseResolved & combined)) |
| | | 116 | | { |
| | 2 | 117 | | motionBound = default; |
| | 2 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | 7122 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public static bool TryResolveRotationalIntervalMotionBound( |
| | | 125 | | Vector3d displacement, |
| | | 126 | | Fixed64 angularDistance, |
| | | 127 | | Fixed64 pivotRadius, |
| | | 128 | | Fixed64 intervalSpan, |
| | | 129 | | out Fixed64 motionBound) |
| | | 130 | | { |
| | 9567 | 131 | | Vector3d halfIntervalDisplacement = displacement * (intervalSpan * Fixed64.Half); |
| | 9567 | 132 | | bool linearResolved = Vector3d.TryGetMagnitude( |
| | 9567 | 133 | | halfIntervalDisplacement, |
| | 9567 | 134 | | out Fixed64 linearMotion); |
| | 9567 | 135 | | bool angularResolved = Fixed64.TryMultiplyDivide( |
| | 9567 | 136 | | angularDistance, |
| | 9567 | 137 | | pivotRadius, |
| | 9567 | 138 | | intervalSpan, |
| | 9567 | 139 | | Fixed64.Two, |
| | 9567 | 140 | | out Fixed64 angularMotion); |
| | 9567 | 141 | | bool poseResolved = Fixed64.TryMultiplyDivide( |
| | 9567 | 142 | | pivotRadius, |
| | 9567 | 143 | | RotationalRelativeUncertainty, |
| | 9567 | 144 | | Fixed64.One, |
| | 9567 | 145 | | out Fixed64 poseUncertainty); |
| | 9567 | 146 | | bool combined = Fixed64.TryAdd(linearMotion, angularMotion, out motionBound) |
| | 9567 | 147 | | & Fixed64.TryAdd(motionBound, poseUncertainty, out motionBound) |
| | 9567 | 148 | | & Fixed64.TryAdd(motionBound, RotationalMotionUncertainty, out motionBound); |
| | 9567 | 149 | | if (!(linearResolved & angularResolved & poseResolved & combined)) |
| | | 150 | | { |
| | 2 | 151 | | motionBound = default; |
| | 2 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 9565 | 155 | | return true; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | public static bool AreBoundsSeparatedByMoreThan( |
| | | 159 | | FixedBoundArea source, |
| | | 160 | | FixedBoundArea target, |
| | | 161 | | Fixed64 motionBound) => |
| | 312 | 162 | | IsAxisGapGreaterThan(source.Min.X, source.Max.X, target.Min.X, target.Max.X, motionBound) |
| | 312 | 163 | | | IsAxisGapGreaterThan(source.Min.Y, source.Max.Y, target.Min.Y, target.Max.Y, motionBound); |
| | | 164 | | |
| | | 165 | | public static bool AreBoundsSeparatedByMoreThan( |
| | | 166 | | FixedBoundBox source, |
| | | 167 | | FixedBoundBox target, |
| | | 168 | | Fixed64 motionBound) => |
| | 922 | 169 | | IsAxisGapGreaterThan(source.Min.X, source.Max.X, target.Min.X, target.Max.X, motionBound) |
| | 922 | 170 | | | IsAxisGapGreaterThan(source.Min.Y, source.Max.Y, target.Min.Y, target.Max.Y, motionBound) |
| | 922 | 171 | | | IsAxisGapGreaterThan(source.Min.Z, source.Max.Z, target.Min.Z, target.Max.Z, motionBound); |
| | | 172 | | |
| | | 173 | | public static bool TrySubtractClosestFeatureUncertainty( |
| | | 174 | | Fixed64 separationGap, |
| | | 175 | | Fixed64 characteristicScale, |
| | | 176 | | out Fixed64 conservativeGap) |
| | | 177 | | { |
| | 6759 | 178 | | bool inputValid = separationGap > Fixed64.Zero |
| | 6759 | 179 | | & characteristicScale >= Fixed64.Zero; |
| | 6759 | 180 | | bool scaled = Fixed64.TryMultiplyDivide( |
| | 6759 | 181 | | characteristicScale, |
| | 6759 | 182 | | ClosestFeatureRelativeUncertainty, |
| | 6759 | 183 | | Fixed64.One, |
| | 6759 | 184 | | out Fixed64 scaledUncertainty); |
| | 6759 | 185 | | bool combined = Fixed64.TryAdd( |
| | 6759 | 186 | | scaledUncertainty, |
| | 6759 | 187 | | RotationalMotionUncertainty, |
| | 6759 | 188 | | out Fixed64 uncertainty); |
| | 6759 | 189 | | bool subtracted = Fixed64.TrySubtract( |
| | 6759 | 190 | | separationGap, |
| | 6759 | 191 | | uncertainty, |
| | 6759 | 192 | | out conservativeGap); |
| | 6759 | 193 | | if (!(inputValid & scaled & combined & subtracted)) |
| | | 194 | | { |
| | 1500 | 195 | | conservativeGap = default; |
| | 1500 | 196 | | return false; |
| | | 197 | | } |
| | | 198 | | |
| | 5259 | 199 | | return conservativeGap > Fixed64.Zero; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 203 | | private static bool IsAxisGapGreaterThan( |
| | | 204 | | Fixed64 sourceMin, |
| | | 205 | | Fixed64 sourceMax, |
| | | 206 | | Fixed64 targetMin, |
| | | 207 | | Fixed64 targetMax, |
| | | 208 | | Fixed64 motionBound) |
| | | 209 | | { |
| | 3390 | 210 | | if (sourceMax < targetMin) |
| | 573 | 211 | | return !Fixed64.TrySubtract(targetMin, sourceMax, out Fixed64 gap) |
| | 573 | 212 | | | gap > motionBound; |
| | | 213 | | |
| | 2817 | 214 | | return targetMax < sourceMin |
| | 2817 | 215 | | & (!Fixed64.TrySubtract(sourceMin, targetMax, out Fixed64 reverseGap) |
| | 2817 | 216 | | | reverseGap > motionBound); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 220 | | public static bool IsWithinProxyRadius( |
| | | 221 | | Vector3d displacement, |
| | | 222 | | Fixed64 displacementMagnitudeSquared, |
| | | 223 | | Fixed64 proxyRadius) |
| | | 224 | | { |
| | 16 | 225 | | Fixed64 proxyRadiusSquared = proxyRadius * proxyRadius; |
| | 16 | 226 | | if (displacementMagnitudeSquared != Fixed64.MaxValue || proxyRadiusSquared != Fixed64.MaxValue) |
| | 13 | 227 | | return displacementMagnitudeSquared <= proxyRadiusSquared; |
| | | 228 | | |
| | 3 | 229 | | return Vector3d.TryGetMagnitude(displacement, out Fixed64 displacementMagnitude) |
| | 3 | 230 | | && displacementMagnitude <= proxyRadius; |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 234 | | public static bool IsWithinProxyRadius( |
| | | 235 | | Vector2d displacement, |
| | | 236 | | Fixed64 displacementMagnitudeSquared, |
| | | 237 | | Fixed64 proxyRadius) |
| | | 238 | | { |
| | 13 | 239 | | Fixed64 proxyRadiusSquared = proxyRadius * proxyRadius; |
| | 13 | 240 | | if (displacementMagnitudeSquared != Fixed64.MaxValue || proxyRadiusSquared != Fixed64.MaxValue) |
| | 9 | 241 | | return displacementMagnitudeSquared <= proxyRadiusSquared; |
| | | 242 | | |
| | 4 | 243 | | return Vector2d.TryGetMagnitude(displacement, out Fixed64 displacementMagnitude) |
| | 4 | 244 | | && displacementMagnitude <= proxyRadius; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 248 | | public static Vector3d ResolveContactPointOnTarget( |
| | | 249 | | Vector3d sourceCenter, |
| | | 250 | | Vector3d targetCenter, |
| | | 251 | | Vector3d normalForSource, |
| | | 252 | | Fixed64 targetRadius) |
| | | 253 | | { |
| | 5 | 254 | | if (normalForSource.MagnitudeSquared > Fixed64.Epsilon) |
| | 3 | 255 | | return targetCenter + normalForSource * targetRadius; |
| | | 256 | | |
| | 2 | 257 | | Vector3d fallback = sourceCenter - targetCenter; |
| | 2 | 258 | | return fallback.MagnitudeSquared > Fixed64.Epsilon |
| | 2 | 259 | | ? targetCenter + fallback.Normalized * targetRadius |
| | 2 | 260 | | : targetCenter; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 264 | | public static bool ShouldReplaceContinuousCollisionHit( |
| | | 265 | | Fixed64 candidateSafeTime, |
| | | 266 | | int candidateTargetId, |
| | | 267 | | bool hasCurrent, |
| | | 268 | | Fixed64 currentSafeTime, |
| | | 269 | | int currentTargetId) |
| | | 270 | | { |
| | 455 | 271 | | if (!hasCurrent) |
| | 439 | 272 | | return true; |
| | | 273 | | |
| | 16 | 274 | | int timeCompare = candidateSafeTime.CompareTo(currentSafeTime); |
| | 16 | 275 | | if (timeCompare != 0) |
| | 9 | 276 | | return timeCompare < 0; |
| | | 277 | | |
| | 7 | 278 | | return candidateTargetId < currentTargetId; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 282 | | public static void ClipTranslationalTrajectoryInterval( |
| | | 283 | | Fixed64 queryStart, |
| | | 284 | | Fixed64 segmentStart, |
| | | 285 | | Fixed64 segmentEnd, |
| | | 286 | | out Fixed64 overlapStart, |
| | | 287 | | out Fixed64 overlapEnd, |
| | | 288 | | out Fixed64 sourceStartTime, |
| | | 289 | | out Fixed64 sourceEndTime) |
| | | 290 | | { |
| | 522 | 291 | | overlapStart = FixedMath.Max(queryStart, segmentStart); |
| | 522 | 292 | | overlapEnd = segmentEnd; |
| | 522 | 293 | | Fixed64 querySpan = Fixed64.One - queryStart; |
| | 522 | 294 | | sourceStartTime = FixedMath.Clamp01((overlapStart - queryStart) / querySpan); |
| | 522 | 295 | | sourceEndTime = FixedMath.Clamp01((overlapEnd - queryStart) / querySpan); |
| | 522 | 296 | | } |
| | | 297 | | |
| | | 298 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 299 | | public static bool IsSupersededTranslationalBoundaryHit( |
| | | 300 | | bool hitAtSegmentEnd, |
| | | 301 | | Fixed64 overlapEnd, |
| | | 302 | | int segmentIndex, |
| | | 303 | | int segmentCount, |
| | | 304 | | Fixed64 successorStart) => |
| | 524 | 305 | | hitAtSegmentEnd |
| | 524 | 306 | | && overlapEnd < Fixed64.One |
| | 524 | 307 | | && segmentIndex + 1 < segmentCount |
| | 524 | 308 | | && successorStart == overlapEnd; |
| | | 309 | | |
| | | 310 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 311 | | public static bool TryNormalizeTranslationalClosingSpeed( |
| | | 312 | | Fixed64 localClosingSpeed, |
| | | 313 | | Fixed64 sourceIntervalSpan, |
| | | 314 | | out Fixed64 closingSpeed) |
| | | 315 | | { |
| | 409 | 316 | | if (localClosingSpeed <= Fixed64.Epsilon |
| | 409 | 317 | | || sourceIntervalSpan <= Fixed64.Zero) |
| | | 318 | | { |
| | 5 | 319 | | closingSpeed = Fixed64.Zero; |
| | 5 | 320 | | return false; |
| | | 321 | | } |
| | | 322 | | |
| | 404 | 323 | | closingSpeed = localClosingSpeed / sourceIntervalSpan; |
| | 404 | 324 | | return true; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | public static bool TryGetRelativeSphereOverlapDistanceInterval( |
| | | 328 | | Vector3d sourceStart, |
| | | 329 | | Vector3d sourceDisplacement, |
| | | 330 | | Fixed64 sourceRadius, |
| | | 331 | | Vector3d targetStart, |
| | | 332 | | Vector3d targetDisplacement, |
| | | 333 | | Fixed64 targetRadius, |
| | | 334 | | out Fixed64 entryDistance, |
| | | 335 | | out Fixed64 exitDistance, |
| | | 336 | | out Vector3d relativeDisplacement, |
| | | 337 | | out Fixed64 relativeLength, |
| | | 338 | | out Vector3d normalForSource, |
| | | 339 | | out Fixed64 closingSpeed) |
| | | 340 | | { |
| | 249 | 341 | | entryDistance = Fixed64.Zero; |
| | 249 | 342 | | exitDistance = Fixed64.Zero; |
| | 249 | 343 | | relativeDisplacement = Vector3d.Zero; |
| | 249 | 344 | | normalForSource = Vector3d.Zero; |
| | 249 | 345 | | closingSpeed = Fixed64.Zero; |
| | | 346 | | |
| | 249 | 347 | | relativeDisplacement = ContinuousCollisionSweepRange.ValidateRelativeDisplacement( |
| | 249 | 348 | | sourceDisplacement, |
| | 249 | 349 | | targetDisplacement, |
| | 249 | 350 | | out relativeLength); |
| | 248 | 351 | | Vector3d sourceEnd = GetSweepEnd(sourceStart, sourceDisplacement); |
| | 246 | 352 | | Vector3d targetEnd = GetSweepEnd(targetStart, targetDisplacement); |
| | 246 | 353 | | if (relativeDisplacement.MagnitudeSquared <= Fixed64.Epsilon |
| | 246 | 354 | | || !HasUsableCombinedRadius(sourceRadius, targetRadius)) |
| | | 355 | | { |
| | 9 | 356 | | return false; |
| | | 357 | | } |
| | | 358 | | |
| | 237 | 359 | | if (!WideFiniteAxisIntersection.TryGetSphereDirectionDistanceInterval( |
| | 237 | 360 | | sourceStart, |
| | 237 | 361 | | relativeDisplacement, |
| | 237 | 362 | | new FixedBoundSphere(targetStart, targetRadius), |
| | 237 | 363 | | sourceRadius, |
| | 237 | 364 | | relativeLength, |
| | 237 | 365 | | out entryDistance, |
| | 237 | 366 | | out exitDistance)) |
| | | 367 | | { |
| | 13 | 368 | | return false; |
| | | 369 | | } |
| | | 370 | | |
| | 224 | 371 | | Vector3d sourceImpact = new FixedSegment(sourceStart, sourceEnd) |
| | 224 | 372 | | .GetPointAtDistance(entryDistance, relativeLength); |
| | 224 | 373 | | Vector3d targetImpact = new FixedSegment(targetStart, targetEnd) |
| | 224 | 374 | | .GetPointAtDistance(entryDistance, relativeLength); |
| | 224 | 375 | | normalForSource = ResolveNormal(targetImpact, sourceImpact, relativeDisplacement); |
| | 224 | 376 | | closingSpeed = -Vector3d.Dot(relativeDisplacement, normalForSource); |
| | 224 | 377 | | return true; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | public static bool TryGetRelativeCircleOverlapDistanceInterval( |
| | | 381 | | Vector2d sourceStart, |
| | | 382 | | Vector2d sourceDisplacement, |
| | | 383 | | Fixed64 sourceRadius, |
| | | 384 | | Vector2d targetStart, |
| | | 385 | | Vector2d targetDisplacement, |
| | | 386 | | Fixed64 targetRadius, |
| | | 387 | | out Fixed64 entryDistance, |
| | | 388 | | out Fixed64 exitDistance, |
| | | 389 | | out Vector2d relativeDisplacement, |
| | | 390 | | out Fixed64 relativeLength, |
| | | 391 | | out Vector2d normalForSource, |
| | | 392 | | out Fixed64 closingSpeed) |
| | | 393 | | { |
| | 310 | 394 | | entryDistance = Fixed64.Zero; |
| | 310 | 395 | | exitDistance = Fixed64.Zero; |
| | 310 | 396 | | relativeDisplacement = Vector2d.Zero; |
| | 310 | 397 | | normalForSource = Vector2d.Zero; |
| | 310 | 398 | | closingSpeed = Fixed64.Zero; |
| | | 399 | | |
| | 310 | 400 | | relativeDisplacement = ContinuousCollisionSweepRange.ValidateRelativeDisplacement( |
| | 310 | 401 | | sourceDisplacement, |
| | 310 | 402 | | targetDisplacement, |
| | 310 | 403 | | out relativeLength); |
| | 309 | 404 | | Vector2d sourceEnd = GetSweepEnd(sourceStart, sourceDisplacement); |
| | 307 | 405 | | Vector2d targetEnd = GetSweepEnd(targetStart, targetDisplacement); |
| | 307 | 406 | | if (relativeDisplacement.MagnitudeSquared <= Fixed64.Epsilon |
| | 307 | 407 | | || !HasUsableCombinedRadius(sourceRadius, targetRadius)) |
| | | 408 | | { |
| | 5 | 409 | | return false; |
| | | 410 | | } |
| | | 411 | | |
| | 302 | 412 | | if (!WideFiniteAxisIntersection.TryGetCircleDirectionDistanceInterval( |
| | 302 | 413 | | sourceStart, |
| | 302 | 414 | | relativeDisplacement, |
| | 302 | 415 | | new FixedBoundCircle(targetStart, targetRadius), |
| | 302 | 416 | | sourceRadius, |
| | 302 | 417 | | relativeLength, |
| | 302 | 418 | | out entryDistance, |
| | 302 | 419 | | out exitDistance)) |
| | | 420 | | { |
| | 17 | 421 | | return false; |
| | | 422 | | } |
| | | 423 | | |
| | 285 | 424 | | Vector2d sourceImpact = new FixedSegment2d(sourceStart, sourceEnd) |
| | 285 | 425 | | .GetPointAtDistance(entryDistance, relativeLength); |
| | 285 | 426 | | Vector2d targetImpact = new FixedSegment2d(targetStart, targetEnd) |
| | 285 | 427 | | .GetPointAtDistance(entryDistance, relativeLength); |
| | 285 | 428 | | normalForSource = ResolveNormal(targetImpact, sourceImpact, relativeDisplacement); |
| | 285 | 429 | | closingSpeed = -Vector2d.Dot(relativeDisplacement, normalForSource); |
| | 285 | 430 | | return true; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 434 | | private static bool HasUsableCombinedRadius(Fixed64 sourceRadius, Fixed64 targetRadius) |
| | | 435 | | { |
| | 540 | 436 | | if (sourceRadius > Fixed64.Epsilon || targetRadius > Fixed64.Epsilon) |
| | 539 | 437 | | return true; |
| | | 438 | | |
| | 1 | 439 | | return sourceRadius + targetRadius > Fixed64.Epsilon; |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | private static Vector3d GetSweepEnd( |
| | | 443 | | Vector3d start, |
| | | 444 | | Vector3d displacement) |
| | | 445 | | { |
| | 494 | 446 | | if (!Vector3d.TryAdd(start, displacement, out Vector3d end)) |
| | 2 | 447 | | throw new System.ArgumentOutOfRangeException(nameof(displacement), "Continuous collision endpoint is outside |
| | | 448 | | |
| | 492 | 449 | | return end; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private static Vector2d GetSweepEnd( |
| | | 453 | | Vector2d start, |
| | | 454 | | Vector2d displacement) |
| | | 455 | | { |
| | 616 | 456 | | if (!Vector2d.TryAdd(start, displacement, out Vector2d end)) |
| | 2 | 457 | | throw new System.ArgumentOutOfRangeException(nameof(displacement), "Continuous collision endpoint is outside |
| | | 458 | | |
| | 614 | 459 | | return end; |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 463 | | private static Vector3d ResolveNormal( |
| | | 464 | | Vector3d targetPosition, |
| | | 465 | | Vector3d sourcePosition, |
| | | 466 | | Vector3d relativeDisplacement) |
| | | 467 | | { |
| | 224 | 468 | | Vector3d normal = Vector3d.GetDirection(targetPosition, sourcePosition); |
| | 224 | 469 | | return normal != Vector3d.Zero ? normal : -relativeDisplacement.Normalized; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 473 | | private static Vector2d ResolveNormal( |
| | | 474 | | Vector2d targetPosition, |
| | | 475 | | Vector2d sourcePosition, |
| | | 476 | | Vector2d relativeDisplacement) |
| | | 477 | | { |
| | 285 | 478 | | Vector2d normal = Vector2d.GetDirection(targetPosition, sourcePosition); |
| | 285 | 479 | | return normal != Vector2d.Zero ? normal : -relativeDisplacement.Normalized; |
| | | 480 | | } |
| | | 481 | | } |