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