| | | 1 | | //======================================================================= |
| | | 2 | | // ContinuousCollisionImpulsePolicy.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 | | internal static class ContinuousCollisionImpulsePolicy |
| | | 13 | | { |
| | | 14 | | internal static bool TryResolveVelocityDelta( |
| | | 15 | | Vector3d signedNormal, |
| | | 16 | | Fixed64 normalVelocity, |
| | | 17 | | Fixed64 responseFactor, |
| | | 18 | | Fixed64 bodyInverseMass, |
| | | 19 | | Fixed64 constrainedInverseMass, |
| | | 20 | | out Vector3d velocityDelta) |
| | | 21 | | { |
| | 32 | 22 | | if (bodyInverseMass == Fixed64.Zero) |
| | | 23 | | { |
| | 8 | 24 | | velocityDelta = default; |
| | 8 | 25 | | return true; |
| | | 26 | | } |
| | | 27 | | |
| | 24 | 28 | | bool resolved = Fixed64.TryMultiplyDivide( |
| | 24 | 29 | | normalVelocity, |
| | 24 | 30 | | responseFactor, |
| | 24 | 31 | | bodyInverseMass, |
| | 24 | 32 | | constrainedInverseMass, |
| | 24 | 33 | | out Fixed64 speedDelta); |
| | 24 | 34 | | velocityDelta = signedNormal * speedDelta; |
| | 24 | 35 | | return resolved; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | internal static bool TryResolveVelocityDelta( |
| | | 39 | | Vector2d signedNormal, |
| | | 40 | | Fixed64 normalVelocity, |
| | | 41 | | Fixed64 responseFactor, |
| | | 42 | | Fixed64 bodyInverseMass, |
| | | 43 | | Fixed64 constrainedInverseMass, |
| | | 44 | | out Vector2d velocityDelta) |
| | | 45 | | { |
| | 92 | 46 | | if (bodyInverseMass == Fixed64.Zero) |
| | | 47 | | { |
| | 27 | 48 | | velocityDelta = default; |
| | 27 | 49 | | return true; |
| | | 50 | | } |
| | | 51 | | |
| | 65 | 52 | | bool resolved = Fixed64.TryMultiplyDivide( |
| | 65 | 53 | | normalVelocity, |
| | 65 | 54 | | responseFactor, |
| | 65 | 55 | | bodyInverseMass, |
| | 65 | 56 | | constrainedInverseMass, |
| | 65 | 57 | | out Fixed64 speedDelta); |
| | 65 | 58 | | velocityDelta = signedNormal * speedDelta; |
| | 65 | 59 | | return resolved; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | internal static bool TryResolveVelocityDelta( |
| | | 63 | | Vector3d normal, |
| | | 64 | | Fixed64 responseSpeed, |
| | | 65 | | Fixed64 bodyInverseMass, |
| | | 66 | | Fixed64 constrainedInverseMass, |
| | | 67 | | out Vector3d velocityDelta) |
| | | 68 | | { |
| | 12555 | 69 | | if (bodyInverseMass == Fixed64.Zero) |
| | | 70 | | { |
| | 7 | 71 | | velocityDelta = default; |
| | 7 | 72 | | return true; |
| | | 73 | | } |
| | | 74 | | |
| | 12548 | 75 | | if (!Fixed64.TryMultiplyDivide(normal.X, responseSpeed, bodyInverseMass, constrainedInverseMass, out Fixed64 x) |
| | 12548 | 76 | | || !Fixed64.TryMultiplyDivide(normal.Y, responseSpeed, bodyInverseMass, constrainedInverseMass, out Fixed64 |
| | 12548 | 77 | | || !Fixed64.TryMultiplyDivide(normal.Z, responseSpeed, bodyInverseMass, constrainedInverseMass, out Fixed64 |
| | | 78 | | { |
| | 15 | 79 | | velocityDelta = default; |
| | 15 | 80 | | return false; |
| | | 81 | | } |
| | | 82 | | |
| | 12533 | 83 | | velocityDelta = new Vector3d(x, y, z); |
| | 12533 | 84 | | return true; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | internal static bool TryResolveVelocityDelta( |
| | | 88 | | Vector2d normal, |
| | | 89 | | Fixed64 responseSpeed, |
| | | 90 | | Fixed64 bodyInverseMass, |
| | | 91 | | Fixed64 constrainedInverseMass, |
| | | 92 | | out Vector2d velocityDelta) |
| | | 93 | | { |
| | 375 | 94 | | if (bodyInverseMass == Fixed64.Zero) |
| | | 95 | | { |
| | 61 | 96 | | velocityDelta = default; |
| | 61 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | |
| | 314 | 100 | | if (!Fixed64.TryMultiplyDivide(normal.X, responseSpeed, bodyInverseMass, constrainedInverseMass, out Fixed64 x) |
| | 314 | 101 | | || !Fixed64.TryMultiplyDivide(normal.Y, responseSpeed, bodyInverseMass, constrainedInverseMass, out Fixed64 |
| | | 102 | | { |
| | 14 | 103 | | velocityDelta = default; |
| | 14 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 300 | 107 | | velocityDelta = new Vector2d(x, y); |
| | 300 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | internal static bool TryResolveSourceNormal( |
| | | 112 | | Vector3d normalForSource, |
| | | 113 | | Vector3d sourceDisplacement, |
| | | 114 | | out Vector3d normal) |
| | | 115 | | { |
| | 14 | 116 | | if (normalForSource.MagnitudeSquared > Fixed64.Epsilon) |
| | | 117 | | { |
| | 12 | 118 | | normal = normalForSource.Normalized; |
| | 12 | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | if (sourceDisplacement.MagnitudeSquared > Fixed64.Epsilon) |
| | | 123 | | { |
| | 1 | 124 | | normal = -sourceDisplacement.Normalized; |
| | 1 | 125 | | return true; |
| | | 126 | | } |
| | | 127 | | |
| | 1 | 128 | | normal = Vector3d.Zero; |
| | 1 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | internal static bool TryResolveSourceNormal( |
| | | 133 | | Vector2d normalForSource, |
| | | 134 | | Vector2d sourceDisplacement, |
| | | 135 | | out Vector2d normal) |
| | | 136 | | { |
| | 12 | 137 | | if (normalForSource.MagnitudeSquared > Fixed64.Epsilon) |
| | | 138 | | { |
| | 10 | 139 | | normal = normalForSource.Normalized; |
| | 10 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | |
| | 2 | 143 | | if (sourceDisplacement.MagnitudeSquared > Fixed64.Epsilon) |
| | | 144 | | { |
| | 1 | 145 | | normal = -sourceDisplacement.Normalized; |
| | 1 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | normal = Vector2d.Zero; |
| | 1 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | internal static bool TryResolveImpactNormal(Vector3d normalForSource, out Vector3d normal) |
| | | 154 | | { |
| | 76 | 155 | | if (normalForSource.MagnitudeSquared > Fixed64.Epsilon) |
| | | 156 | | { |
| | 72 | 157 | | normal = normalForSource.Normalized; |
| | 72 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | 4 | 161 | | normal = Vector3d.Zero; |
| | 4 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | internal static bool TryResolveImpactNormal(Vector2d normalForSource, out Vector2d normal) |
| | | 166 | | { |
| | 81 | 167 | | if (normalForSource.MagnitudeSquared > Fixed64.Epsilon) |
| | | 168 | | { |
| | 67 | 169 | | normal = normalForSource.Normalized; |
| | 67 | 170 | | return true; |
| | | 171 | | } |
| | | 172 | | |
| | 14 | 173 | | normal = Vector2d.Zero; |
| | 14 | 174 | | return false; |
| | | 175 | | } |
| | | 176 | | } |