| | | 1 | | //======================================================================= |
| | | 2 | | // ExactContactLever3D.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 | | |
| | | 11 | | namespace Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Evaluates solver products for a semantic lever that cannot be narrowed to |
| | | 15 | | /// <see cref="Vector3d"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ExactContactLever3D |
| | | 18 | | { |
| | | 19 | | internal static bool TryGetNormalResponse( |
| | | 20 | | SolidBody? bodyA, |
| | | 21 | | Vector3d linearVelocityA, |
| | | 22 | | Vector3d angularVelocityA, |
| | | 23 | | in ExactLever3D relativeContactPointA, |
| | | 24 | | SolidBody? bodyB, |
| | | 25 | | Vector3d linearVelocityB, |
| | | 26 | | Vector3d angularVelocityB, |
| | | 27 | | in ExactLever3D relativeContactPointB, |
| | | 28 | | Vector3d normal, |
| | | 29 | | Fixed64 restitution, |
| | | 30 | | Fixed64 restitutionVelocityThreshold, |
| | | 31 | | out ExactNormalResponse3D response) |
| | | 32 | | { |
| | 57 | 33 | | ExactContactResponseOperand3D first = CreateResponseOperand( |
| | 57 | 34 | | bodyA, |
| | 57 | 35 | | linearVelocityA, |
| | 57 | 36 | | angularVelocityA, |
| | 57 | 37 | | relativeContactPointA, |
| | 57 | 38 | | -normal); |
| | 57 | 39 | | ExactContactResponseOperand3D second = CreateResponseOperand( |
| | 57 | 40 | | bodyB, |
| | 57 | 41 | | linearVelocityB, |
| | 57 | 42 | | angularVelocityB, |
| | 57 | 43 | | relativeContactPointB, |
| | 57 | 44 | | normal); |
| | 57 | 45 | | return ExactContactResponseKernel.TryGetNormalResponse( |
| | 57 | 46 | | first, |
| | 57 | 47 | | second, |
| | 57 | 48 | | normal, |
| | 57 | 49 | | restitution, |
| | 57 | 50 | | restitutionVelocityThreshold, |
| | 57 | 51 | | out response); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | internal static bool TryGetAccumulatedNormalResponse( |
| | | 55 | | SolidBody? bodyA, |
| | | 56 | | Vector3d linearVelocityA, |
| | | 57 | | Vector3d angularVelocityA, |
| | | 58 | | in ExactLever3D relativeContactPointA, |
| | | 59 | | SolidBody? bodyB, |
| | | 60 | | Vector3d linearVelocityB, |
| | | 61 | | Vector3d angularVelocityB, |
| | | 62 | | in ExactLever3D relativeContactPointB, |
| | | 63 | | Vector3d normal, |
| | | 64 | | Fixed64 restitution, |
| | | 65 | | Fixed64 restitutionVelocityThreshold, |
| | | 66 | | Fixed64 accumulatedImpulse, |
| | | 67 | | Fixed64 positiveImpulseScale, |
| | | 68 | | Fixed64 negativeImpulseScale, |
| | | 69 | | out ExactNormalResponse3D response) |
| | | 70 | | { |
| | 1394 | 71 | | ExactContactResponseOperand3D first = CreateResponseOperand( |
| | 1394 | 72 | | bodyA, |
| | 1394 | 73 | | linearVelocityA, |
| | 1394 | 74 | | angularVelocityA, |
| | 1394 | 75 | | relativeContactPointA, |
| | 1394 | 76 | | -normal); |
| | 1394 | 77 | | ExactContactResponseOperand3D second = CreateResponseOperand( |
| | 1394 | 78 | | bodyB, |
| | 1394 | 79 | | linearVelocityB, |
| | 1394 | 80 | | angularVelocityB, |
| | 1394 | 81 | | relativeContactPointB, |
| | 1394 | 82 | | normal); |
| | 1394 | 83 | | return ExactContactResponseKernel.TryGetAccumulatedNormalResponse( |
| | 1394 | 84 | | first, |
| | 1394 | 85 | | second, |
| | 1394 | 86 | | normal, |
| | 1394 | 87 | | restitution, |
| | 1394 | 88 | | restitutionVelocityThreshold, |
| | 1394 | 89 | | accumulatedImpulse, |
| | 1394 | 90 | | positiveImpulseScale, |
| | 1394 | 91 | | negativeImpulseScale, |
| | 1394 | 92 | | out response); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | internal static ExactContactResponseOperand3D CreateResponseOperand( |
| | | 96 | | SolidBody? body, |
| | | 97 | | Vector3d linearVelocity, |
| | | 98 | | Vector3d angularVelocity, |
| | | 99 | | in ExactLever3D relativeContactPoint, |
| | | 100 | | Vector3d signedNormal) => |
| | 4646 | 101 | | new( |
| | 4646 | 102 | | relativeContactPoint, |
| | 4646 | 103 | | linearVelocity, |
| | 4646 | 104 | | angularVelocity, |
| | 4646 | 105 | | body?.ProjectLinearMotion(signedNormal) ?? Vector3d.Zero, |
| | 4646 | 106 | | body?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 4646 | 107 | | body?.GetConstrainedInverseInertiaTensor() ?? Fixed3x3.Zero); |
| | | 108 | | |
| | | 109 | | internal static bool TryGetAngularVelocityDelta( |
| | | 110 | | SolidBody? body, |
| | | 111 | | in ExactLever3D relativeContactPoint, |
| | | 112 | | Vector3d impulse, |
| | | 113 | | out Vector3d velocityDelta) |
| | | 114 | | { |
| | 99 | 115 | | velocityDelta = Vector3d.Zero; |
| | 99 | 116 | | return body?.CanRotate != true |
| | 99 | 117 | | || ExactLever3D.TryGetTransformedScaledCrossProduct( |
| | 99 | 118 | | relativeContactPoint, |
| | 99 | 119 | | impulse, |
| | 99 | 120 | | body.GetConstrainedInverseInertiaTensor(), |
| | 99 | 121 | | Fixed64.One, |
| | 99 | 122 | | Fixed64.One, |
| | 99 | 123 | | Fixed64.One, |
| | 99 | 124 | | out velocityDelta); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal static bool TryGetImpulseCombinationVelocityDeltas( |
| | | 128 | | SolidBody? bodyA, |
| | | 129 | | in ExactLever3D relativeContactPointA, |
| | | 130 | | SolidBody? bodyB, |
| | | 131 | | in ExactLever3D relativeContactPointB, |
| | | 132 | | Vector3d firstAxis, |
| | | 133 | | Fixed64 firstScale, |
| | | 134 | | Vector3d secondAxis, |
| | | 135 | | Fixed64 secondScale, |
| | | 136 | | Vector3d thirdAxis, |
| | | 137 | | Fixed64 thirdScale, |
| | | 138 | | out Vector3d linearVelocityDeltaA, |
| | | 139 | | out Vector3d angularVelocityDeltaA, |
| | | 140 | | out Vector3d linearVelocityDeltaB, |
| | | 141 | | out Vector3d angularVelocityDeltaB) |
| | | 142 | | { |
| | 9 | 143 | | bool linearAResolved = TryGetLinearCombinationVelocityDelta( |
| | 9 | 144 | | bodyA, |
| | 9 | 145 | | -firstAxis, |
| | 9 | 146 | | firstScale, |
| | 9 | 147 | | -secondAxis, |
| | 9 | 148 | | secondScale, |
| | 9 | 149 | | -thirdAxis, |
| | 9 | 150 | | thirdScale, |
| | 9 | 151 | | out linearVelocityDeltaA); |
| | 9 | 152 | | bool angularAResolved = TryGetAngularCombinationVelocityDelta( |
| | 9 | 153 | | bodyA, |
| | 9 | 154 | | relativeContactPointA, |
| | 9 | 155 | | -firstAxis, |
| | 9 | 156 | | firstScale, |
| | 9 | 157 | | -secondAxis, |
| | 9 | 158 | | secondScale, |
| | 9 | 159 | | -thirdAxis, |
| | 9 | 160 | | thirdScale, |
| | 9 | 161 | | out angularVelocityDeltaA); |
| | 9 | 162 | | bool linearBResolved = TryGetLinearCombinationVelocityDelta( |
| | 9 | 163 | | bodyB, |
| | 9 | 164 | | firstAxis, |
| | 9 | 165 | | firstScale, |
| | 9 | 166 | | secondAxis, |
| | 9 | 167 | | secondScale, |
| | 9 | 168 | | thirdAxis, |
| | 9 | 169 | | thirdScale, |
| | 9 | 170 | | out linearVelocityDeltaB); |
| | 9 | 171 | | bool angularBResolved = TryGetAngularCombinationVelocityDelta( |
| | 9 | 172 | | bodyB, |
| | 9 | 173 | | relativeContactPointB, |
| | 9 | 174 | | firstAxis, |
| | 9 | 175 | | firstScale, |
| | 9 | 176 | | secondAxis, |
| | 9 | 177 | | secondScale, |
| | 9 | 178 | | thirdAxis, |
| | 9 | 179 | | thirdScale, |
| | 9 | 180 | | out angularVelocityDeltaB); |
| | 9 | 181 | | return linearAResolved |
| | 9 | 182 | | & angularAResolved |
| | 9 | 183 | | & linearBResolved |
| | 9 | 184 | | & angularBResolved; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | private static bool TryGetLinearCombinationVelocityDelta( |
| | | 188 | | SolidBody? body, |
| | | 189 | | Vector3d firstAxis, |
| | | 190 | | Fixed64 firstScale, |
| | | 191 | | Vector3d secondAxis, |
| | | 192 | | Fixed64 secondScale, |
| | | 193 | | Vector3d thirdAxis, |
| | | 194 | | Fixed64 thirdScale, |
| | | 195 | | out Vector3d velocityDelta) |
| | | 196 | | { |
| | 18 | 197 | | velocityDelta = Vector3d.Zero; |
| | 18 | 198 | | return body?.CanTranslate != true |
| | 18 | 199 | | || Vector3d.TryScaledLinearCombination( |
| | 18 | 200 | | body.ProjectLinearMotion(firstAxis), |
| | 18 | 201 | | firstScale, |
| | 18 | 202 | | body.ProjectLinearMotion(secondAxis), |
| | 18 | 203 | | secondScale, |
| | 18 | 204 | | body.ProjectLinearMotion(thirdAxis), |
| | 18 | 205 | | thirdScale, |
| | 18 | 206 | | body.EffectiveInverseMass, |
| | 18 | 207 | | out velocityDelta); |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | private static bool TryGetAngularCombinationVelocityDelta( |
| | | 211 | | SolidBody? body, |
| | | 212 | | in ExactLever3D relativeContactPoint, |
| | | 213 | | Vector3d firstAxis, |
| | | 214 | | Fixed64 firstScale, |
| | | 215 | | Vector3d secondAxis, |
| | | 216 | | Fixed64 secondScale, |
| | | 217 | | Vector3d thirdAxis, |
| | | 218 | | Fixed64 thirdScale, |
| | | 219 | | out Vector3d velocityDelta) |
| | | 220 | | { |
| | 18 | 221 | | velocityDelta = Vector3d.Zero; |
| | 18 | 222 | | return body?.CanRotate != true |
| | 18 | 223 | | || ExactLever3D.TryGetTransformedWeightedCrossProduct( |
| | 18 | 224 | | relativeContactPoint, |
| | 18 | 225 | | firstAxis, |
| | 18 | 226 | | firstScale, |
| | 18 | 227 | | secondAxis, |
| | 18 | 228 | | secondScale, |
| | 18 | 229 | | thirdAxis, |
| | 18 | 230 | | thirdScale, |
| | 18 | 231 | | body.GetConstrainedInverseInertiaTensor(), |
| | 18 | 232 | | out velocityDelta); |
| | | 233 | | } |
| | | 234 | | } |