| | | 1 | | //======================================================================= |
| | | 2 | | // ContactNormalImpulse2D.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.Runtime.CompilerServices; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Calculates allocation-free 2D contact-point normal response without mutating either body. |
| | | 19 | | /// </summary> |
| | | 20 | | internal static class ContactNormalImpulse2D |
| | | 21 | | { |
| | | 22 | | internal static bool TryCalculateVelocityDeltas( |
| | | 23 | | SolidBody2D? bodyA, |
| | | 24 | | Vector2d linearVelocityA, |
| | | 25 | | Fixed64 angularVelocityA, |
| | | 26 | | Vector2d relativeContactPointA, |
| | | 27 | | SolidBody2D? bodyB, |
| | | 28 | | Vector2d linearVelocityB, |
| | | 29 | | Fixed64 angularVelocityB, |
| | | 30 | | Vector2d relativeContactPointB, |
| | | 31 | | Vector2d normal, |
| | | 32 | | Fixed64 restitution, |
| | | 33 | | Fixed64 restitutionVelocityThreshold, |
| | | 34 | | out ContactNormalVelocityDeltaResult2D result) |
| | | 35 | | { |
| | 48 | 36 | | result = default; |
| | 48 | 37 | | if (!TryComputeNormalVelocity( |
| | 48 | 38 | | linearVelocityA, |
| | 48 | 39 | | angularVelocityA, |
| | 48 | 40 | | relativeContactPointA, |
| | 48 | 41 | | linearVelocityB, |
| | 48 | 42 | | angularVelocityB, |
| | 48 | 43 | | relativeContactPointB, |
| | 48 | 44 | | normal, |
| | 48 | 45 | | out Fixed64 normalVelocity)) |
| | | 46 | | { |
| | 1 | 47 | | return false; |
| | | 48 | | } |
| | 47 | 49 | | if (normalVelocity >= Fixed64.Zero) |
| | | 50 | | { |
| | 2 | 51 | | result = ZeroVelocityDelta(normalVelocity); |
| | 2 | 52 | | return true; |
| | | 53 | | } |
| | | 54 | | |
| | 45 | 55 | | if (!TryComputeDenominator( |
| | 45 | 56 | | bodyA, |
| | 45 | 57 | | relativeContactPointA, |
| | 45 | 58 | | bodyB, |
| | 45 | 59 | | relativeContactPointB, |
| | 45 | 60 | | normal, |
| | 45 | 61 | | out Fixed64 denominator) |
| | 45 | 62 | | || denominator <= Fixed64.Zero) |
| | | 63 | | { |
| | 13 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 32 | 67 | | Fixed64 appliedRestitution = normalVelocity < -restitutionVelocityThreshold |
| | 32 | 68 | | ? restitution |
| | 32 | 69 | | : Fixed64.Zero; |
| | 32 | 70 | | Fixed64 responseFactor = -(Fixed64.One + appliedRestitution); |
| | 32 | 71 | | bool linearAResolved = ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 32 | 72 | | bodyA?.ProjectLinearMotion(-normal) ?? Vector2d.Zero, |
| | 32 | 73 | | normalVelocity, |
| | 32 | 74 | | responseFactor, |
| | 32 | 75 | | bodyA?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 32 | 76 | | denominator, |
| | 32 | 77 | | out Vector2d linearVelocityDeltaA); |
| | 32 | 78 | | bool angularAResolved = TryResolveAngularVelocityDelta( |
| | 32 | 79 | | bodyA, |
| | 32 | 80 | | relativeContactPointA, |
| | 32 | 81 | | -normal, |
| | 32 | 82 | | normalVelocity, |
| | 32 | 83 | | responseFactor, |
| | 32 | 84 | | denominator, |
| | 32 | 85 | | out Fixed64 angularVelocityDeltaA); |
| | 32 | 86 | | bool linearBResolved = ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 32 | 87 | | bodyB?.ProjectLinearMotion(normal) ?? Vector2d.Zero, |
| | 32 | 88 | | normalVelocity, |
| | 32 | 89 | | responseFactor, |
| | 32 | 90 | | bodyB?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 32 | 91 | | denominator, |
| | 32 | 92 | | out Vector2d linearVelocityDeltaB); |
| | 32 | 93 | | bool angularBResolved = TryResolveAngularVelocityDelta( |
| | 32 | 94 | | bodyB, |
| | 32 | 95 | | relativeContactPointB, |
| | 32 | 96 | | normal, |
| | 32 | 97 | | normalVelocity, |
| | 32 | 98 | | responseFactor, |
| | 32 | 99 | | denominator, |
| | 32 | 100 | | out Fixed64 angularVelocityDeltaB); |
| | 32 | 101 | | if (!(linearAResolved |
| | 32 | 102 | | & angularAResolved |
| | 32 | 103 | | & linearBResolved |
| | 32 | 104 | | & angularBResolved)) |
| | | 105 | | { |
| | 1 | 106 | | return false; |
| | | 107 | | } |
| | | 108 | | |
| | 31 | 109 | | result = new ContactNormalVelocityDeltaResult2D( |
| | 31 | 110 | | normalVelocity, |
| | 31 | 111 | | linearVelocityDeltaA, |
| | 31 | 112 | | angularVelocityDeltaA, |
| | 31 | 113 | | linearVelocityDeltaB, |
| | 31 | 114 | | angularVelocityDeltaB); |
| | 31 | 115 | | return true; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | internal static bool TryCalculateAccumulatedDelta( |
| | | 119 | | SolidBody2D? bodyA, |
| | | 120 | | Vector2d linearVelocityA, |
| | | 121 | | Fixed64 angularVelocityA, |
| | | 122 | | Vector2d relativeContactPointA, |
| | | 123 | | SolidBody2D? bodyB, |
| | | 124 | | Vector2d linearVelocityB, |
| | | 125 | | Fixed64 angularVelocityB, |
| | | 126 | | Vector2d relativeContactPointB, |
| | | 127 | | Vector2d normal, |
| | | 128 | | Fixed64 restitution, |
| | | 129 | | Fixed64 restitutionVelocityThreshold, |
| | | 130 | | Fixed64 accumulatedImpulse, |
| | | 131 | | Fixed64 positiveImpulseScale, |
| | | 132 | | Fixed64 negativeImpulseScale, |
| | | 133 | | out ContactNormalImpulseResult2D result) |
| | | 134 | | { |
| | 16090 | 135 | | result = default; |
| | 16090 | 136 | | bool inputsValid = |
| | 16090 | 137 | | accumulatedImpulse >= Fixed64.Zero |
| | 16090 | 138 | | & positiveImpulseScale >= Fixed64.Zero |
| | 16090 | 139 | | & negativeImpulseScale >= Fixed64.Zero; |
| | 16090 | 140 | | if (!inputsValid) |
| | 3 | 141 | | return false; |
| | 16087 | 142 | | if (!TryComputeNormalVelocity( |
| | 16087 | 143 | | linearVelocityA, |
| | 16087 | 144 | | angularVelocityA, |
| | 16087 | 145 | | relativeContactPointA, |
| | 16087 | 146 | | linearVelocityB, |
| | 16087 | 147 | | angularVelocityB, |
| | 16087 | 148 | | relativeContactPointB, |
| | 16087 | 149 | | normal, |
| | 16087 | 150 | | out Fixed64 normalVelocity) |
| | 16087 | 151 | | || !TryComputeDenominator( |
| | 16087 | 152 | | bodyA, |
| | 16087 | 153 | | relativeContactPointA, |
| | 16087 | 154 | | bodyB, |
| | 16087 | 155 | | relativeContactPointB, |
| | 16087 | 156 | | normal, |
| | 16087 | 157 | | out Fixed64 denominator)) |
| | | 158 | | { |
| | 41 | 159 | | return false; |
| | | 160 | | } |
| | 16046 | 161 | | if (denominator <= Fixed64.Zero) |
| | | 162 | | { |
| | 79 | 163 | | result = Zero(normalVelocity); |
| | 79 | 164 | | return true; |
| | | 165 | | } |
| | | 166 | | |
| | 15967 | 167 | | Fixed64 appliedRestitution = normalVelocity < -restitutionVelocityThreshold |
| | 15967 | 168 | | ? restitution |
| | 15967 | 169 | | : Fixed64.Zero; |
| | 15967 | 170 | | Fixed64 responseFactor = -(Fixed64.One + appliedRestitution); |
| | 15967 | 171 | | Fixed64 impulseScale = normalVelocity < Fixed64.Zero |
| | 15967 | 172 | | ? positiveImpulseScale |
| | 15967 | 173 | | : negativeImpulseScale; |
| | | 174 | | Fixed64 impulseScalar; |
| | 15967 | 175 | | if (!Fixed64.TryMultiplyDivide( |
| | 15967 | 176 | | normalVelocity, |
| | 15967 | 177 | | responseFactor, |
| | 15967 | 178 | | impulseScale, |
| | 15967 | 179 | | denominator, |
| | 15967 | 180 | | out Fixed64 scaledImpulse)) |
| | | 181 | | { |
| | 6 | 182 | | if (normalVelocity < Fixed64.Zero) |
| | 4 | 183 | | return false; |
| | 2 | 184 | | impulseScalar = -accumulatedImpulse; |
| | | 185 | | } |
| | 15961 | 186 | | else if (!Fixed64.TryAdd( |
| | 15961 | 187 | | accumulatedImpulse, |
| | 15961 | 188 | | scaledImpulse, |
| | 15961 | 189 | | out Fixed64 accumulated) |
| | 15961 | 190 | | || !Fixed64.TrySubtract( |
| | 15961 | 191 | | FixedMath.Max(Fixed64.Zero, accumulated), |
| | 15961 | 192 | | accumulatedImpulse, |
| | 15961 | 193 | | out impulseScalar)) |
| | | 194 | | { |
| | 1 | 195 | | return false; |
| | | 196 | | } |
| | 15962 | 197 | | if (impulseScalar == Fixed64.Zero) |
| | | 198 | | { |
| | 15629 | 199 | | result = Zero(normalVelocity); |
| | 15629 | 200 | | return true; |
| | | 201 | | } |
| | | 202 | | |
| | 333 | 203 | | bool linearAResolved = TryComputeLinearVelocityDelta( |
| | 333 | 204 | | bodyA, |
| | 333 | 205 | | -normal, |
| | 333 | 206 | | impulseScalar, |
| | 333 | 207 | | out Vector2d linearA); |
| | 333 | 208 | | bool angularAResolved = TryComputeAngularVelocityDelta( |
| | 333 | 209 | | bodyA, |
| | 333 | 210 | | relativeContactPointA, |
| | 333 | 211 | | -normal, |
| | 333 | 212 | | impulseScalar, |
| | 333 | 213 | | out Fixed64 angularA); |
| | 333 | 214 | | bool linearBResolved = TryComputeLinearVelocityDelta( |
| | 333 | 215 | | bodyB, |
| | 333 | 216 | | normal, |
| | 333 | 217 | | impulseScalar, |
| | 333 | 218 | | out Vector2d linearB); |
| | 333 | 219 | | bool angularBResolved = TryComputeAngularVelocityDelta( |
| | 333 | 220 | | bodyB, |
| | 333 | 221 | | relativeContactPointB, |
| | 333 | 222 | | normal, |
| | 333 | 223 | | impulseScalar, |
| | 333 | 224 | | out Fixed64 angularB); |
| | 333 | 225 | | if (!(linearAResolved |
| | 333 | 226 | | & angularAResolved |
| | 333 | 227 | | & linearBResolved |
| | 333 | 228 | | & angularBResolved)) |
| | | 229 | | { |
| | 1 | 230 | | return false; |
| | | 231 | | } |
| | 332 | 232 | | result = new ContactNormalImpulseResult2D( |
| | 332 | 233 | | normalVelocity, |
| | 332 | 234 | | impulseScalar, |
| | 332 | 235 | | linearA, |
| | 332 | 236 | | angularA, |
| | 332 | 237 | | linearB, |
| | 332 | 238 | | angularB); |
| | 332 | 239 | | return true; |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | internal static bool TryCalculateVelocityDeltasExact( |
| | | 243 | | SolidBody2D? bodyA, |
| | | 244 | | Vector2d linearVelocityA, |
| | | 245 | | Fixed64 angularVelocityA, |
| | | 246 | | in ExactLever3D relativeContactPointA, |
| | | 247 | | SolidBody2D? bodyB, |
| | | 248 | | Vector2d linearVelocityB, |
| | | 249 | | Fixed64 angularVelocityB, |
| | | 250 | | in ExactLever3D relativeContactPointB, |
| | | 251 | | Vector2d normal, |
| | | 252 | | Fixed64 restitution, |
| | | 253 | | Fixed64 restitutionVelocityThreshold, |
| | | 254 | | out ContactNormalVelocityDeltaResult2D result) |
| | | 255 | | { |
| | 34 | 256 | | result = default; |
| | 34 | 257 | | if (!ExactContactLever2D.TryGetNormalResponse( |
| | 34 | 258 | | bodyA, |
| | 34 | 259 | | linearVelocityA, |
| | 34 | 260 | | angularVelocityA, |
| | 34 | 261 | | relativeContactPointA, |
| | 34 | 262 | | bodyB, |
| | 34 | 263 | | linearVelocityB, |
| | 34 | 264 | | angularVelocityB, |
| | 34 | 265 | | relativeContactPointB, |
| | 34 | 266 | | normal, |
| | 34 | 267 | | restitution, |
| | 34 | 268 | | restitutionVelocityThreshold, |
| | 34 | 269 | | out ExactNormalResponse3D response)) |
| | | 270 | | { |
| | 10 | 271 | | return false; |
| | | 272 | | } |
| | | 273 | | |
| | 24 | 274 | | bool hasNormalVelocity = |
| | 24 | 275 | | response.TryGetNormalVelocity(out Fixed64 normalVelocity); |
| | 24 | 276 | | result = new ContactNormalVelocityDeltaResult2D( |
| | 24 | 277 | | normalVelocity, |
| | 24 | 278 | | ExactContactLever2D.ToPlanar(response.FirstLinearVelocityDelta), |
| | 24 | 279 | | ExactContactLever2D.ToPlanarAngular(response.FirstAngularVelocityDelta), |
| | 24 | 280 | | ExactContactLever2D.ToPlanar(response.SecondLinearVelocityDelta), |
| | 24 | 281 | | ExactContactLever2D.ToPlanarAngular(response.SecondAngularVelocityDelta), |
| | 24 | 282 | | response.IsClosing, |
| | 24 | 283 | | hasNormalVelocity); |
| | 24 | 284 | | return true; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | internal static bool TryCalculateAccumulatedDeltaExact( |
| | | 288 | | SolidBody2D? bodyA, |
| | | 289 | | Vector2d linearVelocityA, |
| | | 290 | | Fixed64 angularVelocityA, |
| | | 291 | | in ExactLever3D relativeContactPointA, |
| | | 292 | | SolidBody2D? bodyB, |
| | | 293 | | Vector2d linearVelocityB, |
| | | 294 | | Fixed64 angularVelocityB, |
| | | 295 | | in ExactLever3D relativeContactPointB, |
| | | 296 | | Vector2d normal, |
| | | 297 | | Fixed64 restitution, |
| | | 298 | | Fixed64 restitutionVelocityThreshold, |
| | | 299 | | Fixed64 accumulatedImpulse, |
| | | 300 | | Fixed64 positiveImpulseScale, |
| | | 301 | | Fixed64 negativeImpulseScale, |
| | | 302 | | out ContactNormalImpulseResult2D result, |
| | | 303 | | in ContactResponseSnapshot impact = default) |
| | | 304 | | { |
| | 370 | 305 | | result = default; |
| | 370 | 306 | | if (!ExactContactLever2D.TryGetAccumulatedNormalResponse( |
| | 370 | 307 | | bodyA, |
| | 370 | 308 | | linearVelocityA, |
| | 370 | 309 | | angularVelocityA, |
| | 370 | 310 | | relativeContactPointA, |
| | 370 | 311 | | bodyB, |
| | 370 | 312 | | linearVelocityB, |
| | 370 | 313 | | angularVelocityB, |
| | 370 | 314 | | relativeContactPointB, |
| | 370 | 315 | | normal, |
| | 370 | 316 | | restitution, |
| | 370 | 317 | | restitutionVelocityThreshold, |
| | 370 | 318 | | accumulatedImpulse, |
| | 370 | 319 | | positiveImpulseScale, |
| | 370 | 320 | | negativeImpulseScale, |
| | 370 | 321 | | out ExactNormalResponse3D response, impact)) |
| | | 322 | | { |
| | 3 | 323 | | return false; |
| | | 324 | | } |
| | | 325 | | |
| | 367 | 326 | | bool hasNormalVelocity = |
| | 367 | 327 | | response.TryGetNormalVelocity(out Fixed64 normalVelocity); |
| | 367 | 328 | | bool hasAppliedImpulse = |
| | 367 | 329 | | response.TryGetAppliedImpulse(out Fixed64 appliedImpulse); |
| | 367 | 330 | | bool hasAccumulatedImpulse = |
| | 367 | 331 | | response.TryGetAccumulatedImpulse( |
| | 367 | 332 | | out Fixed64 newAccumulatedImpulse); |
| | 367 | 333 | | Fixed64 impulseScalar = hasAccumulatedImpulse |
| | 367 | 334 | | ? newAccumulatedImpulse - accumulatedImpulse |
| | 367 | 335 | | : -accumulatedImpulse; |
| | 367 | 336 | | result = new ContactNormalImpulseResult2D( |
| | 367 | 337 | | normalVelocity, |
| | 367 | 338 | | impulseScalar, |
| | 367 | 339 | | appliedImpulse, |
| | 367 | 340 | | ExactContactLever2D.ToPlanar(response.FirstLinearVelocityDelta), |
| | 367 | 341 | | ExactContactLever2D.ToPlanarAngular(response.FirstAngularVelocityDelta), |
| | 367 | 342 | | ExactContactLever2D.ToPlanar(response.SecondLinearVelocityDelta), |
| | 367 | 343 | | ExactContactLever2D.ToPlanarAngular(response.SecondAngularVelocityDelta), |
| | 367 | 344 | | hasNormalVelocity, |
| | 367 | 345 | | hasAppliedImpulse, |
| | 367 | 346 | | hasAccumulatedImpulse); |
| | 367 | 347 | | return true; |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 351 | | private static bool TryComputeNormalVelocity( |
| | | 352 | | Vector2d linearVelocityA, |
| | | 353 | | Fixed64 angularVelocityA, |
| | | 354 | | Vector2d relativeContactPointA, |
| | | 355 | | Vector2d linearVelocityB, |
| | | 356 | | Fixed64 angularVelocityB, |
| | | 357 | | Vector2d relativeContactPointB, |
| | | 358 | | Vector2d normal, |
| | | 359 | | out Fixed64 normalVelocity) |
| | | 360 | | { |
| | 16135 | 361 | | bool angularAResolved = ContactResponseArithmetic3D.TryCross( |
| | 16135 | 362 | | new Vector3d( |
| | 16135 | 363 | | Fixed64.Zero, |
| | 16135 | 364 | | -angularVelocityA, |
| | 16135 | 365 | | Fixed64.Zero), |
| | 16135 | 366 | | ExactContactLever2D.ToSpatial(relativeContactPointA), |
| | 16135 | 367 | | out Vector3d angularA); |
| | 16135 | 368 | | bool angularBResolved = ContactResponseArithmetic3D.TryCross( |
| | 16135 | 369 | | new Vector3d( |
| | 16135 | 370 | | Fixed64.Zero, |
| | 16135 | 371 | | -angularVelocityB, |
| | 16135 | 372 | | Fixed64.Zero), |
| | 16135 | 373 | | ExactContactLever2D.ToSpatial(relativeContactPointB), |
| | 16135 | 374 | | out Vector3d angularB); |
| | 16135 | 375 | | bool relativeResolved = Vector3d.TrySubtractSums( |
| | 16135 | 376 | | ExactContactLever2D.ToSpatial(linearVelocityB), |
| | 16135 | 377 | | angularB, |
| | 16135 | 378 | | ExactContactLever2D.ToSpatial(linearVelocityA), |
| | 16135 | 379 | | angularA, |
| | 16135 | 380 | | out Vector3d relative); |
| | 16135 | 381 | | bool projectionResolved = ContactResponseArithmetic3D.TryDot( |
| | 16135 | 382 | | relative, |
| | 16135 | 383 | | ExactContactLever2D.ToSpatial(normal), |
| | 16135 | 384 | | out normalVelocity); |
| | 16135 | 385 | | if (!(angularAResolved |
| | 16135 | 386 | | & angularBResolved |
| | 16135 | 387 | | & relativeResolved |
| | 16135 | 388 | | & projectionResolved)) |
| | | 389 | | { |
| | 4 | 390 | | normalVelocity = default; |
| | 4 | 391 | | return false; |
| | | 392 | | } |
| | 16131 | 393 | | return true; |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 397 | | internal static bool TryComputeDenominator( |
| | | 398 | | SolidBody2D? bodyA, |
| | | 399 | | Vector2d relativeContactPointA, |
| | | 400 | | SolidBody2D? bodyB, |
| | | 401 | | Vector2d relativeContactPointB, |
| | | 402 | | Vector2d normal, |
| | | 403 | | out Fixed64 denominator) |
| | | 404 | | { |
| | 18429 | 405 | | bool angularAResolved = TryComputeAngularDenominator( |
| | 18429 | 406 | | bodyA, |
| | 18429 | 407 | | relativeContactPointA, |
| | 18429 | 408 | | normal, |
| | 18429 | 409 | | out Fixed64 angularA); |
| | 18429 | 410 | | bool angularBResolved = TryComputeAngularDenominator( |
| | 18429 | 411 | | bodyB, |
| | 18429 | 412 | | relativeContactPointB, |
| | 18429 | 413 | | normal, |
| | 18429 | 414 | | out Fixed64 angularB); |
| | 18429 | 415 | | bool linearAResolved = TryGetConstrainedInverseMass( |
| | 18429 | 416 | | bodyA, |
| | 18429 | 417 | | normal, |
| | 18429 | 418 | | out Fixed64 linearA); |
| | 18429 | 419 | | bool linearBResolved = TryGetConstrainedInverseMass( |
| | 18429 | 420 | | bodyB, |
| | 18429 | 421 | | normal, |
| | 18429 | 422 | | out Fixed64 linearB); |
| | 18429 | 423 | | bool sumResolved = Fixed64.TryAdd( |
| | 18429 | 424 | | linearA, |
| | 18429 | 425 | | linearB, |
| | 18429 | 426 | | out Fixed64 linear) |
| | 18429 | 427 | | & Fixed64.TryAdd( |
| | 18429 | 428 | | linear, |
| | 18429 | 429 | | angularA, |
| | 18429 | 430 | | out Fixed64 first) |
| | 18429 | 431 | | & Fixed64.TryAdd( |
| | 18429 | 432 | | first, |
| | 18429 | 433 | | angularB, |
| | 18429 | 434 | | out denominator); |
| | 18429 | 435 | | if (!(linearAResolved |
| | 18429 | 436 | | & linearBResolved |
| | 18429 | 437 | | & angularAResolved |
| | 18429 | 438 | | & angularBResolved |
| | 18429 | 439 | | & sumResolved)) |
| | | 440 | | { |
| | 42 | 441 | | denominator = default; |
| | 42 | 442 | | return false; |
| | | 443 | | } |
| | 18387 | 444 | | return true; |
| | | 445 | | } |
| | | 446 | | |
| | | 447 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 448 | | internal static bool TryComputeAngularDenominator( |
| | | 449 | | SolidBody2D? body, |
| | | 450 | | Vector2d relativeContactPoint, |
| | | 451 | | Vector2d axis, |
| | | 452 | | out Fixed64 denominator) |
| | | 453 | | { |
| | 221209 | 454 | | if (body?.CanRotate != true) |
| | | 455 | | { |
| | 201210 | 456 | | denominator = Fixed64.Zero; |
| | 201210 | 457 | | return true; |
| | | 458 | | } |
| | | 459 | | |
| | 19999 | 460 | | denominator = default; |
| | 19999 | 461 | | bool crossResolved = ContactResponseArithmetic3D.TryCross( |
| | 19999 | 462 | | ExactContactLever2D.ToSpatial(relativeContactPoint), |
| | 19999 | 463 | | ExactContactLever2D.ToSpatial(axis), |
| | 19999 | 464 | | out Vector3d cross); |
| | 19999 | 465 | | bool denominatorResolved = crossResolved |
| | 19999 | 466 | | && Fixed64.TryMultiplyDivide( |
| | 19999 | 467 | | cross.Y, |
| | 19999 | 468 | | cross.Y, |
| | 19999 | 469 | | body.EffectiveInverseMomentOfInertia, |
| | 19999 | 470 | | Fixed64.One, |
| | 19999 | 471 | | out denominator); |
| | 19999 | 472 | | if (!denominatorResolved |
| | 19999 | 473 | | || (denominator == Fixed64.Zero |
| | 19999 | 474 | | && cross.Y != Fixed64.Zero |
| | 19999 | 475 | | && body.EffectiveInverseMomentOfInertia |
| | 19999 | 476 | | != Fixed64.Zero)) |
| | | 477 | | { |
| | 50 | 478 | | denominator = default; |
| | 50 | 479 | | return false; |
| | | 480 | | } |
| | | 481 | | |
| | 19949 | 482 | | return true; |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 486 | | internal static bool TryGetConstrainedInverseMass( |
| | | 487 | | SolidBody2D? body, |
| | | 488 | | Vector2d axis, |
| | | 489 | | out Fixed64 inverseMass) |
| | | 490 | | { |
| | 36880 | 491 | | if (body == null) |
| | | 492 | | { |
| | 5175 | 493 | | inverseMass = Fixed64.Zero; |
| | 5175 | 494 | | return true; |
| | | 495 | | } |
| | | 496 | | |
| | 31705 | 497 | | inverseMass = body.GetConstrainedInverseMass(axis); |
| | 31705 | 498 | | return inverseMass != Fixed64.Zero |
| | 31705 | 499 | | || body.EffectiveInverseMass == Fixed64.Zero |
| | 31705 | 500 | | || body.ProjectLinearMotion(axis) == Vector2d.Zero; |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 504 | | internal static bool TryComputeLinearVelocityDelta( |
| | | 505 | | SolidBody2D? body, |
| | | 506 | | Vector2d signedNormal, |
| | | 507 | | Fixed64 impulseScalar, |
| | | 508 | | out Vector2d velocityDelta) => |
| | 18587 | 509 | | ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 18587 | 510 | | body?.ProjectLinearMotion(signedNormal) ?? Vector2d.Zero, |
| | 18587 | 511 | | impulseScalar, |
| | 18587 | 512 | | body?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 18587 | 513 | | Fixed64.One, |
| | 18587 | 514 | | out velocityDelta); |
| | | 515 | | |
| | | 516 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 517 | | internal static bool TryComputeAngularVelocityDelta( |
| | | 518 | | SolidBody2D? body, |
| | | 519 | | Vector2d relativeContactPoint, |
| | | 520 | | Vector2d signedNormal, |
| | | 521 | | Fixed64 impulseScalar, |
| | | 522 | | out Fixed64 velocityDelta) |
| | | 523 | | { |
| | 18590 | 524 | | velocityDelta = Fixed64.Zero; |
| | 18590 | 525 | | if (body?.CanRotate != true) |
| | 17816 | 526 | | return true; |
| | | 527 | | |
| | 774 | 528 | | Vector3d spatialPoint = |
| | 774 | 529 | | ExactContactLever2D.ToSpatial(relativeContactPoint); |
| | 774 | 530 | | Vector3d spatialNormal = |
| | 774 | 531 | | ExactContactLever2D.ToSpatial(signedNormal); |
| | 774 | 532 | | return ContactResponseArithmetic3D.TryCross( |
| | 774 | 533 | | spatialPoint, |
| | 774 | 534 | | spatialNormal, |
| | 774 | 535 | | out Vector3d cross) |
| | 774 | 536 | | && Fixed64.TryMultiplyDivide( |
| | 774 | 537 | | -cross.Y, |
| | 774 | 538 | | impulseScalar, |
| | 774 | 539 | | body.EffectiveInverseMomentOfInertia, |
| | 774 | 540 | | Fixed64.One, |
| | 774 | 541 | | out velocityDelta); |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 545 | | private static bool TryResolveAngularVelocityDelta( |
| | | 546 | | SolidBody2D? body, |
| | | 547 | | Vector2d relativeContactPoint, |
| | | 548 | | Vector2d signedNormal, |
| | | 549 | | Fixed64 normalVelocity, |
| | | 550 | | Fixed64 responseFactor, |
| | | 551 | | Fixed64 denominator, |
| | | 552 | | out Fixed64 velocityDelta) |
| | | 553 | | { |
| | 64 | 554 | | velocityDelta = Fixed64.Zero; |
| | 64 | 555 | | if (body?.CanRotate != true) |
| | 21 | 556 | | return true; |
| | | 557 | | |
| | 43 | 558 | | Fixed64 torqueScale = Vector2d.CrossProduct(relativeContactPoint, signedNormal); |
| | 43 | 559 | | if (torqueScale == Fixed64.Zero) |
| | 30 | 560 | | return true; |
| | | 561 | | |
| | 13 | 562 | | bool angularScaleResolved = Fixed64.TryMultiplyDivide( |
| | 13 | 563 | | normalVelocity, |
| | 13 | 564 | | responseFactor, |
| | 13 | 565 | | body.EffectiveInverseMomentOfInertia, |
| | 13 | 566 | | denominator, |
| | 13 | 567 | | out Fixed64 angularScale); |
| | 13 | 568 | | bool velocityDeltaResolved = Fixed64.TryMultiplyDivide( |
| | 13 | 569 | | torqueScale, |
| | 13 | 570 | | angularScale, |
| | 13 | 571 | | Fixed64.One, |
| | 13 | 572 | | out velocityDelta); |
| | 13 | 573 | | return angularScaleResolved |
| | 13 | 574 | | & (angularScale != Fixed64.Zero | torqueScale.Abs() <= Fixed64.One) |
| | 13 | 575 | | & velocityDeltaResolved; |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 579 | | private static ContactNormalImpulseResult2D Zero(Fixed64 normalVelocity) => |
| | 15708 | 580 | | new( |
| | 15708 | 581 | | normalVelocity, |
| | 15708 | 582 | | Fixed64.Zero, |
| | 15708 | 583 | | Vector2d.Zero, |
| | 15708 | 584 | | Fixed64.Zero, |
| | 15708 | 585 | | Vector2d.Zero, |
| | 15708 | 586 | | Fixed64.Zero); |
| | | 587 | | |
| | | 588 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 589 | | private static ContactNormalVelocityDeltaResult2D ZeroVelocityDelta(Fixed64 normalVelocity) => |
| | 2 | 590 | | new( |
| | 2 | 591 | | normalVelocity, |
| | 2 | 592 | | Vector2d.Zero, |
| | 2 | 593 | | Fixed64.Zero, |
| | 2 | 594 | | Vector2d.Zero, |
| | 2 | 595 | | Fixed64.Zero); |
| | | 596 | | } |