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