| | | 1 | | //======================================================================= |
| | | 2 | | // ContactNormalImpulse3D.Accumulation.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 | | /// <content>Owns accumulated unilateral impulses and frozen incoming restitution targets.</content> |
| | | 13 | | internal static partial class ContactNormalImpulse3D |
| | | 14 | | { |
| | | 15 | | internal static ContactNormalImpulseResult3D CalculateAccumulatedDelta( |
| | | 16 | | SolidBody? bodyA, |
| | | 17 | | Vector3d linearVelocityA, |
| | | 18 | | Vector3d angularVelocityA, |
| | | 19 | | Vector3d relativeContactPointA, |
| | | 20 | | SolidBody? bodyB, |
| | | 21 | | Vector3d linearVelocityB, |
| | | 22 | | Vector3d angularVelocityB, |
| | | 23 | | Vector3d relativeContactPointB, |
| | | 24 | | Vector3d normal, |
| | | 25 | | Fixed64 restitution, |
| | | 26 | | Fixed64 restitutionVelocityThreshold, |
| | | 27 | | Fixed64 accumulatedImpulse, |
| | | 28 | | Fixed64 positiveImpulseScale, |
| | | 29 | | Fixed64 negativeImpulseScale) |
| | | 30 | | { |
| | 1 | 31 | | _ = TryCalculateAccumulatedDelta( |
| | 1 | 32 | | bodyA, |
| | 1 | 33 | | linearVelocityA, |
| | 1 | 34 | | angularVelocityA, |
| | 1 | 35 | | relativeContactPointA, |
| | 1 | 36 | | bodyB, |
| | 1 | 37 | | linearVelocityB, |
| | 1 | 38 | | angularVelocityB, |
| | 1 | 39 | | relativeContactPointB, |
| | 1 | 40 | | normal, |
| | 1 | 41 | | restitution, |
| | 1 | 42 | | restitutionVelocityThreshold, |
| | 1 | 43 | | accumulatedImpulse, |
| | 1 | 44 | | positiveImpulseScale, |
| | 1 | 45 | | negativeImpulseScale, |
| | 1 | 46 | | out ContactNormalImpulseResult3D result); |
| | 1 | 47 | | return result; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal static bool TryCalculateAccumulatedDelta( |
| | | 51 | | SolidBody? bodyA, |
| | | 52 | | Vector3d linearVelocityA, |
| | | 53 | | Vector3d angularVelocityA, |
| | | 54 | | Vector3d relativeContactPointA, |
| | | 55 | | SolidBody? bodyB, |
| | | 56 | | Vector3d linearVelocityB, |
| | | 57 | | Vector3d angularVelocityB, |
| | | 58 | | Vector3d relativeContactPointB, |
| | | 59 | | Vector3d normal, |
| | | 60 | | Fixed64 restitution, |
| | | 61 | | Fixed64 restitutionVelocityThreshold, |
| | | 62 | | Fixed64 accumulatedImpulse, |
| | | 63 | | Fixed64 positiveImpulseScale, |
| | | 64 | | Fixed64 negativeImpulseScale, |
| | | 65 | | out ContactNormalImpulseResult3D result) |
| | | 66 | | { |
| | 512624 | 67 | | result = default; |
| | 512624 | 68 | | if (!TryComputeNormalVelocity( |
| | 512624 | 69 | | linearVelocityA, |
| | 512624 | 70 | | angularVelocityA, |
| | 512624 | 71 | | relativeContactPointA, |
| | 512624 | 72 | | linearVelocityB, |
| | 512624 | 73 | | angularVelocityB, |
| | 512624 | 74 | | relativeContactPointB, |
| | 512624 | 75 | | normal, |
| | 512624 | 76 | | out Fixed64 normalVelocity) |
| | 512624 | 77 | | || !TryComputeDenominator( |
| | 512624 | 78 | | bodyA, |
| | 512624 | 79 | | relativeContactPointA, |
| | 512624 | 80 | | bodyB, |
| | 512624 | 81 | | relativeContactPointB, |
| | 512624 | 82 | | normal, |
| | 512624 | 83 | | out ContactEffectiveMassTerms3D denominator)) |
| | | 84 | | { |
| | 5586 | 85 | | return false; |
| | | 86 | | } |
| | | 87 | | |
| | 507038 | 88 | | if (denominator.SaturatedSum <= Fixed64.Zero) |
| | | 89 | | { |
| | 37 | 90 | | result = Zero(normalVelocity); |
| | 37 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | |
| | 507001 | 94 | | if (!TryCalculateAccumulatedImpulseDelta( |
| | 507001 | 95 | | normalVelocity, |
| | 507001 | 96 | | denominator, |
| | 507001 | 97 | | restitution, |
| | 507001 | 98 | | restitutionVelocityThreshold, |
| | 507001 | 99 | | accumulatedImpulse, |
| | 507001 | 100 | | positiveImpulseScale, |
| | 507001 | 101 | | negativeImpulseScale, |
| | 507001 | 102 | | out Fixed64 impulseScalar)) |
| | | 103 | | { |
| | 4 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 506997 | 107 | | return TryCreateAccumulatedResult(bodyA, relativeContactPointA, |
| | 506997 | 108 | | bodyB, relativeContactPointB, normal, normalVelocity, impulseScalar, out result); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private static bool TryCreateAccumulatedResult(SolidBody? bodyA, |
| | | 112 | | Vector3d relativeContactPointA, SolidBody? bodyB, Vector3d relativeContactPointB, |
| | | 113 | | Vector3d normal, Fixed64 normalVelocity, Fixed64 impulseScalar, |
| | | 114 | | out ContactNormalImpulseResult3D result) |
| | | 115 | | { |
| | 594793 | 116 | | result = default; |
| | 594793 | 117 | | if (impulseScalar == Fixed64.Zero) |
| | | 118 | | { |
| | 543741 | 119 | | result = Zero(normalVelocity); |
| | 543741 | 120 | | return true; |
| | | 121 | | } |
| | | 122 | | |
| | 51052 | 123 | | Vector3d impulseB = normal * impulseScalar; |
| | 51052 | 124 | | Vector3d impulseA = -impulseB; |
| | 51052 | 125 | | bool linearAResolved = TryComputeLinearVelocityDelta( |
| | 51052 | 126 | | bodyA, |
| | 51052 | 127 | | impulseA, |
| | 51052 | 128 | | out Vector3d linearVelocityDeltaA); |
| | 51052 | 129 | | bool angularAResolved = TryComputeAngularVelocityDelta( |
| | 51052 | 130 | | bodyA, |
| | 51052 | 131 | | relativeContactPointA, |
| | 51052 | 132 | | impulseA, |
| | 51052 | 133 | | out Vector3d angularVelocityDeltaA); |
| | 51052 | 134 | | bool linearBResolved = TryComputeLinearVelocityDelta( |
| | 51052 | 135 | | bodyB, |
| | 51052 | 136 | | impulseB, |
| | 51052 | 137 | | out Vector3d linearVelocityDeltaB); |
| | 51052 | 138 | | bool angularBResolved = TryComputeAngularVelocityDelta( |
| | 51052 | 139 | | bodyB, |
| | 51052 | 140 | | relativeContactPointB, |
| | 51052 | 141 | | impulseB, |
| | 51052 | 142 | | out Vector3d angularVelocityDeltaB); |
| | 51052 | 143 | | if (!(linearAResolved |
| | 51052 | 144 | | & angularAResolved |
| | 51052 | 145 | | & linearBResolved |
| | 51052 | 146 | | & angularBResolved)) |
| | | 147 | | { |
| | 3942 | 148 | | return false; |
| | | 149 | | } |
| | | 150 | | |
| | 47110 | 151 | | result = new ContactNormalImpulseResult3D( |
| | 47110 | 152 | | normalVelocity, |
| | 47110 | 153 | | impulseScalar, |
| | 47110 | 154 | | linearVelocityDeltaA, |
| | 47110 | 155 | | angularVelocityDeltaA, |
| | 47110 | 156 | | linearVelocityDeltaB, |
| | 47110 | 157 | | angularVelocityDeltaB); |
| | 47110 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | internal static bool TryCalculateAccumulatedDeltaWithImpact( |
| | | 162 | | SolidBody? bodyA, Vector3d linearVelocityA, Vector3d angularVelocityA, |
| | | 163 | | Vector3d relativeContactPointA, SolidBody? bodyB, Vector3d linearVelocityB, |
| | | 164 | | Vector3d angularVelocityB, Vector3d relativeContactPointB, Vector3d normal, |
| | | 165 | | Fixed64 restitution, Fixed64 restitutionVelocityThreshold, Fixed64 accumulatedImpulse, |
| | | 166 | | in ContactResponseSnapshot impact, out ContactNormalImpulseResult3D result) |
| | | 167 | | { |
| | 602153 | 168 | | if (restitution == Fixed64.Zero) |
| | 512615 | 169 | | return TryCalculateAccumulatedDelta(bodyA, linearVelocityA, angularVelocityA, |
| | 512615 | 170 | | relativeContactPointA, bodyB, linearVelocityB, angularVelocityB, |
| | 512615 | 171 | | relativeContactPointB, normal, restitution, restitutionVelocityThreshold, |
| | 512615 | 172 | | accumulatedImpulse, Fixed64.One, Fixed64.One, out result); |
| | 89538 | 173 | | result = default; |
| | 89538 | 174 | | if (!TryComputeNormalVelocity(linearVelocityA, angularVelocityA, relativeContactPointA, |
| | 89538 | 175 | | linearVelocityB, angularVelocityB, relativeContactPointB, normal, out Fixed64 velocity) |
| | 89538 | 176 | | || !TryComputeNormalVelocity(impact.LinearA, impact.AngularA, relativeContactPointA, |
| | 89538 | 177 | | impact.LinearB, impact.AngularB, relativeContactPointB, normal, out Fixed64 incoming) |
| | 89538 | 178 | | || !TryComputeDenominator(bodyA, relativeContactPointA, bodyB, |
| | 89538 | 179 | | relativeContactPointB, normal, out ContactEffectiveMassTerms3D denominator)) |
| | 1737 | 180 | | return false; |
| | 87801 | 181 | | if (denominator.SaturatedSum <= Fixed64.Zero) |
| | | 182 | | { |
| | 3 | 183 | | result = Zero(velocity); |
| | 3 | 184 | | return true; |
| | | 185 | | } |
| | 87798 | 186 | | Fixed64 bounce = incoming < -restitutionVelocityThreshold ? restitution : Fixed64.Zero; |
| | | 187 | | // Fuse v_current + e*v_incoming before division. Materializing a rounded |
| | | 188 | | // bounce speed here would add another rounding to the completed impulse. |
| | 87798 | 189 | | Signed320 numerator = WideArithmetic.Negate(WideArithmetic.AddSigned320( |
| | 87798 | 190 | | WideArithmetic.MultiplySigned192(Signed192.Raw(velocity), Signed192.One), |
| | 87798 | 191 | | WideArithmetic.MultiplySigned192(Signed192.Raw(incoming), Signed192.Raw(bounce)))); |
| | 87798 | 192 | | Signed192 sum = WideArithmetic.AddSigned192( |
| | 87798 | 193 | | WideArithmetic.AddSigned192(Signed192.Raw(denominator.LinearA), Signed192.Raw(denominator.LinearB)), |
| | 87798 | 194 | | WideArithmetic.AddSigned192(Signed192.Raw(denominator.AngularA), Signed192.Raw(denominator.AngularB))); |
| | | 195 | | Fixed64 impulse; |
| | 87798 | 196 | | if (!Fixed64.TryGetSignedRawRatio(Signed832.ExtendValue(Signed576.ExtendValue(numerator)), Signed832.ExtendValue |
| | | 197 | | { |
| | 3 | 198 | | if (numerator.Sign >= 0) return false; |
| | 1 | 199 | | impulse = -accumulatedImpulse; |
| | | 200 | | } |
| | 87796 | 201 | | else if (!Fixed64.TryAdd(accumulatedImpulse, delta, out Fixed64 accumulated) |
| | 87796 | 202 | | || !Fixed64.TrySubtract(FixedMath.Max(Fixed64.Zero, accumulated), accumulatedImpulse, out impulse)) |
| | 1 | 203 | | return false; |
| | 87796 | 204 | | return TryCreateAccumulatedResult(bodyA, relativeContactPointA, bodyB, |
| | 87796 | 205 | | relativeContactPointB, normal, velocity, impulse, out result); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | internal static bool TryCalculateAccumulatedDeltaExact( |
| | | 209 | | SolidBody? bodyA, |
| | | 210 | | Vector3d linearVelocityA, |
| | | 211 | | Vector3d angularVelocityA, |
| | | 212 | | in ExactLever3D relativeContactPointA, |
| | | 213 | | SolidBody? bodyB, |
| | | 214 | | Vector3d linearVelocityB, |
| | | 215 | | Vector3d angularVelocityB, |
| | | 216 | | in ExactLever3D relativeContactPointB, |
| | | 217 | | Vector3d normal, |
| | | 218 | | Fixed64 restitution, |
| | | 219 | | Fixed64 restitutionVelocityThreshold, |
| | | 220 | | Fixed64 accumulatedImpulse, |
| | | 221 | | Fixed64 positiveImpulseScale, |
| | | 222 | | Fixed64 negativeImpulseScale, |
| | | 223 | | out ContactNormalImpulseResult3D result, |
| | | 224 | | in ContactResponseSnapshot impact = default) |
| | | 225 | | { |
| | 11308 | 226 | | result = default; |
| | 11308 | 227 | | if (!ExactContactLever3D.TryGetAccumulatedNormalResponse( |
| | 11308 | 228 | | bodyA, |
| | 11308 | 229 | | linearVelocityA, |
| | 11308 | 230 | | angularVelocityA, |
| | 11308 | 231 | | relativeContactPointA, |
| | 11308 | 232 | | bodyB, |
| | 11308 | 233 | | linearVelocityB, |
| | 11308 | 234 | | angularVelocityB, |
| | 11308 | 235 | | relativeContactPointB, |
| | 11308 | 236 | | normal, |
| | 11308 | 237 | | restitution, |
| | 11308 | 238 | | restitutionVelocityThreshold, |
| | 11308 | 239 | | accumulatedImpulse, |
| | 11308 | 240 | | positiveImpulseScale, |
| | 11308 | 241 | | negativeImpulseScale, |
| | 11308 | 242 | | out ExactNormalResponse3D response, impact)) |
| | | 243 | | { |
| | 5 | 244 | | return false; |
| | | 245 | | } |
| | | 246 | | |
| | 11303 | 247 | | bool hasNormalVelocity = |
| | 11303 | 248 | | response.TryGetNormalVelocity(out Fixed64 normalVelocity); |
| | 11303 | 249 | | bool hasAppliedImpulse = |
| | 11303 | 250 | | response.TryGetAppliedImpulse(out Fixed64 appliedImpulse); |
| | | 251 | | Fixed64 impulseScalar; |
| | 11303 | 252 | | if (response.TryGetAccumulatedImpulse( |
| | 11303 | 253 | | out Fixed64 newAccumulatedImpulse)) |
| | | 254 | | { |
| | | 255 | | // Both values are nonnegative, so their difference is always in |
| | | 256 | | // [-Fixed64.MaxValue, Fixed64.MaxValue]. |
| | 11299 | 257 | | impulseScalar = newAccumulatedImpulse - accumulatedImpulse; |
| | | 258 | | } |
| | | 259 | | else |
| | | 260 | | { |
| | 4 | 261 | | impulseScalar = -accumulatedImpulse; |
| | | 262 | | } |
| | | 263 | | |
| | 11303 | 264 | | result = new ContactNormalImpulseResult3D( |
| | 11303 | 265 | | normalVelocity, |
| | 11303 | 266 | | impulseScalar, |
| | 11303 | 267 | | appliedImpulse, |
| | 11303 | 268 | | response.FirstLinearVelocityDelta, |
| | 11303 | 269 | | response.FirstAngularVelocityDelta, |
| | 11303 | 270 | | response.SecondLinearVelocityDelta, |
| | 11303 | 271 | | response.SecondAngularVelocityDelta, |
| | 11303 | 272 | | hasNormalVelocity, |
| | 11303 | 273 | | hasAppliedImpulse); |
| | 11303 | 274 | | return true; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | private static bool TryCalculateAccumulatedImpulseDelta( |
| | | 278 | | Fixed64 normalVelocity, |
| | | 279 | | in ContactEffectiveMassTerms3D denominator, |
| | | 280 | | Fixed64 restitution, |
| | | 281 | | Fixed64 restitutionVelocityThreshold, |
| | | 282 | | Fixed64 accumulatedImpulse, |
| | | 283 | | Fixed64 positiveImpulseScale, |
| | | 284 | | Fixed64 negativeImpulseScale, |
| | | 285 | | out Fixed64 impulseDelta) |
| | | 286 | | { |
| | 507001 | 287 | | Fixed64 appliedRestitution = |
| | 507001 | 288 | | normalVelocity < -restitutionVelocityThreshold |
| | 507001 | 289 | | ? restitution |
| | 507001 | 290 | | : Fixed64.Zero; |
| | 507001 | 291 | | Fixed64 responseFactor = -(Fixed64.One + appliedRestitution); |
| | 507001 | 292 | | Fixed64 impulseScale = normalVelocity < Fixed64.Zero |
| | 507001 | 293 | | ? positiveImpulseScale |
| | 507001 | 294 | | : negativeImpulseScale; |
| | 507001 | 295 | | if (!Fixed64.TryMultiplyDivideBySum( |
| | 507001 | 296 | | normalVelocity, |
| | 507001 | 297 | | responseFactor, |
| | 507001 | 298 | | impulseScale, |
| | 507001 | 299 | | Fixed64.One, |
| | 507001 | 300 | | denominator.LinearA, |
| | 507001 | 301 | | denominator.LinearB, |
| | 507001 | 302 | | denominator.AngularA, |
| | 507001 | 303 | | denominator.AngularB, |
| | 507001 | 304 | | out Fixed64 scaledImpulse)) |
| | | 305 | | { |
| | 2 | 306 | | impulseDelta = default; |
| | 2 | 307 | | return normalVelocity >= Fixed64.Zero |
| | 2 | 308 | | && responseFactor <= Fixed64.Zero |
| | 2 | 309 | | && impulseScale >= Fixed64.Zero |
| | 2 | 310 | | && accumulatedImpulse >= Fixed64.Zero |
| | 2 | 311 | | && Fixed64.TrySubtract( |
| | 2 | 312 | | Fixed64.Zero, |
| | 2 | 313 | | accumulatedImpulse, |
| | 2 | 314 | | out impulseDelta); |
| | | 315 | | } |
| | | 316 | | |
| | 506999 | 317 | | if (!Fixed64.TryAdd( |
| | 506999 | 318 | | accumulatedImpulse, |
| | 506999 | 319 | | scaledImpulse, |
| | 506999 | 320 | | out Fixed64 accumulated)) |
| | | 321 | | { |
| | 1 | 322 | | impulseDelta = default; |
| | 1 | 323 | | return false; |
| | | 324 | | } |
| | | 325 | | |
| | 506998 | 326 | | return Fixed64.TrySubtract( |
| | 506998 | 327 | | FixedMath.Max(Fixed64.Zero, accumulated), |
| | 506998 | 328 | | accumulatedImpulse, |
| | 506998 | 329 | | out impulseDelta); |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | } |