| | | 1 | | //======================================================================= |
| | | 2 | | // ContinuousCollisionMotionSegment2D.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 | | |
| | | 10 | | namespace Gravitas.CollisionHandling; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// One immutable, normalized-time piece of a 2D body's prepared CCD trajectory. |
| | | 14 | | /// </summary> |
| | | 15 | | internal readonly struct ContinuousCollisionMotionSegment2D |
| | | 16 | | { |
| | | 17 | | public ContinuousCollisionMotionSegment2D( |
| | | 18 | | Fixed64 startFraction, |
| | | 19 | | Fixed64 endFraction, |
| | | 20 | | Vector2d startPosition, |
| | | 21 | | Vector2d endPosition, |
| | | 22 | | Vector2d displacement, |
| | | 23 | | Fixed64 startRotation, |
| | | 24 | | Fixed64 angularDelta, |
| | | 25 | | Fixed64 angularVelocity) |
| | | 26 | | { |
| | 3201 | 27 | | StartFraction = startFraction; |
| | 3201 | 28 | | EndFraction = endFraction; |
| | 3201 | 29 | | StartPosition = startPosition; |
| | 3201 | 30 | | EndPosition = endPosition; |
| | 3201 | 31 | | Displacement = displacement; |
| | 3201 | 32 | | StartRotation = startRotation; |
| | 3201 | 33 | | AngularDelta = angularDelta; |
| | 3201 | 34 | | AngularVelocity = angularVelocity; |
| | 3201 | 35 | | } |
| | | 36 | | |
| | | 37 | | public Fixed64 StartFraction { get; } |
| | | 38 | | public Fixed64 EndFraction { get; } |
| | | 39 | | public Vector2d StartPosition { get; } |
| | | 40 | | public Vector2d EndPosition { get; } |
| | | 41 | | public Vector2d Displacement { get; } |
| | | 42 | | public Fixed64 StartRotation { get; } |
| | | 43 | | public Fixed64 AngularDelta { get; } |
| | | 44 | | public Fixed64 AngularVelocity { get; } |
| | 4517 | 45 | | public Fixed64 AngularDistance => AngularDelta.Abs(); |
| | | 46 | | |
| | | 47 | | public Vector2d SamplePosition(Fixed64 frameFraction) => |
| | 3981 | 48 | | Vector2d.Lerp(StartPosition, EndPosition, ResolveLocalFraction(frameFraction)); |
| | | 49 | | |
| | | 50 | | public Fixed64 SampleRotation(Fixed64 frameFraction) => |
| | 3870 | 51 | | StartRotation + AngularDelta * ResolveLocalFraction(frameFraction); |
| | | 52 | | |
| | | 53 | | public bool TryResolveMotionBound( |
| | | 54 | | Fixed64 lowerFraction, |
| | | 55 | | Fixed64 upperFraction, |
| | | 56 | | Fixed64 pivotRadius, |
| | | 57 | | out Fixed64 motionBound) |
| | | 58 | | { |
| | 3137 | 59 | | Fixed64 segmentSpan = EndFraction - StartFraction; |
| | 3137 | 60 | | Fixed64 localSpan = (upperFraction - lowerFraction) / segmentSpan; |
| | 3137 | 61 | | if (!Vector2d.TrySubtract(EndPosition, StartPosition, out Vector2d exactDisplacement)) |
| | | 62 | | { |
| | 1 | 63 | | motionBound = default; |
| | 1 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 3136 | 67 | | return ContinuousCollisionMath.TryResolveRotationalIntervalMotionBound( |
| | 3136 | 68 | | exactDisplacement, |
| | 3136 | 69 | | AngularDistance, |
| | 3136 | 70 | | pivotRadius, |
| | 3136 | 71 | | localSpan, |
| | 3136 | 72 | | out motionBound); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private Fixed64 ResolveLocalFraction(Fixed64 frameFraction) |
| | | 76 | | { |
| | 7851 | 77 | | Fixed64 span = EndFraction - StartFraction; |
| | 7851 | 78 | | return FixedMath.Clamp01((frameFraction - StartFraction) / span); |
| | | 79 | | } |
| | | 80 | | } |