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