| | | 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 FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Side-effect-free 2D contact-normal impulse result for two participants. |
| | | 16 | | /// </summary> |
| | | 17 | | internal readonly struct ContactNormalImpulseResult2D |
| | | 18 | | { |
| | | 19 | | public ContactNormalImpulseResult2D( |
| | | 20 | | Fixed64 normalVelocity, |
| | | 21 | | Fixed64 impulseScalar, |
| | | 22 | | Vector2d linearVelocityDeltaA, |
| | | 23 | | Fixed64 angularVelocityDeltaA, |
| | | 24 | | Vector2d linearVelocityDeltaB, |
| | | 25 | | Fixed64 angularVelocityDeltaB) |
| | | 26 | | : this( |
| | | 27 | | normalVelocity, |
| | | 28 | | impulseScalar, |
| | | 29 | | impulseScalar, |
| | | 30 | | linearVelocityDeltaA, |
| | | 31 | | angularVelocityDeltaA, |
| | | 32 | | linearVelocityDeltaB, |
| | | 33 | | angularVelocityDeltaB) |
| | | 34 | | { |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public ContactNormalImpulseResult2D( |
| | | 38 | | Fixed64 normalVelocity, |
| | | 39 | | Fixed64 impulseScalar, |
| | | 40 | | Fixed64 appliedImpulseScalar, |
| | | 41 | | Vector2d linearVelocityDeltaA, |
| | | 42 | | Fixed64 angularVelocityDeltaA, |
| | | 43 | | Vector2d linearVelocityDeltaB, |
| | | 44 | | Fixed64 angularVelocityDeltaB, |
| | | 45 | | bool hasRepresentableNormalVelocity = true, |
| | | 46 | | bool hasRepresentableAppliedImpulse = true, |
| | | 47 | | bool hasRepresentableAccumulatedImpulse = true) |
| | | 48 | | { |
| | | 49 | | NormalVelocity = normalVelocity; |
| | | 50 | | ImpulseScalar = impulseScalar; |
| | | 51 | | AppliedImpulseScalar = appliedImpulseScalar; |
| | | 52 | | LinearVelocityDeltaA = linearVelocityDeltaA; |
| | | 53 | | AngularVelocityDeltaA = angularVelocityDeltaA; |
| | | 54 | | LinearVelocityDeltaB = linearVelocityDeltaB; |
| | | 55 | | AngularVelocityDeltaB = angularVelocityDeltaB; |
| | | 56 | | HasRepresentableNormalVelocity = hasRepresentableNormalVelocity; |
| | | 57 | | HasRepresentableAppliedImpulse = hasRepresentableAppliedImpulse; |
| | | 58 | | HasRepresentableAccumulatedImpulse = |
| | | 59 | | hasRepresentableAccumulatedImpulse; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public Fixed64 NormalVelocity { get; } |
| | | 63 | | |
| | | 64 | | public Fixed64 ImpulseScalar { get; } |
| | | 65 | | |
| | | 66 | | public Fixed64 AppliedImpulseScalar { get; } |
| | | 67 | | |
| | | 68 | | public bool HasRepresentableNormalVelocity { get; } |
| | | 69 | | |
| | | 70 | | public bool HasRepresentableAppliedImpulse { get; } |
| | | 71 | | |
| | | 72 | | public bool HasRepresentableAccumulatedImpulse { get; } |
| | | 73 | | |
| | | 74 | | public Vector2d LinearVelocityDeltaA { get; } |
| | | 75 | | |
| | | 76 | | public Fixed64 AngularVelocityDeltaA { get; } |
| | | 77 | | |
| | | 78 | | public Vector2d LinearVelocityDeltaB { get; } |
| | | 79 | | |
| | | 80 | | public Fixed64 AngularVelocityDeltaB { get; } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Side-effect-free 2D velocity deltas for a normal response whose impulse |
| | | 85 | | /// scalar does not need to be representable. |
| | | 86 | | /// </summary> |
| | | 87 | | internal readonly struct ContactNormalVelocityDeltaResult2D |
| | | 88 | | { |
| | | 89 | | public ContactNormalVelocityDeltaResult2D( |
| | | 90 | | Fixed64 normalVelocity, |
| | | 91 | | Vector2d linearVelocityDeltaA, |
| | | 92 | | Fixed64 angularVelocityDeltaA, |
| | | 93 | | Vector2d linearVelocityDeltaB, |
| | | 94 | | Fixed64 angularVelocityDeltaB) |
| | | 95 | | : this( |
| | | 96 | | normalVelocity, |
| | | 97 | | linearVelocityDeltaA, |
| | | 98 | | angularVelocityDeltaA, |
| | | 99 | | linearVelocityDeltaB, |
| | | 100 | | angularVelocityDeltaB, |
| | | 101 | | normalVelocity < Fixed64.Zero, |
| | | 102 | | hasRepresentableNormalVelocity: true) |
| | | 103 | | { |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public ContactNormalVelocityDeltaResult2D( |
| | | 107 | | Fixed64 normalVelocity, |
| | | 108 | | Vector2d linearVelocityDeltaA, |
| | | 109 | | Fixed64 angularVelocityDeltaA, |
| | | 110 | | Vector2d linearVelocityDeltaB, |
| | | 111 | | Fixed64 angularVelocityDeltaB, |
| | | 112 | | bool isClosing, |
| | | 113 | | bool hasRepresentableNormalVelocity) |
| | | 114 | | { |
| | | 115 | | NormalVelocity = normalVelocity; |
| | | 116 | | LinearVelocityDeltaA = linearVelocityDeltaA; |
| | | 117 | | AngularVelocityDeltaA = angularVelocityDeltaA; |
| | | 118 | | LinearVelocityDeltaB = linearVelocityDeltaB; |
| | | 119 | | AngularVelocityDeltaB = angularVelocityDeltaB; |
| | | 120 | | IsClosing = isClosing; |
| | | 121 | | HasRepresentableNormalVelocity = hasRepresentableNormalVelocity; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public Fixed64 NormalVelocity { get; } |
| | | 125 | | |
| | | 126 | | public bool IsClosing { get; } |
| | | 127 | | |
| | | 128 | | public bool HasRepresentableNormalVelocity { get; } |
| | | 129 | | |
| | | 130 | | public Vector2d LinearVelocityDeltaA { get; } |
| | | 131 | | |
| | | 132 | | public Fixed64 AngularVelocityDeltaA { get; } |
| | | 133 | | |
| | | 134 | | public Vector2d LinearVelocityDeltaB { get; } |
| | | 135 | | |
| | | 136 | | public Fixed64 AngularVelocityDeltaB { get; } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Calculates allocation-free 2D contact-point normal response without mutating either body. |
| | | 141 | | /// </summary> |
| | | 142 | | internal static class ContactNormalImpulse2D |
| | | 143 | | { |
| | | 144 | | internal static bool TryCalculateVelocityDeltas( |
| | | 145 | | SolidBody2D? bodyA, |
| | | 146 | | Vector2d linearVelocityA, |
| | | 147 | | Fixed64 angularVelocityA, |
| | | 148 | | Vector2d relativeContactPointA, |
| | | 149 | | SolidBody2D? bodyB, |
| | | 150 | | Vector2d linearVelocityB, |
| | | 151 | | Fixed64 angularVelocityB, |
| | | 152 | | Vector2d relativeContactPointB, |
| | | 153 | | Vector2d normal, |
| | | 154 | | Fixed64 restitution, |
| | | 155 | | Fixed64 restitutionVelocityThreshold, |
| | | 156 | | out ContactNormalVelocityDeltaResult2D result) |
| | | 157 | | { |
| | 39 | 158 | | result = default; |
| | 39 | 159 | | if (!TryComputeNormalVelocity( |
| | 39 | 160 | | linearVelocityA, |
| | 39 | 161 | | angularVelocityA, |
| | 39 | 162 | | relativeContactPointA, |
| | 39 | 163 | | linearVelocityB, |
| | 39 | 164 | | angularVelocityB, |
| | 39 | 165 | | relativeContactPointB, |
| | 39 | 166 | | normal, |
| | 39 | 167 | | out Fixed64 normalVelocity)) |
| | | 168 | | { |
| | 1 | 169 | | return false; |
| | | 170 | | } |
| | 38 | 171 | | if (normalVelocity >= Fixed64.Zero) |
| | | 172 | | { |
| | 1 | 173 | | result = ZeroVelocityDelta(normalVelocity); |
| | 1 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | |
| | 37 | 177 | | if (!TryComputeDenominator( |
| | 37 | 178 | | bodyA, |
| | 37 | 179 | | relativeContactPointA, |
| | 37 | 180 | | bodyB, |
| | 37 | 181 | | relativeContactPointB, |
| | 37 | 182 | | normal, |
| | 37 | 183 | | out Fixed64 denominator) |
| | 37 | 184 | | || denominator <= Fixed64.Zero) |
| | | 185 | | { |
| | 7 | 186 | | return false; |
| | | 187 | | } |
| | | 188 | | |
| | 30 | 189 | | Fixed64 appliedRestitution = normalVelocity < -restitutionVelocityThreshold |
| | 30 | 190 | | ? restitution |
| | 30 | 191 | | : Fixed64.Zero; |
| | 30 | 192 | | Fixed64 responseFactor = -(Fixed64.One + appliedRestitution); |
| | 30 | 193 | | bool linearAResolved = ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 30 | 194 | | bodyA?.ProjectLinearMotion(-normal) ?? Vector2d.Zero, |
| | 30 | 195 | | normalVelocity, |
| | 30 | 196 | | responseFactor, |
| | 30 | 197 | | bodyA?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 30 | 198 | | denominator, |
| | 30 | 199 | | out Vector2d linearVelocityDeltaA); |
| | 30 | 200 | | bool angularAResolved = TryResolveAngularVelocityDelta( |
| | 30 | 201 | | bodyA, |
| | 30 | 202 | | relativeContactPointA, |
| | 30 | 203 | | -normal, |
| | 30 | 204 | | normalVelocity, |
| | 30 | 205 | | responseFactor, |
| | 30 | 206 | | denominator, |
| | 30 | 207 | | out Fixed64 angularVelocityDeltaA); |
| | 30 | 208 | | bool linearBResolved = ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 30 | 209 | | bodyB?.ProjectLinearMotion(normal) ?? Vector2d.Zero, |
| | 30 | 210 | | normalVelocity, |
| | 30 | 211 | | responseFactor, |
| | 30 | 212 | | bodyB?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 30 | 213 | | denominator, |
| | 30 | 214 | | out Vector2d linearVelocityDeltaB); |
| | 30 | 215 | | bool angularBResolved = TryResolveAngularVelocityDelta( |
| | 30 | 216 | | bodyB, |
| | 30 | 217 | | relativeContactPointB, |
| | 30 | 218 | | normal, |
| | 30 | 219 | | normalVelocity, |
| | 30 | 220 | | responseFactor, |
| | 30 | 221 | | denominator, |
| | 30 | 222 | | out Fixed64 angularVelocityDeltaB); |
| | 30 | 223 | | if (!(linearAResolved |
| | 30 | 224 | | & angularAResolved |
| | 30 | 225 | | & linearBResolved |
| | 30 | 226 | | & angularBResolved)) |
| | | 227 | | { |
| | 1 | 228 | | return false; |
| | | 229 | | } |
| | | 230 | | |
| | 29 | 231 | | result = new ContactNormalVelocityDeltaResult2D( |
| | 29 | 232 | | normalVelocity, |
| | 29 | 233 | | linearVelocityDeltaA, |
| | 29 | 234 | | angularVelocityDeltaA, |
| | 29 | 235 | | linearVelocityDeltaB, |
| | 29 | 236 | | angularVelocityDeltaB); |
| | 29 | 237 | | return true; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | internal static bool TryCalculateAccumulatedDelta( |
| | | 241 | | SolidBody2D? bodyA, |
| | | 242 | | Vector2d linearVelocityA, |
| | | 243 | | Fixed64 angularVelocityA, |
| | | 244 | | Vector2d relativeContactPointA, |
| | | 245 | | SolidBody2D? bodyB, |
| | | 246 | | Vector2d linearVelocityB, |
| | | 247 | | Fixed64 angularVelocityB, |
| | | 248 | | Vector2d relativeContactPointB, |
| | | 249 | | Vector2d normal, |
| | | 250 | | Fixed64 restitution, |
| | | 251 | | Fixed64 restitutionVelocityThreshold, |
| | | 252 | | Fixed64 accumulatedImpulse, |
| | | 253 | | Fixed64 positiveImpulseScale, |
| | | 254 | | Fixed64 negativeImpulseScale, |
| | | 255 | | out ContactNormalImpulseResult2D result) |
| | | 256 | | { |
| | 995 | 257 | | result = default; |
| | 995 | 258 | | bool inputsValid = |
| | 995 | 259 | | accumulatedImpulse >= Fixed64.Zero |
| | 995 | 260 | | & positiveImpulseScale >= Fixed64.Zero |
| | 995 | 261 | | & negativeImpulseScale >= Fixed64.Zero; |
| | 995 | 262 | | if (!inputsValid) |
| | 3 | 263 | | return false; |
| | 992 | 264 | | if (!TryComputeNormalVelocity( |
| | 992 | 265 | | linearVelocityA, |
| | 992 | 266 | | angularVelocityA, |
| | 992 | 267 | | relativeContactPointA, |
| | 992 | 268 | | linearVelocityB, |
| | 992 | 269 | | angularVelocityB, |
| | 992 | 270 | | relativeContactPointB, |
| | 992 | 271 | | normal, |
| | 992 | 272 | | out Fixed64 normalVelocity) |
| | 992 | 273 | | || !TryComputeDenominator( |
| | 992 | 274 | | bodyA, |
| | 992 | 275 | | relativeContactPointA, |
| | 992 | 276 | | bodyB, |
| | 992 | 277 | | relativeContactPointB, |
| | 992 | 278 | | normal, |
| | 992 | 279 | | out Fixed64 denominator)) |
| | | 280 | | { |
| | 12 | 281 | | return false; |
| | | 282 | | } |
| | 980 | 283 | | if (denominator <= Fixed64.Zero) |
| | | 284 | | { |
| | 1 | 285 | | result = Zero(normalVelocity); |
| | 1 | 286 | | return true; |
| | | 287 | | } |
| | | 288 | | |
| | 979 | 289 | | Fixed64 appliedRestitution = normalVelocity < -restitutionVelocityThreshold |
| | 979 | 290 | | ? restitution |
| | 979 | 291 | | : Fixed64.Zero; |
| | 979 | 292 | | Fixed64 responseFactor = -(Fixed64.One + appliedRestitution); |
| | 979 | 293 | | Fixed64 impulseScale = normalVelocity < Fixed64.Zero |
| | 979 | 294 | | ? positiveImpulseScale |
| | 979 | 295 | | : negativeImpulseScale; |
| | | 296 | | Fixed64 impulseScalar; |
| | 979 | 297 | | if (!Fixed64.TryMultiplyDivide( |
| | 979 | 298 | | normalVelocity, |
| | 979 | 299 | | responseFactor, |
| | 979 | 300 | | impulseScale, |
| | 979 | 301 | | denominator, |
| | 979 | 302 | | out Fixed64 scaledImpulse)) |
| | | 303 | | { |
| | 6 | 304 | | if (normalVelocity < Fixed64.Zero) |
| | 4 | 305 | | return false; |
| | 2 | 306 | | impulseScalar = -accumulatedImpulse; |
| | | 307 | | } |
| | 973 | 308 | | else if (!Fixed64.TryAdd( |
| | 973 | 309 | | accumulatedImpulse, |
| | 973 | 310 | | scaledImpulse, |
| | 973 | 311 | | out Fixed64 accumulated) |
| | 973 | 312 | | || !Fixed64.TrySubtract( |
| | 973 | 313 | | FixedMath.Max(Fixed64.Zero, accumulated), |
| | 973 | 314 | | accumulatedImpulse, |
| | 973 | 315 | | out impulseScalar)) |
| | | 316 | | { |
| | 1 | 317 | | return false; |
| | | 318 | | } |
| | 974 | 319 | | if (impulseScalar == Fixed64.Zero) |
| | | 320 | | { |
| | 914 | 321 | | result = Zero(normalVelocity); |
| | 914 | 322 | | return true; |
| | | 323 | | } |
| | | 324 | | |
| | 60 | 325 | | bool linearAResolved = TryComputeLinearVelocityDelta( |
| | 60 | 326 | | bodyA, |
| | 60 | 327 | | -normal, |
| | 60 | 328 | | impulseScalar, |
| | 60 | 329 | | out Vector2d linearA); |
| | 60 | 330 | | bool angularAResolved = TryComputeAngularVelocityDelta( |
| | 60 | 331 | | bodyA, |
| | 60 | 332 | | relativeContactPointA, |
| | 60 | 333 | | -normal, |
| | 60 | 334 | | impulseScalar, |
| | 60 | 335 | | out Fixed64 angularA); |
| | 60 | 336 | | bool linearBResolved = TryComputeLinearVelocityDelta( |
| | 60 | 337 | | bodyB, |
| | 60 | 338 | | normal, |
| | 60 | 339 | | impulseScalar, |
| | 60 | 340 | | out Vector2d linearB); |
| | 60 | 341 | | bool angularBResolved = TryComputeAngularVelocityDelta( |
| | 60 | 342 | | bodyB, |
| | 60 | 343 | | relativeContactPointB, |
| | 60 | 344 | | normal, |
| | 60 | 345 | | impulseScalar, |
| | 60 | 346 | | out Fixed64 angularB); |
| | 60 | 347 | | if (!(linearAResolved |
| | 60 | 348 | | & angularAResolved |
| | 60 | 349 | | & linearBResolved |
| | 60 | 350 | | & angularBResolved)) |
| | | 351 | | { |
| | 1 | 352 | | return false; |
| | | 353 | | } |
| | 59 | 354 | | result = new ContactNormalImpulseResult2D( |
| | 59 | 355 | | normalVelocity, |
| | 59 | 356 | | impulseScalar, |
| | 59 | 357 | | linearA, |
| | 59 | 358 | | angularA, |
| | 59 | 359 | | linearB, |
| | 59 | 360 | | angularB); |
| | 59 | 361 | | return true; |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | internal static bool TryCalculateVelocityDeltasExact( |
| | | 365 | | SolidBody2D? bodyA, |
| | | 366 | | Vector2d linearVelocityA, |
| | | 367 | | Fixed64 angularVelocityA, |
| | | 368 | | in ExactLever3D relativeContactPointA, |
| | | 369 | | SolidBody2D? bodyB, |
| | | 370 | | Vector2d linearVelocityB, |
| | | 371 | | Fixed64 angularVelocityB, |
| | | 372 | | in ExactLever3D relativeContactPointB, |
| | | 373 | | Vector2d normal, |
| | | 374 | | Fixed64 restitution, |
| | | 375 | | Fixed64 restitutionVelocityThreshold, |
| | | 376 | | out ContactNormalVelocityDeltaResult2D result) |
| | | 377 | | { |
| | 26 | 378 | | result = default; |
| | 26 | 379 | | if (!ExactContactLever2D.TryGetNormalResponse( |
| | 26 | 380 | | bodyA, |
| | 26 | 381 | | linearVelocityA, |
| | 26 | 382 | | angularVelocityA, |
| | 26 | 383 | | relativeContactPointA, |
| | 26 | 384 | | bodyB, |
| | 26 | 385 | | linearVelocityB, |
| | 26 | 386 | | angularVelocityB, |
| | 26 | 387 | | relativeContactPointB, |
| | 26 | 388 | | normal, |
| | 26 | 389 | | restitution, |
| | 26 | 390 | | restitutionVelocityThreshold, |
| | 26 | 391 | | out ExactNormalResponse3D response)) |
| | | 392 | | { |
| | 4 | 393 | | return false; |
| | | 394 | | } |
| | | 395 | | |
| | 22 | 396 | | bool hasNormalVelocity = |
| | 22 | 397 | | response.TryGetNormalVelocity(out Fixed64 normalVelocity); |
| | 22 | 398 | | result = new ContactNormalVelocityDeltaResult2D( |
| | 22 | 399 | | normalVelocity, |
| | 22 | 400 | | ExactContactLever2D.ToPlanar(response.FirstLinearVelocityDelta), |
| | 22 | 401 | | ExactContactLever2D.ToPlanarAngular(response.FirstAngularVelocityDelta), |
| | 22 | 402 | | ExactContactLever2D.ToPlanar(response.SecondLinearVelocityDelta), |
| | 22 | 403 | | ExactContactLever2D.ToPlanarAngular(response.SecondAngularVelocityDelta), |
| | 22 | 404 | | response.IsClosing, |
| | 22 | 405 | | hasNormalVelocity); |
| | 22 | 406 | | return true; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | internal static bool TryCalculateAccumulatedDeltaExact( |
| | | 410 | | SolidBody2D? bodyA, |
| | | 411 | | Vector2d linearVelocityA, |
| | | 412 | | Fixed64 angularVelocityA, |
| | | 413 | | in ExactLever3D relativeContactPointA, |
| | | 414 | | SolidBody2D? bodyB, |
| | | 415 | | Vector2d linearVelocityB, |
| | | 416 | | Fixed64 angularVelocityB, |
| | | 417 | | in ExactLever3D relativeContactPointB, |
| | | 418 | | Vector2d normal, |
| | | 419 | | Fixed64 restitution, |
| | | 420 | | Fixed64 restitutionVelocityThreshold, |
| | | 421 | | Fixed64 accumulatedImpulse, |
| | | 422 | | Fixed64 positiveImpulseScale, |
| | | 423 | | Fixed64 negativeImpulseScale, |
| | | 424 | | out ContactNormalImpulseResult2D result) |
| | | 425 | | { |
| | 38 | 426 | | result = default; |
| | 38 | 427 | | if (!ExactContactLever2D.TryGetAccumulatedNormalResponse( |
| | 38 | 428 | | bodyA, |
| | 38 | 429 | | linearVelocityA, |
| | 38 | 430 | | angularVelocityA, |
| | 38 | 431 | | relativeContactPointA, |
| | 38 | 432 | | bodyB, |
| | 38 | 433 | | linearVelocityB, |
| | 38 | 434 | | angularVelocityB, |
| | 38 | 435 | | relativeContactPointB, |
| | 38 | 436 | | normal, |
| | 38 | 437 | | restitution, |
| | 38 | 438 | | restitutionVelocityThreshold, |
| | 38 | 439 | | accumulatedImpulse, |
| | 38 | 440 | | positiveImpulseScale, |
| | 38 | 441 | | negativeImpulseScale, |
| | 38 | 442 | | out ExactNormalResponse3D response)) |
| | | 443 | | { |
| | 3 | 444 | | return false; |
| | | 445 | | } |
| | | 446 | | |
| | 35 | 447 | | bool hasNormalVelocity = |
| | 35 | 448 | | response.TryGetNormalVelocity(out Fixed64 normalVelocity); |
| | 35 | 449 | | bool hasAppliedImpulse = |
| | 35 | 450 | | response.TryGetAppliedImpulse(out Fixed64 appliedImpulse); |
| | 35 | 451 | | bool hasAccumulatedImpulse = |
| | 35 | 452 | | response.TryGetAccumulatedImpulse( |
| | 35 | 453 | | out Fixed64 newAccumulatedImpulse); |
| | 35 | 454 | | Fixed64 impulseScalar = hasAccumulatedImpulse |
| | 35 | 455 | | ? newAccumulatedImpulse - accumulatedImpulse |
| | 35 | 456 | | : -accumulatedImpulse; |
| | 35 | 457 | | result = new ContactNormalImpulseResult2D( |
| | 35 | 458 | | normalVelocity, |
| | 35 | 459 | | impulseScalar, |
| | 35 | 460 | | appliedImpulse, |
| | 35 | 461 | | ExactContactLever2D.ToPlanar(response.FirstLinearVelocityDelta), |
| | 35 | 462 | | ExactContactLever2D.ToPlanarAngular(response.FirstAngularVelocityDelta), |
| | 35 | 463 | | ExactContactLever2D.ToPlanar(response.SecondLinearVelocityDelta), |
| | 35 | 464 | | ExactContactLever2D.ToPlanarAngular(response.SecondAngularVelocityDelta), |
| | 35 | 465 | | hasNormalVelocity, |
| | 35 | 466 | | hasAppliedImpulse, |
| | 35 | 467 | | hasAccumulatedImpulse); |
| | 35 | 468 | | return true; |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 472 | | private static bool TryComputeNormalVelocity( |
| | | 473 | | Vector2d linearVelocityA, |
| | | 474 | | Fixed64 angularVelocityA, |
| | | 475 | | Vector2d relativeContactPointA, |
| | | 476 | | Vector2d linearVelocityB, |
| | | 477 | | Fixed64 angularVelocityB, |
| | | 478 | | Vector2d relativeContactPointB, |
| | | 479 | | Vector2d normal, |
| | | 480 | | out Fixed64 normalVelocity) |
| | | 481 | | { |
| | 1031 | 482 | | bool angularAResolved = ContactResponseArithmetic3D.TryCross( |
| | 1031 | 483 | | new Vector3d( |
| | 1031 | 484 | | Fixed64.Zero, |
| | 1031 | 485 | | -angularVelocityA, |
| | 1031 | 486 | | Fixed64.Zero), |
| | 1031 | 487 | | ExactContactLever2D.ToSpatial(relativeContactPointA), |
| | 1031 | 488 | | out Vector3d angularA); |
| | 1031 | 489 | | bool angularBResolved = ContactResponseArithmetic3D.TryCross( |
| | 1031 | 490 | | new Vector3d( |
| | 1031 | 491 | | Fixed64.Zero, |
| | 1031 | 492 | | -angularVelocityB, |
| | 1031 | 493 | | Fixed64.Zero), |
| | 1031 | 494 | | ExactContactLever2D.ToSpatial(relativeContactPointB), |
| | 1031 | 495 | | out Vector3d angularB); |
| | 1031 | 496 | | bool relativeResolved = Vector3d.TrySubtractSums( |
| | 1031 | 497 | | ExactContactLever2D.ToSpatial(linearVelocityB), |
| | 1031 | 498 | | angularB, |
| | 1031 | 499 | | ExactContactLever2D.ToSpatial(linearVelocityA), |
| | 1031 | 500 | | angularA, |
| | 1031 | 501 | | out Vector3d relative); |
| | 1031 | 502 | | bool projectionResolved = ContactResponseArithmetic3D.TryDot( |
| | 1031 | 503 | | relative, |
| | 1031 | 504 | | ExactContactLever2D.ToSpatial(normal), |
| | 1031 | 505 | | out normalVelocity); |
| | 1031 | 506 | | if (!(angularAResolved |
| | 1031 | 507 | | & angularBResolved |
| | 1031 | 508 | | & relativeResolved |
| | 1031 | 509 | | & projectionResolved)) |
| | | 510 | | { |
| | 4 | 511 | | normalVelocity = default; |
| | 4 | 512 | | return false; |
| | | 513 | | } |
| | 1027 | 514 | | return true; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 518 | | internal static bool TryComputeDenominator( |
| | | 519 | | SolidBody2D? bodyA, |
| | | 520 | | Vector2d relativeContactPointA, |
| | | 521 | | SolidBody2D? bodyB, |
| | | 522 | | Vector2d relativeContactPointB, |
| | | 523 | | Vector2d normal, |
| | | 524 | | out Fixed64 denominator) |
| | | 525 | | { |
| | 1081 | 526 | | bool angularAResolved = TryComputeAngularDenominator( |
| | 1081 | 527 | | bodyA, |
| | 1081 | 528 | | relativeContactPointA, |
| | 1081 | 529 | | normal, |
| | 1081 | 530 | | out Fixed64 angularA); |
| | 1081 | 531 | | bool angularBResolved = TryComputeAngularDenominator( |
| | 1081 | 532 | | bodyB, |
| | 1081 | 533 | | relativeContactPointB, |
| | 1081 | 534 | | normal, |
| | 1081 | 535 | | out Fixed64 angularB); |
| | 1081 | 536 | | bool linearAResolved = TryGetConstrainedInverseMass( |
| | 1081 | 537 | | bodyA, |
| | 1081 | 538 | | normal, |
| | 1081 | 539 | | out Fixed64 linearA); |
| | 1081 | 540 | | bool linearBResolved = TryGetConstrainedInverseMass( |
| | 1081 | 541 | | bodyB, |
| | 1081 | 542 | | normal, |
| | 1081 | 543 | | out Fixed64 linearB); |
| | 1081 | 544 | | bool sumResolved = Fixed64.TryAdd( |
| | 1081 | 545 | | linearA, |
| | 1081 | 546 | | linearB, |
| | 1081 | 547 | | out Fixed64 linear) |
| | 1081 | 548 | | & Fixed64.TryAdd( |
| | 1081 | 549 | | linear, |
| | 1081 | 550 | | angularA, |
| | 1081 | 551 | | out Fixed64 first) |
| | 1081 | 552 | | & Fixed64.TryAdd( |
| | 1081 | 553 | | first, |
| | 1081 | 554 | | angularB, |
| | 1081 | 555 | | out denominator); |
| | 1081 | 556 | | if (!(linearAResolved |
| | 1081 | 557 | | & linearBResolved |
| | 1081 | 558 | | & angularAResolved |
| | 1081 | 559 | | & angularBResolved |
| | 1081 | 560 | | & sumResolved)) |
| | | 561 | | { |
| | 13 | 562 | | denominator = default; |
| | 13 | 563 | | return false; |
| | | 564 | | } |
| | 1068 | 565 | | return true; |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 569 | | internal static bool TryComputeAngularDenominator( |
| | | 570 | | SolidBody2D? body, |
| | | 571 | | Vector2d relativeContactPoint, |
| | | 572 | | Vector2d axis, |
| | | 573 | | out Fixed64 denominator) |
| | | 574 | | { |
| | 2500 | 575 | | if (body?.CanRotate != true) |
| | | 576 | | { |
| | 1119 | 577 | | denominator = Fixed64.Zero; |
| | 1119 | 578 | | return true; |
| | | 579 | | } |
| | | 580 | | |
| | 1381 | 581 | | denominator = default; |
| | 1381 | 582 | | bool crossResolved = ContactResponseArithmetic3D.TryCross( |
| | 1381 | 583 | | ExactContactLever2D.ToSpatial(relativeContactPoint), |
| | 1381 | 584 | | ExactContactLever2D.ToSpatial(axis), |
| | 1381 | 585 | | out Vector3d cross); |
| | 1381 | 586 | | bool denominatorResolved = crossResolved |
| | 1381 | 587 | | && Fixed64.TryMultiplyDivide( |
| | 1381 | 588 | | cross.Y, |
| | 1381 | 589 | | cross.Y, |
| | 1381 | 590 | | body.EffectiveInverseMomentOfInertia, |
| | 1381 | 591 | | Fixed64.One, |
| | 1381 | 592 | | out denominator); |
| | 1381 | 593 | | if (!denominatorResolved |
| | 1381 | 594 | | || (denominator == Fixed64.Zero |
| | 1381 | 595 | | && cross.Y != Fixed64.Zero |
| | 1381 | 596 | | && body.EffectiveInverseMomentOfInertia |
| | 1381 | 597 | | != Fixed64.Zero)) |
| | | 598 | | { |
| | 10 | 599 | | denominator = default; |
| | 10 | 600 | | return false; |
| | | 601 | | } |
| | | 602 | | |
| | 1371 | 603 | | return true; |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 607 | | internal static bool TryGetConstrainedInverseMass( |
| | | 608 | | SolidBody2D? body, |
| | | 609 | | Vector2d axis, |
| | | 610 | | out Fixed64 inverseMass) |
| | | 611 | | { |
| | 2182 | 612 | | if (body == null) |
| | | 613 | | { |
| | 54 | 614 | | inverseMass = Fixed64.Zero; |
| | 54 | 615 | | return true; |
| | | 616 | | } |
| | | 617 | | |
| | 2128 | 618 | | inverseMass = body.GetConstrainedInverseMass(axis); |
| | 2128 | 619 | | return inverseMass != Fixed64.Zero |
| | 2128 | 620 | | || body.EffectiveInverseMass == Fixed64.Zero |
| | 2128 | 621 | | || body.ProjectLinearMotion(axis) == Vector2d.Zero; |
| | | 622 | | } |
| | | 623 | | |
| | | 624 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 625 | | internal static bool TryComputeLinearVelocityDelta( |
| | | 626 | | SolidBody2D? body, |
| | | 627 | | Vector2d signedNormal, |
| | | 628 | | Fixed64 impulseScalar, |
| | | 629 | | out Vector2d velocityDelta) => |
| | 223 | 630 | | ContinuousCollisionImpulsePolicy.TryResolveVelocityDelta( |
| | 223 | 631 | | body?.ProjectLinearMotion(signedNormal) ?? Vector2d.Zero, |
| | 223 | 632 | | impulseScalar, |
| | 223 | 633 | | body?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 223 | 634 | | Fixed64.One, |
| | 223 | 635 | | out velocityDelta); |
| | | 636 | | |
| | | 637 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 638 | | internal static bool TryComputeAngularVelocityDelta( |
| | | 639 | | SolidBody2D? body, |
| | | 640 | | Vector2d relativeContactPoint, |
| | | 641 | | Vector2d signedNormal, |
| | | 642 | | Fixed64 impulseScalar, |
| | | 643 | | out Fixed64 velocityDelta) |
| | | 644 | | { |
| | 226 | 645 | | velocityDelta = Fixed64.Zero; |
| | 226 | 646 | | if (body?.CanRotate != true) |
| | 80 | 647 | | return true; |
| | | 648 | | |
| | 146 | 649 | | Vector3d spatialPoint = |
| | 146 | 650 | | ExactContactLever2D.ToSpatial(relativeContactPoint); |
| | 146 | 651 | | Vector3d spatialNormal = |
| | 146 | 652 | | ExactContactLever2D.ToSpatial(signedNormal); |
| | 146 | 653 | | return ContactResponseArithmetic3D.TryCross( |
| | 146 | 654 | | spatialPoint, |
| | 146 | 655 | | spatialNormal, |
| | 146 | 656 | | out Vector3d cross) |
| | 146 | 657 | | && Fixed64.TryMultiplyDivide( |
| | 146 | 658 | | -cross.Y, |
| | 146 | 659 | | impulseScalar, |
| | 146 | 660 | | body.EffectiveInverseMomentOfInertia, |
| | 146 | 661 | | Fixed64.One, |
| | 146 | 662 | | out velocityDelta); |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 666 | | private static bool TryResolveAngularVelocityDelta( |
| | | 667 | | SolidBody2D? body, |
| | | 668 | | Vector2d relativeContactPoint, |
| | | 669 | | Vector2d signedNormal, |
| | | 670 | | Fixed64 normalVelocity, |
| | | 671 | | Fixed64 responseFactor, |
| | | 672 | | Fixed64 denominator, |
| | | 673 | | out Fixed64 velocityDelta) |
| | | 674 | | { |
| | 60 | 675 | | velocityDelta = Fixed64.Zero; |
| | 60 | 676 | | if (body?.CanRotate != true) |
| | 20 | 677 | | return true; |
| | | 678 | | |
| | 40 | 679 | | Fixed64 torqueScale = Vector2d.CrossProduct(relativeContactPoint, signedNormal); |
| | 40 | 680 | | if (torqueScale == Fixed64.Zero) |
| | 28 | 681 | | return true; |
| | | 682 | | |
| | 12 | 683 | | bool angularScaleResolved = Fixed64.TryMultiplyDivide( |
| | 12 | 684 | | normalVelocity, |
| | 12 | 685 | | responseFactor, |
| | 12 | 686 | | body.EffectiveInverseMomentOfInertia, |
| | 12 | 687 | | denominator, |
| | 12 | 688 | | out Fixed64 angularScale); |
| | 12 | 689 | | bool velocityDeltaResolved = Fixed64.TryMultiplyDivide( |
| | 12 | 690 | | torqueScale, |
| | 12 | 691 | | angularScale, |
| | 12 | 692 | | Fixed64.One, |
| | 12 | 693 | | out velocityDelta); |
| | 12 | 694 | | return angularScaleResolved |
| | 12 | 695 | | & (angularScale != Fixed64.Zero | torqueScale.Abs() <= Fixed64.One) |
| | 12 | 696 | | & velocityDeltaResolved; |
| | | 697 | | } |
| | | 698 | | |
| | | 699 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 700 | | private static ContactNormalImpulseResult2D Zero(Fixed64 normalVelocity) => |
| | 915 | 701 | | new( |
| | 915 | 702 | | normalVelocity, |
| | 915 | 703 | | Fixed64.Zero, |
| | 915 | 704 | | Vector2d.Zero, |
| | 915 | 705 | | Fixed64.Zero, |
| | 915 | 706 | | Vector2d.Zero, |
| | 915 | 707 | | Fixed64.Zero); |
| | | 708 | | |
| | | 709 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 710 | | private static ContactNormalVelocityDeltaResult2D ZeroVelocityDelta(Fixed64 normalVelocity) => |
| | 1 | 711 | | new( |
| | 1 | 712 | | normalVelocity, |
| | 1 | 713 | | Vector2d.Zero, |
| | 1 | 714 | | Fixed64.Zero, |
| | 1 | 715 | | Vector2d.Zero, |
| | 1 | 716 | | Fixed64.Zero); |
| | | 717 | | } |