| | | 1 | | //======================================================================= |
| | | 2 | | // ExactContactResponseKernel.Coulomb.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; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using FixedMathSharp.Geometry; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.CollisionHandling; |
| | | 15 | | |
| | | 16 | | /// <content> |
| | | 17 | | /// Owns exact line and disk Coulomb-friction policy and materialization. |
| | | 18 | | /// </content> |
| | | 19 | | internal static partial class ExactContactResponseKernel |
| | | 20 | | { |
| | | 21 | | private const int MaxCoulombWords = MaxResponseWords * 2; |
| | | 22 | | private const int MaxCoulombSquareWords = MaxCoulombWords * 2; |
| | | 23 | | // Normal-response ratios use fewer than 46 active words. A common tangent |
| | | 24 | | // denominator therefore uses fewer than 92, its squared magnitude fewer |
| | | 25 | | // than 185 including carry, and the largest radial comparison fewer than |
| | | 26 | | // 307. The remaining words prevent silent product truncation. |
| | | 27 | | private const int MaxCoulombComparisonWords = 320; |
| | | 28 | | |
| | | 29 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 30 | | internal static bool TryGetCoulombLineResponse( |
| | | 31 | | in ExactNormalConstraint3D normalConstraint, |
| | | 32 | | in ExactContactResponseOperand3D firstTangent, |
| | | 33 | | in ExactContactResponseOperand3D secondTangent, |
| | | 34 | | Vector3d tangent, |
| | | 35 | | Fixed64 accumulatedTangentImpulse, |
| | | 36 | | Fixed64 staticFriction, |
| | | 37 | | Fixed64 dynamicFriction, |
| | | 38 | | out ExactCoulombResponse3D response) |
| | | 39 | | { |
| | 59 | 40 | | response = default; |
| | 59 | 41 | | bool inputsValid = |
| | 59 | 42 | | staticFriction >= Fixed64.Zero |
| | 59 | 43 | | & dynamicFriction >= Fixed64.Zero |
| | 59 | 44 | | & tangent.IsNormalized(); |
| | 59 | 45 | | if (!inputsValid) |
| | 3 | 46 | | return false; |
| | | 47 | | |
| | 56 | 48 | | bool constraintValid = |
| | 56 | 49 | | FixedMath.Abs(Vector3d.Dot( |
| | 56 | 50 | | normalConstraint.Normal, |
| | 56 | 51 | | tangent)) <= Fixed64.Epsilon |
| | 56 | 52 | | & HaveMatchingParticipants( |
| | 56 | 53 | | normalConstraint.First, |
| | 56 | 54 | | firstTangent) |
| | 56 | 55 | | & HaveMatchingParticipants( |
| | 56 | 56 | | normalConstraint.Second, |
| | 56 | 57 | | secondTangent); |
| | 56 | 58 | | if (!constraintValid) |
| | 2 | 59 | | return false; |
| | | 60 | | |
| | 54 | 61 | | Span<ulong> normalNumerator = stackalloc ulong[MaxResponseWords]; |
| | 54 | 62 | | Span<ulong> normalDenominator = stackalloc ulong[MaxResponseWords]; |
| | 54 | 63 | | Span<ulong> tangentNumerator = stackalloc ulong[MaxResponseWords]; |
| | 54 | 64 | | Span<ulong> tangentDenominator = stackalloc ulong[MaxResponseWords]; |
| | 54 | 65 | | if (!TryGetCompletedNormalAccumulatorRatio( |
| | 54 | 66 | | normalConstraint, |
| | 54 | 67 | | normalNumerator, |
| | 54 | 68 | | normalDenominator)) |
| | 1 | 69 | | return false; |
| | 53 | 70 | | GetBilateralImpulseRatio( |
| | 53 | 71 | | firstTangent, |
| | 53 | 72 | | secondTangent, |
| | 53 | 73 | | tangent, |
| | 53 | 74 | | Fixed64.Zero, |
| | 53 | 75 | | tangentNumerator, |
| | 53 | 76 | | tangentDenominator, |
| | 53 | 77 | | out int tangentSign); |
| | | 78 | | |
| | 53 | 79 | | Span<ulong> desiredNumerator = stackalloc ulong[MaxResponseWords]; |
| | 53 | 80 | | AddFixedToRatio( |
| | 53 | 81 | | tangentNumerator, |
| | 53 | 82 | | tangentDenominator, |
| | 53 | 83 | | tangentSign, |
| | 53 | 84 | | accumulatedTangentImpulse, |
| | 53 | 85 | | desiredNumerator, |
| | 53 | 86 | | out int desiredSign); |
| | | 87 | | |
| | 53 | 88 | | Span<ulong> staticNumerator = stackalloc ulong[MaxResponseWords]; |
| | 53 | 89 | | Span<ulong> staticDenominator = stackalloc ulong[MaxResponseWords]; |
| | 53 | 90 | | GetFrictionLimit( |
| | 53 | 91 | | normalNumerator, |
| | 53 | 92 | | normalDenominator, |
| | 53 | 93 | | staticFriction, |
| | 53 | 94 | | staticNumerator, |
| | 53 | 95 | | staticDenominator); |
| | | 96 | | |
| | 53 | 97 | | Span<ulong> appliedNumerator = stackalloc ulong[MaxCoulombWords]; |
| | 53 | 98 | | Span<ulong> appliedDenominator = stackalloc ulong[MaxCoulombWords]; |
| | | 99 | | int appliedSign; |
| | 53 | 100 | | Span<ulong> accumulatedNumerator = stackalloc ulong[MaxCoulombWords]; |
| | 53 | 101 | | Span<ulong> accumulatedDenominator = stackalloc ulong[MaxCoulombWords]; |
| | | 102 | | int accumulatedSign; |
| | 53 | 103 | | appliedNumerator.Clear(); |
| | 53 | 104 | | appliedDenominator.Clear(); |
| | 53 | 105 | | accumulatedNumerator.Clear(); |
| | 53 | 106 | | accumulatedDenominator.Clear(); |
| | 53 | 107 | | Span<ulong> clampedNumerator = stackalloc ulong[MaxCoulombWords]; |
| | 53 | 108 | | Span<ulong> clampedDenominator = stackalloc ulong[MaxResponseWords]; |
| | 53 | 109 | | if (CompareRatios( |
| | 53 | 110 | | desiredNumerator, |
| | 53 | 111 | | tangentDenominator, |
| | 53 | 112 | | staticNumerator, |
| | 53 | 113 | | staticDenominator) <= 0) |
| | | 114 | | { |
| | 37 | 115 | | tangentNumerator.CopyTo(appliedNumerator); |
| | 37 | 116 | | tangentDenominator.CopyTo(appliedDenominator); |
| | 37 | 117 | | appliedSign = tangentSign; |
| | 37 | 118 | | desiredNumerator.CopyTo(accumulatedNumerator); |
| | 37 | 119 | | tangentDenominator.CopyTo(accumulatedDenominator); |
| | 37 | 120 | | accumulatedSign = desiredSign; |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | 16 | 124 | | GetFrictionLimit( |
| | 16 | 125 | | normalNumerator, |
| | 16 | 126 | | normalDenominator, |
| | 16 | 127 | | dynamicFriction, |
| | 16 | 128 | | clampedNumerator, |
| | 16 | 129 | | clampedDenominator); |
| | 16 | 130 | | clampedNumerator.CopyTo(accumulatedNumerator); |
| | 16 | 131 | | clampedDenominator.CopyTo(accumulatedDenominator); |
| | 16 | 132 | | accumulatedSign = WideArithmetic.IsZeroMagnitude( |
| | 16 | 133 | | clampedNumerator) |
| | 16 | 134 | | ? 0 |
| | 16 | 135 | | : desiredSign; |
| | 16 | 136 | | SubtractFixedFromRatio( |
| | 16 | 137 | | clampedNumerator, |
| | 16 | 138 | | clampedDenominator, |
| | 16 | 139 | | accumulatedSign, |
| | 16 | 140 | | accumulatedTangentImpulse, |
| | 16 | 141 | | appliedNumerator, |
| | 16 | 142 | | appliedDenominator, |
| | 16 | 143 | | out appliedSign); |
| | | 144 | | } |
| | | 145 | | |
| | 53 | 146 | | bool resolved = TryGetLinearVelocityDelta( |
| | 53 | 147 | | firstTangent.LinearImpulseAxis, |
| | 53 | 148 | | firstTangent.InverseMass, |
| | 53 | 149 | | appliedNumerator, |
| | 53 | 150 | | appliedDenominator, |
| | 53 | 151 | | appliedSign, |
| | 53 | 152 | | out Vector3d firstLinear); |
| | 53 | 153 | | resolved &= TryGetAngularVelocityDelta( |
| | 53 | 154 | | firstTangent.Lever, |
| | 53 | 155 | | -tangent, |
| | 53 | 156 | | firstTangent.InverseInertia, |
| | 53 | 157 | | appliedNumerator, |
| | 53 | 158 | | appliedDenominator, |
| | 53 | 159 | | appliedSign, |
| | 53 | 160 | | out Vector3d firstAngular); |
| | 53 | 161 | | resolved &= TryGetLinearVelocityDelta( |
| | 53 | 162 | | secondTangent.LinearImpulseAxis, |
| | 53 | 163 | | secondTangent.InverseMass, |
| | 53 | 164 | | appliedNumerator, |
| | 53 | 165 | | appliedDenominator, |
| | 53 | 166 | | appliedSign, |
| | 53 | 167 | | out Vector3d secondLinear); |
| | 53 | 168 | | resolved &= TryGetAngularVelocityDelta( |
| | 53 | 169 | | secondTangent.Lever, |
| | 53 | 170 | | tangent, |
| | 53 | 171 | | secondTangent.InverseInertia, |
| | 53 | 172 | | appliedNumerator, |
| | 53 | 173 | | appliedDenominator, |
| | 53 | 174 | | appliedSign, |
| | 53 | 175 | | out Vector3d secondAngular); |
| | 53 | 176 | | if (!resolved) |
| | 3 | 177 | | return false; |
| | | 178 | | |
| | 50 | 179 | | bool hasAccumulatedProjection = Fixed64.TryGetSignedRawRatio( |
| | 50 | 180 | | accumulatedNumerator, |
| | 50 | 181 | | accumulatedDenominator, |
| | 50 | 182 | | accumulatedSign < 0, |
| | 50 | 183 | | out Fixed64 accumulatedProjection); |
| | 50 | 184 | | response = new ExactCoulombResponse3D( |
| | 50 | 185 | | !WideArithmetic.IsZeroMagnitude(appliedNumerator), |
| | 50 | 186 | | firstLinear, |
| | 50 | 187 | | firstAngular, |
| | 50 | 188 | | secondLinear, |
| | 50 | 189 | | secondAngular, |
| | 50 | 190 | | hasAccumulatedProjection, |
| | 50 | 191 | | accumulatedProjection, |
| | 50 | 192 | | hasSecondaryAccumulatedImpulse: false, |
| | 50 | 193 | | secondaryAccumulatedImpulse: default); |
| | 50 | 194 | | return true; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 198 | | internal static bool TryGetCoulombDiskResponse( |
| | | 199 | | Vector3d normal, |
| | | 200 | | Fixed64 completedNormalImpulse, |
| | | 201 | | in ExactContactResponseOperand3D primaryFirst, |
| | | 202 | | in ExactContactResponseOperand3D primarySecond, |
| | | 203 | | Vector3d primaryTangent, |
| | | 204 | | Fixed64 accumulatedPrimaryTangentImpulse, |
| | | 205 | | in ExactContactResponseOperand3D secondaryFirst, |
| | | 206 | | in ExactContactResponseOperand3D secondarySecond, |
| | | 207 | | Vector3d secondaryTangent, |
| | | 208 | | Fixed64 accumulatedSecondaryTangentImpulse, |
| | | 209 | | Fixed64 staticFriction, |
| | | 210 | | Fixed64 dynamicFriction, |
| | | 211 | | out ExactCoulombResponse3D response) |
| | | 212 | | { |
| | 386 | 213 | | response = default; |
| | 386 | 214 | | if (completedNormalImpulse < Fixed64.Zero |
| | 386 | 215 | | || !AreCoulombDiskInputsValid( |
| | 386 | 216 | | normal, |
| | 386 | 217 | | primaryFirst, |
| | 386 | 218 | | primarySecond, |
| | 386 | 219 | | primaryTangent, |
| | 386 | 220 | | secondaryFirst, |
| | 386 | 221 | | secondarySecond, |
| | 386 | 222 | | secondaryTangent, |
| | 386 | 223 | | staticFriction, |
| | 386 | 224 | | dynamicFriction)) |
| | | 225 | | { |
| | 2 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | 384 | 229 | | Span<ulong> normalNumerator = stackalloc ulong[MaxResponseWords]; |
| | 384 | 230 | | Span<ulong> normalDenominator = stackalloc ulong[MaxResponseWords]; |
| | 384 | 231 | | SetMagnitude(completedNormalImpulse, normalNumerator); |
| | 384 | 232 | | normalDenominator.Clear(); |
| | 384 | 233 | | normalDenominator[0] = 1UL; |
| | 384 | 234 | | return TryGetCoulombDiskResponseCore( |
| | 384 | 235 | | normalNumerator, |
| | 384 | 236 | | normalDenominator, |
| | 384 | 237 | | primaryFirst, |
| | 384 | 238 | | primarySecond, |
| | 384 | 239 | | primaryTangent, |
| | 384 | 240 | | accumulatedPrimaryTangentImpulse, |
| | 384 | 241 | | secondaryFirst, |
| | 384 | 242 | | secondarySecond, |
| | 384 | 243 | | secondaryTangent, |
| | 384 | 244 | | accumulatedSecondaryTangentImpulse, |
| | 384 | 245 | | staticFriction, |
| | 384 | 246 | | dynamicFriction, |
| | 384 | 247 | | Fixed64.Epsilon, |
| | 384 | 248 | | out response); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 252 | | internal static bool TryGetCoulombDiskResponse( |
| | | 253 | | in ExactNormalConstraint3D normalConstraint, |
| | | 254 | | in ExactContactResponseOperand3D primaryFirst, |
| | | 255 | | in ExactContactResponseOperand3D primarySecond, |
| | | 256 | | Vector3d primaryTangent, |
| | | 257 | | Fixed64 accumulatedPrimaryTangentImpulse, |
| | | 258 | | in ExactContactResponseOperand3D secondaryFirst, |
| | | 259 | | in ExactContactResponseOperand3D secondarySecond, |
| | | 260 | | Vector3d secondaryTangent, |
| | | 261 | | Fixed64 accumulatedSecondaryTangentImpulse, |
| | | 262 | | Fixed64 staticFriction, |
| | | 263 | | Fixed64 dynamicFriction, |
| | | 264 | | out ExactCoulombResponse3D response) |
| | | 265 | | { |
| | 59 | 266 | | response = default; |
| | 59 | 267 | | bool inputsValid = AreCoulombDiskInputsValid( |
| | 59 | 268 | | normalConstraint.Normal, |
| | 59 | 269 | | primaryFirst, |
| | 59 | 270 | | primarySecond, |
| | 59 | 271 | | primaryTangent, |
| | 59 | 272 | | secondaryFirst, |
| | 59 | 273 | | secondarySecond, |
| | 59 | 274 | | secondaryTangent, |
| | 59 | 275 | | staticFriction, |
| | 59 | 276 | | dynamicFriction) |
| | 59 | 277 | | & HaveMatchingParticipants( |
| | 59 | 278 | | normalConstraint.First, |
| | 59 | 279 | | primaryFirst) |
| | 59 | 280 | | & HaveMatchingParticipants( |
| | 59 | 281 | | normalConstraint.Second, |
| | 59 | 282 | | primarySecond) |
| | 59 | 283 | | & HaveMatchingParticipants(primaryFirst, secondaryFirst) |
| | 59 | 284 | | & HaveMatchingParticipants(primarySecond, secondarySecond); |
| | 59 | 285 | | if (!inputsValid) |
| | 3 | 286 | | return false; |
| | | 287 | | |
| | 56 | 288 | | Span<ulong> normalNumerator = stackalloc ulong[MaxResponseWords]; |
| | 56 | 289 | | Span<ulong> normalDenominator = stackalloc ulong[MaxResponseWords]; |
| | 56 | 290 | | if (!TryGetCompletedNormalAccumulatorRatio( |
| | 56 | 291 | | normalConstraint, |
| | 56 | 292 | | normalNumerator, |
| | 56 | 293 | | normalDenominator)) |
| | 1 | 294 | | return false; |
| | | 295 | | |
| | 55 | 296 | | return TryGetCoulombDiskResponseCore( |
| | 55 | 297 | | normalNumerator, |
| | 55 | 298 | | normalDenominator, |
| | 55 | 299 | | primaryFirst, |
| | 55 | 300 | | primarySecond, |
| | 55 | 301 | | primaryTangent, |
| | 55 | 302 | | accumulatedPrimaryTangentImpulse, |
| | 55 | 303 | | secondaryFirst, |
| | 55 | 304 | | secondarySecond, |
| | 55 | 305 | | secondaryTangent, |
| | 55 | 306 | | accumulatedSecondaryTangentImpulse, |
| | 55 | 307 | | staticFriction, |
| | 55 | 308 | | dynamicFriction, |
| | 55 | 309 | | Fixed64.Zero, |
| | 55 | 310 | | out response); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private static bool TryGetCoulombDiskResponseCore( |
| | | 314 | | ReadOnlySpan<ulong> normalNumerator, |
| | | 315 | | ReadOnlySpan<ulong> normalDenominator, |
| | | 316 | | in ExactContactResponseOperand3D primaryFirst, |
| | | 317 | | in ExactContactResponseOperand3D primarySecond, |
| | | 318 | | Vector3d primaryTangent, |
| | | 319 | | Fixed64 accumulatedPrimaryTangentImpulse, |
| | | 320 | | in ExactContactResponseOperand3D secondaryFirst, |
| | | 321 | | in ExactContactResponseOperand3D secondarySecond, |
| | | 322 | | Vector3d secondaryTangent, |
| | | 323 | | Fixed64 accumulatedSecondaryTangentImpulse, |
| | | 324 | | Fixed64 staticFriction, |
| | | 325 | | Fixed64 dynamicFriction, |
| | | 326 | | Fixed64 velocityDeadzone, |
| | | 327 | | out ExactCoulombResponse3D response) |
| | | 328 | | { |
| | 439 | 329 | | response = default; |
| | 439 | 330 | | Span<ulong> primaryNumerator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 331 | | Span<ulong> primaryDenominator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 332 | | Span<ulong> secondaryNumerator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 333 | | Span<ulong> secondaryDenominator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 334 | | GetBilateralImpulseRatio( |
| | 439 | 335 | | primaryFirst, |
| | 439 | 336 | | primarySecond, |
| | 439 | 337 | | primaryTangent, |
| | 439 | 338 | | velocityDeadzone, |
| | 439 | 339 | | primaryNumerator, |
| | 439 | 340 | | primaryDenominator, |
| | 439 | 341 | | out int primarySign); |
| | 439 | 342 | | GetBilateralImpulseRatio( |
| | 439 | 343 | | secondaryFirst, |
| | 439 | 344 | | secondarySecond, |
| | 439 | 345 | | secondaryTangent, |
| | 439 | 346 | | velocityDeadzone, |
| | 439 | 347 | | secondaryNumerator, |
| | 439 | 348 | | secondaryDenominator, |
| | 439 | 349 | | out int secondarySign); |
| | | 350 | | |
| | 439 | 351 | | Span<ulong> desiredPrimaryNumerator = |
| | 439 | 352 | | stackalloc ulong[MaxResponseWords]; |
| | 439 | 353 | | Span<ulong> desiredSecondaryNumerator = |
| | 439 | 354 | | stackalloc ulong[MaxResponseWords]; |
| | 439 | 355 | | AddFixedToRatio( |
| | 439 | 356 | | primaryNumerator, |
| | 439 | 357 | | primaryDenominator, |
| | 439 | 358 | | primarySign, |
| | 439 | 359 | | accumulatedPrimaryTangentImpulse, |
| | 439 | 360 | | desiredPrimaryNumerator, |
| | 439 | 361 | | out int desiredPrimarySign); |
| | 439 | 362 | | AddFixedToRatio( |
| | 439 | 363 | | secondaryNumerator, |
| | 439 | 364 | | secondaryDenominator, |
| | 439 | 365 | | secondarySign, |
| | 439 | 366 | | accumulatedSecondaryTangentImpulse, |
| | 439 | 367 | | desiredSecondaryNumerator, |
| | 439 | 368 | | out int desiredSecondarySign); |
| | | 369 | | |
| | 439 | 370 | | Span<ulong> commonDenominator = stackalloc ulong[MaxCoulombWords]; |
| | 439 | 371 | | Span<ulong> primaryAtCommon = stackalloc ulong[MaxCoulombWords]; |
| | 439 | 372 | | Span<ulong> secondaryAtCommon = stackalloc ulong[MaxCoulombWords]; |
| | 439 | 373 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 374 | | primaryDenominator, |
| | 439 | 375 | | secondaryDenominator, |
| | 439 | 376 | | commonDenominator); |
| | 439 | 377 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 378 | | desiredPrimaryNumerator, |
| | 439 | 379 | | secondaryDenominator, |
| | 439 | 380 | | primaryAtCommon); |
| | 439 | 381 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 382 | | desiredSecondaryNumerator, |
| | 439 | 383 | | primaryDenominator, |
| | 439 | 384 | | secondaryAtCommon); |
| | | 385 | | |
| | 439 | 386 | | Span<ulong> magnitudeSquared = stackalloc ulong[MaxCoulombSquareWords]; |
| | 439 | 387 | | Span<ulong> square = stackalloc ulong[MaxCoulombSquareWords]; |
| | 439 | 388 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 389 | | primaryAtCommon, |
| | 439 | 390 | | primaryAtCommon, |
| | 439 | 391 | | magnitudeSquared); |
| | 439 | 392 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 393 | | secondaryAtCommon, |
| | 439 | 394 | | secondaryAtCommon, |
| | 439 | 395 | | square); |
| | 439 | 396 | | WideArithmetic.AddMagnitudeInto(square, magnitudeSquared); |
| | | 397 | | |
| | 439 | 398 | | Span<ulong> staticNumerator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 399 | | Span<ulong> staticDenominator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 400 | | GetFrictionLimit( |
| | 439 | 401 | | normalNumerator, |
| | 439 | 402 | | normalDenominator, |
| | 439 | 403 | | staticFriction, |
| | 439 | 404 | | staticNumerator, |
| | 439 | 405 | | staticDenominator); |
| | 439 | 406 | | bool withinStaticLimit = IsVectorWithinLimit( |
| | 439 | 407 | | magnitudeSquared, |
| | 439 | 408 | | commonDenominator, |
| | 439 | 409 | | staticNumerator, |
| | 439 | 410 | | staticDenominator); |
| | | 411 | | |
| | 439 | 412 | | Span<ulong> dynamicNumerator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 413 | | Span<ulong> dynamicDenominator = stackalloc ulong[MaxResponseWords]; |
| | 439 | 414 | | if (!withinStaticLimit) |
| | | 415 | | { |
| | 225 | 416 | | GetFrictionLimit( |
| | 225 | 417 | | normalNumerator, |
| | 225 | 418 | | normalDenominator, |
| | 225 | 419 | | dynamicFriction, |
| | 225 | 420 | | dynamicNumerator, |
| | 225 | 421 | | dynamicDenominator); |
| | | 422 | | } |
| | | 423 | | |
| | 439 | 424 | | bool useDynamicProjection = |
| | 439 | 425 | | !withinStaticLimit |
| | 439 | 426 | | && !WideArithmetic.IsZeroMagnitude(magnitudeSquared) |
| | 439 | 427 | | && !WideArithmetic.IsZeroMagnitude(dynamicNumerator); |
| | | 428 | | bool hasAppliedImpulse; |
| | | 429 | | bool resolved; |
| | | 430 | | Vector3d firstLinear; |
| | | 431 | | Vector3d firstAngular; |
| | | 432 | | Vector3d secondLinear; |
| | | 433 | | Vector3d secondAngular; |
| | 439 | 434 | | if (useDynamicProjection) |
| | | 435 | | { |
| | 217 | 436 | | hasAppliedImpulse = |
| | 217 | 437 | | !IsRadialProjectionEqualToFixed( |
| | 217 | 438 | | primaryAtCommon, |
| | 217 | 439 | | desiredPrimarySign, |
| | 217 | 440 | | dynamicNumerator, |
| | 217 | 441 | | dynamicDenominator, |
| | 217 | 442 | | magnitudeSquared, |
| | 217 | 443 | | accumulatedPrimaryTangentImpulse) |
| | 217 | 444 | | || !IsRadialProjectionEqualToFixed( |
| | 217 | 445 | | secondaryAtCommon, |
| | 217 | 446 | | desiredSecondarySign, |
| | 217 | 447 | | dynamicNumerator, |
| | 217 | 448 | | dynamicDenominator, |
| | 217 | 449 | | magnitudeSquared, |
| | 217 | 450 | | accumulatedSecondaryTangentImpulse); |
| | 217 | 451 | | resolved = TryGetDiskVelocityDeltas( |
| | 217 | 452 | | primaryFirst, |
| | 217 | 453 | | primarySecond, |
| | 217 | 454 | | secondaryFirst, |
| | 217 | 455 | | secondarySecond, |
| | 217 | 456 | | primaryTangent, |
| | 217 | 457 | | secondaryTangent, |
| | 217 | 458 | | primaryAtCommon, |
| | 217 | 459 | | desiredPrimarySign, |
| | 217 | 460 | | secondaryAtCommon, |
| | 217 | 461 | | desiredSecondarySign, |
| | 217 | 462 | | commonDenominator, |
| | 217 | 463 | | rational: false, |
| | 217 | 464 | | dynamicNumerator, |
| | 217 | 465 | | dynamicDenominator, |
| | 217 | 466 | | magnitudeSquared, |
| | 217 | 467 | | accumulatedPrimaryTangentImpulse, |
| | 217 | 468 | | accumulatedSecondaryTangentImpulse, |
| | 217 | 469 | | out firstLinear, |
| | 217 | 470 | | out firstAngular, |
| | 217 | 471 | | out secondLinear, |
| | 217 | 472 | | out secondAngular); |
| | | 473 | | } |
| | | 474 | | else |
| | | 475 | | { |
| | 222 | 476 | | Span<ulong> zero = stackalloc ulong[MaxCoulombWords]; |
| | 222 | 477 | | Span<ulong> appliedPrimaryNumerator = |
| | 222 | 478 | | stackalloc ulong[MaxCoulombWords]; |
| | 222 | 479 | | Span<ulong> appliedDenominator = |
| | 222 | 480 | | stackalloc ulong[MaxCoulombWords]; |
| | 222 | 481 | | Span<ulong> appliedSecondaryNumerator = |
| | 222 | 482 | | stackalloc ulong[MaxCoulombWords]; |
| | 222 | 483 | | zero.Clear(); |
| | 222 | 484 | | SubtractFixedFromRatio( |
| | 222 | 485 | | withinStaticLimit ? primaryAtCommon : zero, |
| | 222 | 486 | | commonDenominator, |
| | 222 | 487 | | withinStaticLimit ? desiredPrimarySign : 0, |
| | 222 | 488 | | accumulatedPrimaryTangentImpulse, |
| | 222 | 489 | | appliedPrimaryNumerator, |
| | 222 | 490 | | appliedDenominator, |
| | 222 | 491 | | out int appliedPrimarySign); |
| | 222 | 492 | | SubtractFixedFromRatio( |
| | 222 | 493 | | withinStaticLimit ? secondaryAtCommon : zero, |
| | 222 | 494 | | commonDenominator, |
| | 222 | 495 | | withinStaticLimit ? desiredSecondarySign : 0, |
| | 222 | 496 | | accumulatedSecondaryTangentImpulse, |
| | 222 | 497 | | appliedSecondaryNumerator, |
| | 222 | 498 | | appliedDenominator, |
| | 222 | 499 | | out int appliedSecondarySign); |
| | 222 | 500 | | hasAppliedImpulse = |
| | 222 | 501 | | appliedPrimarySign != 0 |
| | 222 | 502 | | || appliedSecondarySign != 0; |
| | 222 | 503 | | resolved = TryGetDiskVelocityDeltas( |
| | 222 | 504 | | primaryFirst, |
| | 222 | 505 | | primarySecond, |
| | 222 | 506 | | secondaryFirst, |
| | 222 | 507 | | secondarySecond, |
| | 222 | 508 | | primaryTangent, |
| | 222 | 509 | | secondaryTangent, |
| | 222 | 510 | | appliedPrimaryNumerator, |
| | 222 | 511 | | appliedPrimarySign, |
| | 222 | 512 | | appliedSecondaryNumerator, |
| | 222 | 513 | | appliedSecondarySign, |
| | 222 | 514 | | appliedDenominator, |
| | 222 | 515 | | rational: true, |
| | 222 | 516 | | dynamicNumerator, |
| | 222 | 517 | | dynamicDenominator, |
| | 222 | 518 | | magnitudeSquared, |
| | 222 | 519 | | Fixed64.Zero, |
| | 222 | 520 | | Fixed64.Zero, |
| | 222 | 521 | | out firstLinear, |
| | 222 | 522 | | out firstAngular, |
| | 222 | 523 | | out secondLinear, |
| | 222 | 524 | | out secondAngular); |
| | | 525 | | } |
| | 439 | 526 | | if (!resolved) |
| | 6 | 527 | | return false; |
| | | 528 | | |
| | | 529 | | bool hasPrimaryProjection; |
| | | 530 | | bool hasSecondaryProjection; |
| | | 531 | | Fixed64 primaryProjection; |
| | | 532 | | Fixed64 secondaryProjection; |
| | 433 | 533 | | if (withinStaticLimit) |
| | | 534 | | { |
| | 213 | 535 | | hasPrimaryProjection = Fixed64.TryGetSignedRawRatio( |
| | 213 | 536 | | primaryAtCommon, |
| | 213 | 537 | | commonDenominator, |
| | 213 | 538 | | desiredPrimarySign < 0, |
| | 213 | 539 | | out primaryProjection); |
| | 213 | 540 | | hasSecondaryProjection = Fixed64.TryGetSignedRawRatio( |
| | 213 | 541 | | secondaryAtCommon, |
| | 213 | 542 | | commonDenominator, |
| | 213 | 543 | | desiredSecondarySign < 0, |
| | 213 | 544 | | out secondaryProjection); |
| | | 545 | | } |
| | 220 | 546 | | else if (useDynamicProjection) |
| | | 547 | | { |
| | 214 | 548 | | Span<ulong> projectedNumerator = |
| | 214 | 549 | | stackalloc ulong[MaxCoulombWords + MaxResponseWords]; |
| | 214 | 550 | | WideArithmetic.MultiplyMagnitudes( |
| | 214 | 551 | | primaryAtCommon, |
| | 214 | 552 | | dynamicNumerator, |
| | 214 | 553 | | projectedNumerator); |
| | 214 | 554 | | hasPrimaryProjection = TryGetSignedRatioOverSquareRoot( |
| | 214 | 555 | | projectedNumerator, |
| | 214 | 556 | | dynamicDenominator, |
| | 214 | 557 | | magnitudeSquared, |
| | 214 | 558 | | desiredPrimarySign < 0, |
| | 214 | 559 | | out primaryProjection); |
| | 214 | 560 | | WideArithmetic.MultiplyMagnitudes( |
| | 214 | 561 | | secondaryAtCommon, |
| | 214 | 562 | | dynamicNumerator, |
| | 214 | 563 | | projectedNumerator); |
| | 214 | 564 | | hasSecondaryProjection = TryGetSignedRatioOverSquareRoot( |
| | 214 | 565 | | projectedNumerator, |
| | 214 | 566 | | dynamicDenominator, |
| | 214 | 567 | | magnitudeSquared, |
| | 214 | 568 | | desiredSecondarySign < 0, |
| | 214 | 569 | | out secondaryProjection); |
| | | 570 | | } |
| | | 571 | | else |
| | | 572 | | { |
| | 6 | 573 | | hasPrimaryProjection = true; |
| | 6 | 574 | | hasSecondaryProjection = true; |
| | 6 | 575 | | primaryProjection = Fixed64.Zero; |
| | 6 | 576 | | secondaryProjection = Fixed64.Zero; |
| | | 577 | | } |
| | | 578 | | |
| | 433 | 579 | | response = new ExactCoulombResponse3D( |
| | 433 | 580 | | hasAppliedImpulse, |
| | 433 | 581 | | firstLinear, |
| | 433 | 582 | | firstAngular, |
| | 433 | 583 | | secondLinear, |
| | 433 | 584 | | secondAngular, |
| | 433 | 585 | | hasPrimaryProjection, |
| | 433 | 586 | | primaryProjection, |
| | 433 | 587 | | hasSecondaryProjection, |
| | 433 | 588 | | secondaryProjection); |
| | 433 | 589 | | return true; |
| | | 590 | | } |
| | | 591 | | |
| | | 592 | | private static bool AreCoulombDiskInputsValid( |
| | | 593 | | Vector3d normal, |
| | | 594 | | in ExactContactResponseOperand3D primaryFirst, |
| | | 595 | | in ExactContactResponseOperand3D primarySecond, |
| | | 596 | | Vector3d primaryTangent, |
| | | 597 | | in ExactContactResponseOperand3D secondaryFirst, |
| | | 598 | | in ExactContactResponseOperand3D secondarySecond, |
| | | 599 | | Vector3d secondaryTangent, |
| | | 600 | | Fixed64 staticFriction, |
| | | 601 | | Fixed64 dynamicFriction) => |
| | 444 | 602 | | staticFriction >= Fixed64.Zero |
| | 444 | 603 | | & dynamicFriction >= Fixed64.Zero |
| | 444 | 604 | | & primaryFirst.Lever.Denominator.Sign != 0 |
| | 444 | 605 | | & primarySecond.Lever.Denominator.Sign != 0 |
| | 444 | 606 | | & primaryFirst.InverseMass >= Fixed64.Zero |
| | 444 | 607 | | & primarySecond.InverseMass >= Fixed64.Zero |
| | 444 | 608 | | & normal.IsNormalized() |
| | 444 | 609 | | & primaryTangent.IsNormalized() |
| | 444 | 610 | | & secondaryTangent.IsNormalized() |
| | 444 | 611 | | & FixedMath.Abs(Vector3d.Dot( |
| | 444 | 612 | | primaryTangent, |
| | 444 | 613 | | secondaryTangent)) <= Fixed64.Epsilon |
| | 444 | 614 | | & FixedMath.Abs(Vector3d.Dot( |
| | 444 | 615 | | normal, |
| | 444 | 616 | | primaryTangent)) <= Fixed64.Epsilon |
| | 444 | 617 | | & FixedMath.Abs(Vector3d.Dot( |
| | 444 | 618 | | normal, |
| | 444 | 619 | | secondaryTangent)) <= Fixed64.Epsilon |
| | 444 | 620 | | & HaveMatchingParticipants(primaryFirst, secondaryFirst) |
| | 444 | 621 | | & HaveMatchingParticipants(primarySecond, secondarySecond); |
| | | 622 | | |
| | | 623 | | private static bool TryGetCompletedNormalAccumulatorRatio( |
| | | 624 | | in ExactNormalConstraint3D constraint, |
| | | 625 | | Span<ulong> numerator, |
| | | 626 | | Span<ulong> denominator) |
| | | 627 | | { |
| | 110 | 628 | | numerator.Clear(); |
| | 110 | 629 | | denominator.Clear(); |
| | 110 | 630 | | ExactContactResponseOperand3D first = constraint.First; |
| | 110 | 631 | | ExactContactResponseOperand3D second = constraint.Second; |
| | 110 | 632 | | bool inputsValid = |
| | 110 | 633 | | first.Lever.Denominator.Sign != 0 |
| | 110 | 634 | | & second.Lever.Denominator.Sign != 0 |
| | 110 | 635 | | & constraint.Restitution >= Fixed64.Zero |
| | 110 | 636 | | & constraint.RestitutionVelocityThreshold >= Fixed64.Zero |
| | 110 | 637 | | & constraint.AccumulatedImpulse >= Fixed64.Zero |
| | 110 | 638 | | & constraint.PositiveImpulseScale >= Fixed64.Zero |
| | 110 | 639 | | & constraint.NegativeImpulseScale >= Fixed64.Zero |
| | 110 | 640 | | & first.InverseMass >= Fixed64.Zero |
| | 110 | 641 | | & second.InverseMass >= Fixed64.Zero |
| | 110 | 642 | | & constraint.Normal.IsNormalized(); |
| | 110 | 643 | | if (!inputsValid) |
| | 2 | 644 | | return false; |
| | | 645 | | |
| | 108 | 646 | | ExactLever3D.GetRelativePointVelocityRatio( |
| | 108 | 647 | | first.LinearVelocity, |
| | 108 | 648 | | first.AngularVelocity, |
| | 108 | 649 | | first.Lever, |
| | 108 | 650 | | second.LinearVelocity, |
| | 108 | 651 | | second.AngularVelocity, |
| | 108 | 652 | | second.Lever, |
| | 108 | 653 | | constraint.Normal, |
| | 108 | 654 | | out Signed832 velocityNumerator, |
| | 108 | 655 | | out Signed832 velocityDenominator); |
| | | 656 | | |
| | 108 | 657 | | Span<ulong> effectiveNumerator = stackalloc ulong[MaxResponseWords]; |
| | 108 | 658 | | Span<ulong> effectiveDenominator = stackalloc ulong[MaxResponseWords]; |
| | 108 | 659 | | GetEffectiveMassRatio( |
| | 108 | 660 | | first, |
| | 108 | 661 | | second, |
| | 108 | 662 | | constraint.Normal, |
| | 108 | 663 | | effectiveNumerator, |
| | 108 | 664 | | effectiveDenominator); |
| | 108 | 665 | | int velocitySign = |
| | 108 | 666 | | velocityNumerator.Sign * velocityDenominator.Sign; |
| | 108 | 667 | | if (velocitySign == 0 |
| | 108 | 668 | | || WideArithmetic.IsZeroMagnitude(effectiveNumerator)) |
| | | 669 | | { |
| | 12 | 670 | | SetMagnitude(constraint.AccumulatedImpulse, numerator); |
| | 12 | 671 | | denominator[0] = 1UL; |
| | 12 | 672 | | return true; |
| | | 673 | | } |
| | | 674 | | |
| | 96 | 675 | | bool hasVelocityProjection = Fixed64.TryGetSignedRawRatio( |
| | 96 | 676 | | velocityNumerator, |
| | 96 | 677 | | velocityDenominator, |
| | 96 | 678 | | 0, |
| | 96 | 679 | | out Fixed64 velocityProjection); |
| | 96 | 680 | | bool applyRestitution = velocitySign < 0 |
| | 96 | 681 | | & (!hasVelocityProjection |
| | 96 | 682 | | | velocityProjection |
| | 96 | 683 | | < -constraint.RestitutionVelocityThreshold); |
| | 96 | 684 | | Fixed64 scale = velocitySign < 0 |
| | 96 | 685 | | ? constraint.PositiveImpulseScale |
| | 96 | 686 | | : constraint.NegativeImpulseScale; |
| | 96 | 687 | | Span<ulong> impulseNumerator = stackalloc ulong[MaxResponseWords]; |
| | 96 | 688 | | Span<ulong> impulseDenominator = stackalloc ulong[MaxResponseWords]; |
| | 96 | 689 | | BuildImpulseRatio( |
| | 96 | 690 | | velocityNumerator, |
| | 96 | 691 | | velocityDenominator, |
| | 96 | 692 | | effectiveNumerator, |
| | 96 | 693 | | effectiveDenominator, |
| | 96 | 694 | | applyRestitution ? constraint.Restitution : Fixed64.Zero, |
| | 96 | 695 | | scale, |
| | 96 | 696 | | impulseNumerator, |
| | 96 | 697 | | impulseDenominator); |
| | | 698 | | |
| | 96 | 699 | | Span<ulong> accumulatorMagnitude = |
| | 96 | 700 | | stackalloc ulong[MaxResponseWords]; |
| | 96 | 701 | | Span<ulong> accumulatorAtImpulseDenominator = |
| | 96 | 702 | | stackalloc ulong[MaxResponseWords]; |
| | 96 | 703 | | SetMagnitude(constraint.AccumulatedImpulse, accumulatorMagnitude); |
| | 96 | 704 | | WideArithmetic.MultiplyMagnitudes( |
| | 96 | 705 | | accumulatorMagnitude, |
| | 96 | 706 | | impulseDenominator, |
| | 96 | 707 | | accumulatorAtImpulseDenominator); |
| | 96 | 708 | | int impulseSign = -velocitySign; |
| | 96 | 709 | | if (impulseSign < 0 |
| | 96 | 710 | | && WideArithmetic.CompareMagnitudeEqualLength( |
| | 96 | 711 | | impulseNumerator, |
| | 96 | 712 | | accumulatorAtImpulseDenominator) >= 0) |
| | | 713 | | { |
| | 1 | 714 | | denominator[0] = 1UL; |
| | 1 | 715 | | return true; |
| | | 716 | | } |
| | | 717 | | |
| | 95 | 718 | | accumulatorAtImpulseDenominator.CopyTo(numerator); |
| | 95 | 719 | | if (impulseSign > 0) |
| | 93 | 720 | | WideArithmetic.AddMagnitudeInto(impulseNumerator, numerator); |
| | | 721 | | else |
| | 2 | 722 | | WideArithmetic.SubtractEqualMagnitudes( |
| | 2 | 723 | | accumulatorAtImpulseDenominator, |
| | 2 | 724 | | impulseNumerator, |
| | 2 | 725 | | numerator); |
| | 95 | 726 | | impulseDenominator.CopyTo(denominator); |
| | 95 | 727 | | return true; |
| | | 728 | | } |
| | | 729 | | |
| | | 730 | | private static void GetBilateralImpulseRatio( |
| | | 731 | | in ExactContactResponseOperand3D first, |
| | | 732 | | in ExactContactResponseOperand3D second, |
| | | 733 | | Vector3d tangent, |
| | | 734 | | Fixed64 velocityDeadzone, |
| | | 735 | | Span<ulong> numerator, |
| | | 736 | | Span<ulong> denominator, |
| | | 737 | | out int sign) |
| | | 738 | | { |
| | 931 | 739 | | numerator.Clear(); |
| | 931 | 740 | | denominator.Clear(); |
| | 931 | 741 | | sign = 0; |
| | 931 | 742 | | ExactLever3D.GetRelativePointVelocityRatio( |
| | 931 | 743 | | first.LinearVelocity, |
| | 931 | 744 | | first.AngularVelocity, |
| | 931 | 745 | | first.Lever, |
| | 931 | 746 | | second.LinearVelocity, |
| | 931 | 747 | | second.AngularVelocity, |
| | 931 | 748 | | second.Lever, |
| | 931 | 749 | | tangent, |
| | 931 | 750 | | out Signed832 velocityNumerator, |
| | 931 | 751 | | out Signed832 velocityDenominator); |
| | | 752 | | |
| | 931 | 753 | | Span<ulong> effectiveNumerator = stackalloc ulong[MaxResponseWords]; |
| | 931 | 754 | | Span<ulong> effectiveDenominator = stackalloc ulong[MaxResponseWords]; |
| | 931 | 755 | | GetEffectiveMassRatio( |
| | 931 | 756 | | first, |
| | 931 | 757 | | second, |
| | 931 | 758 | | tangent, |
| | 931 | 759 | | effectiveNumerator, |
| | 931 | 760 | | effectiveDenominator); |
| | 931 | 761 | | int velocitySign = |
| | 931 | 762 | | velocityNumerator.Sign * velocityDenominator.Sign; |
| | 931 | 763 | | bool suppressVelocity = velocityDeadzone != Fixed64.Zero |
| | 931 | 764 | | && Fixed64.TryGetSignedRawRatio( |
| | 931 | 765 | | velocityNumerator, |
| | 931 | 766 | | velocityDenominator, |
| | 931 | 767 | | 0, |
| | 931 | 768 | | out Fixed64 velocityProjection) |
| | 931 | 769 | | && velocityProjection >= -velocityDeadzone |
| | 931 | 770 | | && velocityProjection <= velocityDeadzone; |
| | 931 | 771 | | if (velocitySign == 0 |
| | 931 | 772 | | || suppressVelocity |
| | 931 | 773 | | || WideArithmetic.IsZeroMagnitude(effectiveNumerator)) |
| | | 774 | | { |
| | 467 | 775 | | denominator[0] = 1UL; |
| | 467 | 776 | | return; |
| | | 777 | | } |
| | | 778 | | |
| | 464 | 779 | | BuildImpulseRatio( |
| | 464 | 780 | | velocityNumerator, |
| | 464 | 781 | | velocityDenominator, |
| | 464 | 782 | | effectiveNumerator, |
| | 464 | 783 | | effectiveDenominator, |
| | 464 | 784 | | Fixed64.Zero, |
| | 464 | 785 | | Fixed64.One, |
| | 464 | 786 | | numerator, |
| | 464 | 787 | | denominator); |
| | 464 | 788 | | sign = -velocitySign; |
| | 464 | 789 | | } |
| | | 790 | | |
| | | 791 | | private static void GetFrictionLimit( |
| | | 792 | | ReadOnlySpan<ulong> normalNumerator, |
| | | 793 | | ReadOnlySpan<ulong> normalDenominator, |
| | | 794 | | Fixed64 friction, |
| | | 795 | | Span<ulong> numerator, |
| | | 796 | | Span<ulong> denominator) |
| | | 797 | | { |
| | 733 | 798 | | Span<ulong> frictionMagnitude = |
| | 733 | 799 | | stackalloc ulong[3]; |
| | 733 | 800 | | Span<ulong> fixedScale = |
| | 733 | 801 | | stackalloc ulong[1]; |
| | 733 | 802 | | SetMagnitude(friction, frictionMagnitude); |
| | 733 | 803 | | fixedScale.Clear(); |
| | 733 | 804 | | fixedScale[0] = (ulong)FixedMath.ONE_L; |
| | 733 | 805 | | WideArithmetic.MultiplyMagnitudes( |
| | 733 | 806 | | normalNumerator, |
| | 733 | 807 | | frictionMagnitude, |
| | 733 | 808 | | numerator); |
| | 733 | 809 | | WideArithmetic.MultiplyMagnitudes( |
| | 733 | 810 | | normalDenominator, |
| | 733 | 811 | | fixedScale, |
| | 733 | 812 | | denominator); |
| | 733 | 813 | | } |
| | | 814 | | |
| | | 815 | | private static void AddFixedToRatio( |
| | | 816 | | ReadOnlySpan<ulong> ratioNumerator, |
| | | 817 | | ReadOnlySpan<ulong> ratioDenominator, |
| | | 818 | | int ratioSign, |
| | | 819 | | Fixed64 value, |
| | | 820 | | Span<ulong> resultNumerator, |
| | | 821 | | out int resultSign) |
| | | 822 | | { |
| | 931 | 823 | | Span<ulong> valueMagnitude = stackalloc ulong[3]; |
| | 931 | 824 | | Span<ulong> valueAtDenominator = |
| | 931 | 825 | | stackalloc ulong[MaxResponseWords]; |
| | 931 | 826 | | SetMagnitude(value, valueMagnitude); |
| | 931 | 827 | | WideArithmetic.MultiplyMagnitudes( |
| | 931 | 828 | | valueMagnitude, |
| | 931 | 829 | | ratioDenominator, |
| | 931 | 830 | | valueAtDenominator); |
| | 931 | 831 | | AddSignedMagnitudes( |
| | 931 | 832 | | ratioNumerator, |
| | 931 | 833 | | ratioSign, |
| | 931 | 834 | | valueAtDenominator, |
| | 931 | 835 | | value == Fixed64.Zero ? 0 : value < Fixed64.Zero ? -1 : 1, |
| | 931 | 836 | | resultNumerator, |
| | 931 | 837 | | out resultSign); |
| | 931 | 838 | | } |
| | | 839 | | |
| | | 840 | | private static void SubtractFixedFromRatio( |
| | | 841 | | ReadOnlySpan<ulong> ratioNumerator, |
| | | 842 | | ReadOnlySpan<ulong> ratioDenominator, |
| | | 843 | | int ratioSign, |
| | | 844 | | Fixed64 value, |
| | | 845 | | Span<ulong> resultNumerator, |
| | | 846 | | Span<ulong> resultDenominator, |
| | | 847 | | out int resultSign) |
| | | 848 | | { |
| | 460 | 849 | | Span<ulong> valueMagnitude = stackalloc ulong[3]; |
| | 460 | 850 | | Span<ulong> valueAtDenominator = |
| | 460 | 851 | | stackalloc ulong[MaxCoulombWords]; |
| | 460 | 852 | | SetMagnitude(value, valueMagnitude); |
| | 460 | 853 | | WideArithmetic.MultiplyMagnitudes( |
| | 460 | 854 | | valueMagnitude, |
| | 460 | 855 | | ratioDenominator, |
| | 460 | 856 | | valueAtDenominator); |
| | 460 | 857 | | AddSignedMagnitudes( |
| | 460 | 858 | | ratioNumerator, |
| | 460 | 859 | | ratioSign, |
| | 460 | 860 | | valueAtDenominator, |
| | 460 | 861 | | value == Fixed64.Zero ? 0 : value < Fixed64.Zero ? 1 : -1, |
| | 460 | 862 | | resultNumerator, |
| | 460 | 863 | | out resultSign); |
| | 460 | 864 | | resultDenominator.Clear(); |
| | 460 | 865 | | ratioDenominator.CopyTo(resultDenominator); |
| | 460 | 866 | | } |
| | | 867 | | |
| | | 868 | | private static int CompareRatios( |
| | | 869 | | ReadOnlySpan<ulong> firstNumerator, |
| | | 870 | | ReadOnlySpan<ulong> firstDenominator, |
| | | 871 | | ReadOnlySpan<ulong> secondNumerator, |
| | | 872 | | ReadOnlySpan<ulong> secondDenominator) |
| | | 873 | | { |
| | 53 | 874 | | Span<ulong> first = stackalloc ulong[MaxCoulombWords]; |
| | 53 | 875 | | Span<ulong> second = stackalloc ulong[MaxCoulombWords]; |
| | 53 | 876 | | WideArithmetic.MultiplyMagnitudes( |
| | 53 | 877 | | firstNumerator, |
| | 53 | 878 | | secondDenominator, |
| | 53 | 879 | | first); |
| | 53 | 880 | | WideArithmetic.MultiplyMagnitudes( |
| | 53 | 881 | | secondNumerator, |
| | 53 | 882 | | firstDenominator, |
| | 53 | 883 | | second); |
| | 53 | 884 | | return WideArithmetic.CompareMagnitudeEqualLength(first, second); |
| | | 885 | | } |
| | | 886 | | |
| | | 887 | | private static bool IsVectorWithinLimit( |
| | | 888 | | ReadOnlySpan<ulong> vectorMagnitudeSquared, |
| | | 889 | | ReadOnlySpan<ulong> vectorDenominator, |
| | | 890 | | ReadOnlySpan<ulong> limitNumerator, |
| | | 891 | | ReadOnlySpan<ulong> limitDenominator) |
| | | 892 | | { |
| | 439 | 893 | | Span<ulong> denominatorSquared = |
| | 439 | 894 | | stackalloc ulong[MaxCoulombSquareWords]; |
| | 439 | 895 | | Span<ulong> limitNumeratorSquared = |
| | 439 | 896 | | stackalloc ulong[MaxCoulombSquareWords]; |
| | 439 | 897 | | Span<ulong> limitDenominatorSquared = |
| | 439 | 898 | | stackalloc ulong[MaxCoulombSquareWords]; |
| | 439 | 899 | | Span<ulong> left = stackalloc ulong[MaxCoulombComparisonWords]; |
| | 439 | 900 | | Span<ulong> right = stackalloc ulong[MaxCoulombComparisonWords]; |
| | 439 | 901 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 902 | | vectorDenominator, |
| | 439 | 903 | | vectorDenominator, |
| | 439 | 904 | | denominatorSquared); |
| | 439 | 905 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 906 | | limitNumerator, |
| | 439 | 907 | | limitNumerator, |
| | 439 | 908 | | limitNumeratorSquared); |
| | 439 | 909 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 910 | | limitDenominator, |
| | 439 | 911 | | limitDenominator, |
| | 439 | 912 | | limitDenominatorSquared); |
| | 439 | 913 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 914 | | vectorMagnitudeSquared, |
| | 439 | 915 | | limitDenominatorSquared, |
| | 439 | 916 | | left); |
| | 439 | 917 | | WideArithmetic.MultiplyMagnitudes( |
| | 439 | 918 | | limitNumeratorSquared, |
| | 439 | 919 | | denominatorSquared, |
| | 439 | 920 | | right); |
| | 439 | 921 | | return WideArithmetic.CompareMagnitudeEqualLength(left, right) <= 0; |
| | | 922 | | } |
| | | 923 | | |
| | | 924 | | private static void AddSignedMagnitudes( |
| | | 925 | | ReadOnlySpan<ulong> first, |
| | | 926 | | int firstSign, |
| | | 927 | | ReadOnlySpan<ulong> second, |
| | | 928 | | int secondSign, |
| | | 929 | | Span<ulong> result, |
| | | 930 | | out int resultSign) |
| | | 931 | | { |
| | 64205 | 932 | | result.Clear(); |
| | 64205 | 933 | | resultSign = 0; |
| | 64205 | 934 | | if (firstSign != 0 & !WideArithmetic.IsZeroMagnitude(first)) |
| | 50021 | 935 | | WideArithmetic.AddSignedMagnitude( |
| | 50021 | 936 | | first, |
| | 50021 | 937 | | firstSign, |
| | 50021 | 938 | | result, |
| | 50021 | 939 | | ref resultSign); |
| | 64205 | 940 | | if (secondSign != 0 & !WideArithmetic.IsZeroMagnitude(second)) |
| | 54775 | 941 | | WideArithmetic.AddSignedMagnitude( |
| | 54775 | 942 | | second, |
| | 54775 | 943 | | secondSign, |
| | 54775 | 944 | | result, |
| | 54775 | 945 | | ref resultSign); |
| | 64205 | 946 | | } |
| | | 947 | | |
| | | 948 | | private static bool TryGetSignedRatioOverSquareRoot( |
| | | 949 | | ReadOnlySpan<ulong> numerator, |
| | | 950 | | ReadOnlySpan<ulong> denominator, |
| | | 951 | | ReadOnlySpan<ulong> radicand, |
| | | 952 | | bool negative, |
| | | 953 | | out Fixed64 result) |
| | | 954 | | { |
| | 428 | 955 | | if (WideArithmetic.IsZeroMagnitude(numerator)) |
| | | 956 | | { |
| | 206 | 957 | | result = Fixed64.Zero; |
| | 206 | 958 | | return true; |
| | | 959 | | } |
| | | 960 | | |
| | 222 | 961 | | ulong limit = negative ? 1UL << 63 : (ulong)long.MaxValue; |
| | 222 | 962 | | if (CompareRatioOverSquareRoot( |
| | 222 | 963 | | numerator, |
| | 222 | 964 | | denominator, |
| | 222 | 965 | | radicand, |
| | 222 | 966 | | limit) > 0) |
| | | 967 | | { |
| | 2 | 968 | | result = default; |
| | 2 | 969 | | return false; |
| | | 970 | | } |
| | | 971 | | |
| | 220 | 972 | | ulong low = 0UL; |
| | 220 | 973 | | ulong high = limit; |
| | 14083 | 974 | | while (low < high) |
| | | 975 | | { |
| | 13863 | 976 | | ulong difference = high - low; |
| | 13863 | 977 | | ulong middle = low + (difference >> 1) + (difference & 1UL); |
| | 13863 | 978 | | if (CompareRatioOverSquareRoot( |
| | 13863 | 979 | | numerator, |
| | 13863 | 980 | | denominator, |
| | 13863 | 981 | | radicand, |
| | 13863 | 982 | | middle) >= 0) |
| | | 983 | | { |
| | 3394 | 984 | | low = middle; |
| | | 985 | | } |
| | | 986 | | else |
| | | 987 | | { |
| | 10469 | 988 | | high = middle - 1UL; |
| | | 989 | | } |
| | | 990 | | } |
| | | 991 | | |
| | 220 | 992 | | ulong quotient = low; |
| | 220 | 993 | | Span<ulong> doubledQuotient = stackalloc ulong[2]; |
| | 220 | 994 | | doubledQuotient[0] = (quotient << 1) | 1UL; |
| | 220 | 995 | | doubledQuotient[1] = quotient >> 63; |
| | 220 | 996 | | int midpointComparison = CompareRatioOverSquareRoot( |
| | 220 | 997 | | numerator, |
| | 220 | 998 | | denominator, |
| | 220 | 999 | | radicand, |
| | 220 | 1000 | | doubledQuotient, |
| | 220 | 1001 | | numeratorMultiplier: 2UL); |
| | 220 | 1002 | | bool roundUp = midpointComparison > 0 |
| | 220 | 1003 | | | (midpointComparison == 0 & (quotient & 1UL) != 0UL); |
| | 220 | 1004 | | if (roundUp & quotient < limit) |
| | | 1005 | | { |
| | 10 | 1006 | | quotient++; |
| | | 1007 | | } |
| | | 1008 | | |
| | 220 | 1009 | | result = new Fixed64( |
| | 220 | 1010 | | negative ? unchecked(-(long)quotient) : (long)quotient); |
| | 220 | 1011 | | return true; |
| | | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | private static int CompareRatioOverSquareRoot( |
| | | 1015 | | ReadOnlySpan<ulong> numerator, |
| | | 1016 | | ReadOnlySpan<ulong> denominator, |
| | | 1017 | | ReadOnlySpan<ulong> radicand, |
| | | 1018 | | ulong candidate) |
| | | 1019 | | { |
| | 14085 | 1020 | | Span<ulong> candidateMagnitude = stackalloc ulong[1]; |
| | 14085 | 1021 | | candidateMagnitude.Clear(); |
| | 14085 | 1022 | | candidateMagnitude[0] = candidate; |
| | 14085 | 1023 | | return CompareRatioOverSquareRoot( |
| | 14085 | 1024 | | numerator, |
| | 14085 | 1025 | | denominator, |
| | 14085 | 1026 | | radicand, |
| | 14085 | 1027 | | candidateMagnitude, |
| | 14085 | 1028 | | numeratorMultiplier: 1UL); |
| | | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private static int CompareRatioOverSquareRoot( |
| | | 1032 | | ReadOnlySpan<ulong> numerator, |
| | | 1033 | | ReadOnlySpan<ulong> denominator, |
| | | 1034 | | ReadOnlySpan<ulong> radicand, |
| | | 1035 | | ReadOnlySpan<ulong> candidate, |
| | | 1036 | | ulong numeratorMultiplier) |
| | | 1037 | | { |
| | 14305 | 1038 | | Span<ulong> multiplier = stackalloc ulong[1]; |
| | 14305 | 1039 | | multiplier.Clear(); |
| | 14305 | 1040 | | multiplier[0] = numeratorMultiplier; |
| | 14305 | 1041 | | Span<ulong> scaledNumerator = |
| | 14305 | 1042 | | stackalloc ulong[(MaxCoulombComparisonWords / 2) + 1]; |
| | 14305 | 1043 | | Span<ulong> left = stackalloc ulong[MaxCoulombComparisonWords]; |
| | 14305 | 1044 | | Span<ulong> denominatorTimesCandidate = |
| | 14305 | 1045 | | stackalloc ulong[(MaxCoulombComparisonWords / 2) + 1]; |
| | 14305 | 1046 | | Span<ulong> rightBase = |
| | 14305 | 1047 | | stackalloc ulong[MaxCoulombComparisonWords]; |
| | 14305 | 1048 | | Span<ulong> right = stackalloc ulong[MaxCoulombComparisonWords]; |
| | 14305 | 1049 | | WideArithmetic.MultiplyMagnitudes( |
| | 14305 | 1050 | | numerator, |
| | 14305 | 1051 | | multiplier, |
| | 14305 | 1052 | | scaledNumerator); |
| | 14305 | 1053 | | WideArithmetic.MultiplyMagnitudes( |
| | 14305 | 1054 | | scaledNumerator, |
| | 14305 | 1055 | | scaledNumerator, |
| | 14305 | 1056 | | left); |
| | 14305 | 1057 | | WideArithmetic.MultiplyMagnitudes( |
| | 14305 | 1058 | | denominator, |
| | 14305 | 1059 | | candidate, |
| | 14305 | 1060 | | denominatorTimesCandidate); |
| | 14305 | 1061 | | WideArithmetic.MultiplyMagnitudes( |
| | 14305 | 1062 | | denominatorTimesCandidate, |
| | 14305 | 1063 | | denominatorTimesCandidate, |
| | 14305 | 1064 | | rightBase); |
| | 14305 | 1065 | | WideArithmetic.MultiplyMagnitudes(rightBase, radicand, right); |
| | 14305 | 1066 | | return WideArithmetic.CompareMagnitudeEqualLength(left, right); |
| | | 1067 | | } |
| | | 1068 | | |
| | | 1069 | | private static bool HaveMatchingParticipants( |
| | | 1070 | | in ExactContactResponseOperand3D first, |
| | | 1071 | | in ExactContactResponseOperand3D second) => |
| | 1236 | 1072 | | first.Lever.XNumerator.Equals(second.Lever.XNumerator) |
| | 1236 | 1073 | | & first.Lever.YNumerator.Equals(second.Lever.YNumerator) |
| | 1236 | 1074 | | & first.Lever.ZNumerator.Equals(second.Lever.ZNumerator) |
| | 1236 | 1075 | | & first.Lever.Denominator.Equals(second.Lever.Denominator) |
| | 1236 | 1076 | | & first.InverseMass == second.InverseMass |
| | 1236 | 1077 | | & first.InverseInertia == second.InverseInertia; |
| | | 1078 | | } |