| | | 1 | | //======================================================================= |
| | | 2 | | // ExactContactLever2D.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 Gravitas.Colliders; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adapts planar body mobility and signed yaw to the shared exact 3D response |
| | | 15 | | /// kernel. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ExactContactLever2D |
| | | 18 | | { |
| | | 19 | | internal static ContactAnchor GetDetectedNormalAnchor(LSCollider2D owner, int featureNamespace, |
| | | 20 | | ContactAnchor bodyCenter, ContactAnchor surfaceAnchor) |
| | | 21 | | { |
| | 293705 | 22 | | if (owner.Body == null) return surfaceAnchor; |
| | 77801 | 23 | | LSCollider2D leaf = owner is LSCompoundCollider2D compound && featureNamespace < 0 |
| | 77801 | 24 | | ? compound.GetPartCollider(-featureNamespace - 1) : owner; |
| | 77801 | 25 | | if (leaf is not LSCircleCollider2D |
| | 77801 | 26 | | && !(leaf is LSCapsuleCollider2D capsule && capsule.AxisLength == Fixed64.Zero)) |
| | 48726 | 27 | | return surfaceAnchor; |
| | | 28 | | |
| | | 29 | | // A circular slab's normal line passes through its shape center. Its |
| | | 30 | | // rounded radial surface offset must not create a spurious yaw Jacobian: |
| | | 31 | | // with translation locked, even one raw lever unit can stop all spin. |
| | | 32 | | // Retain center-to-COM torque for offset circles and compound leaves; |
| | | 33 | | // friction continues to use the original finite surface anchor. |
| | 29075 | 34 | | if (leaf.TransformRelativeMassPropertyPointExact(Vector2d.Zero).TryGetPoint(out Vector2d localCenter)) |
| | 29073 | 35 | | return new ContactAnchor(bodyCenter.Origin, bodyCenter.Rotation, ToSpatial(localCenter)); |
| | | 36 | | |
| | | 37 | | // A compound-local center may exceed Q32.32 while its world center is |
| | | 38 | | // finite. Detected circular side/support anchors retain that frozen |
| | | 39 | | // center as their origin. Cap normals have no planar yaw coupling. |
| | 2 | 40 | | return ContactAnchor.FromWorldPoint(surfaceAnchor.Origin); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static bool CanUseCompactResponse( |
| | | 44 | | SolidBody2D? bodyA, |
| | | 45 | | Vector2d linearVelocityA, |
| | | 46 | | Fixed64 angularVelocityA, |
| | | 47 | | Vector2d relativeContactPointA, |
| | | 48 | | SolidBody2D? bodyB, |
| | | 49 | | Vector2d linearVelocityB, |
| | | 50 | | Fixed64 angularVelocityB, |
| | | 51 | | Vector2d relativeContactPointB, |
| | | 52 | | Vector2d axis) |
| | | 53 | | { |
| | 32540 | 54 | | Vector3d spatialAxis = ToSpatial(axis); |
| | 32540 | 55 | | Vector3d leverA = ToSpatial(relativeContactPointA); |
| | 32540 | 56 | | Vector3d leverB = ToSpatial(relativeContactPointB); |
| | 32540 | 57 | | return ContactResponseArithmetic3D.CanUseFastPointVelocity( |
| | 32540 | 58 | | ToSpatial(linearVelocityA), |
| | 32540 | 59 | | new Vector3d( |
| | 32540 | 60 | | Fixed64.Zero, |
| | 32540 | 61 | | -angularVelocityA, |
| | 32540 | 62 | | Fixed64.Zero), |
| | 32540 | 63 | | leverA, |
| | 32540 | 64 | | ToSpatial(linearVelocityB), |
| | 32540 | 65 | | new Vector3d( |
| | 32540 | 66 | | Fixed64.Zero, |
| | 32540 | 67 | | -angularVelocityB, |
| | 32540 | 68 | | Fixed64.Zero), |
| | 32540 | 69 | | leverB, |
| | 32540 | 70 | | spatialAxis) |
| | 32540 | 71 | | && ContactResponseArithmetic3D.CanUseFastAngularResponse( |
| | 32540 | 72 | | leverA, |
| | 32540 | 73 | | spatialAxis, |
| | 32540 | 74 | | CreateInverseInertia(bodyA)) |
| | 32540 | 75 | | && ContactResponseArithmetic3D.CanUseFastAngularResponse( |
| | 32540 | 76 | | leverB, |
| | 32540 | 77 | | spatialAxis, |
| | 32540 | 78 | | CreateInverseInertia(bodyB)); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | internal static ExactContactResponseOperand3D CreateResponseOperand( |
| | | 82 | | SolidBody2D? body, |
| | | 83 | | Vector2d linearVelocity, |
| | | 84 | | Fixed64 angularVelocity, |
| | | 85 | | in ExactLever3D lever, |
| | | 86 | | Vector3d signedAxis) => |
| | 4876 | 87 | | new( |
| | 4876 | 88 | | lever, |
| | 4876 | 89 | | ToSpatial(linearVelocity), |
| | 4876 | 90 | | new Vector3d( |
| | 4876 | 91 | | Fixed64.Zero, |
| | 4876 | 92 | | -angularVelocity, |
| | 4876 | 93 | | Fixed64.Zero), |
| | 4876 | 94 | | body == null |
| | 4876 | 95 | | ? Vector3d.Zero |
| | 4876 | 96 | | : ToSpatial(body.ProjectLinearMotion(ToPlanar(signedAxis))), |
| | 4876 | 97 | | body?.EffectiveInverseMass ?? Fixed64.Zero, |
| | 4876 | 98 | | CreateInverseInertia(body), angularVelocity); |
| | | 99 | | |
| | | 100 | | internal static bool TryGetNormalResponse( |
| | | 101 | | SolidBody2D? bodyA, |
| | | 102 | | Vector2d linearVelocityA, |
| | | 103 | | Fixed64 angularVelocityA, |
| | | 104 | | in ExactLever3D relativeContactPointA, |
| | | 105 | | SolidBody2D? bodyB, |
| | | 106 | | Vector2d linearVelocityB, |
| | | 107 | | Fixed64 angularVelocityB, |
| | | 108 | | in ExactLever3D relativeContactPointB, |
| | | 109 | | Vector2d normal, |
| | | 110 | | Fixed64 restitution, |
| | | 111 | | Fixed64 restitutionVelocityThreshold, |
| | | 112 | | out ExactNormalResponse3D response) |
| | | 113 | | { |
| | 34 | 114 | | Vector3d spatialNormal = ToSpatial(normal); |
| | 34 | 115 | | ExactContactResponseOperand3D first = CreateResponseOperand( |
| | 34 | 116 | | bodyA, |
| | 34 | 117 | | linearVelocityA, |
| | 34 | 118 | | angularVelocityA, |
| | 34 | 119 | | relativeContactPointA, |
| | 34 | 120 | | -spatialNormal); |
| | 34 | 121 | | ExactContactResponseOperand3D second = CreateResponseOperand( |
| | 34 | 122 | | bodyB, |
| | 34 | 123 | | linearVelocityB, |
| | 34 | 124 | | angularVelocityB, |
| | 34 | 125 | | relativeContactPointB, |
| | 34 | 126 | | spatialNormal); |
| | 34 | 127 | | return ExactContactResponseKernel.TryGetNormalResponse( |
| | 34 | 128 | | first, |
| | 34 | 129 | | second, |
| | 34 | 130 | | spatialNormal, |
| | 34 | 131 | | restitution, |
| | 34 | 132 | | restitutionVelocityThreshold, |
| | 34 | 133 | | out response); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | internal static bool TryGetAccumulatedNormalResponse( |
| | | 137 | | SolidBody2D? bodyA, |
| | | 138 | | Vector2d linearVelocityA, |
| | | 139 | | Fixed64 angularVelocityA, |
| | | 140 | | in ExactLever3D relativeContactPointA, |
| | | 141 | | SolidBody2D? bodyB, |
| | | 142 | | Vector2d linearVelocityB, |
| | | 143 | | Fixed64 angularVelocityB, |
| | | 144 | | in ExactLever3D relativeContactPointB, |
| | | 145 | | Vector2d normal, |
| | | 146 | | Fixed64 restitution, |
| | | 147 | | Fixed64 restitutionVelocityThreshold, |
| | | 148 | | Fixed64 accumulatedImpulse, |
| | | 149 | | Fixed64 positiveImpulseScale, |
| | | 150 | | Fixed64 negativeImpulseScale, |
| | | 151 | | out ExactNormalResponse3D response, |
| | | 152 | | in ContactResponseSnapshot impact = default) |
| | | 153 | | { |
| | 370 | 154 | | Vector3d spatialNormal = ToSpatial(normal); |
| | 370 | 155 | | ExactContactResponseOperand3D first = CreateResponseOperand( |
| | 370 | 156 | | bodyA, |
| | 370 | 157 | | linearVelocityA, |
| | 370 | 158 | | angularVelocityA, |
| | 370 | 159 | | relativeContactPointA, |
| | 370 | 160 | | -spatialNormal); |
| | 370 | 161 | | ExactContactResponseOperand3D second = CreateResponseOperand( |
| | 370 | 162 | | bodyB, |
| | 370 | 163 | | linearVelocityB, |
| | 370 | 164 | | angularVelocityB, |
| | 370 | 165 | | relativeContactPointB, |
| | 370 | 166 | | spatialNormal); |
| | 370 | 167 | | return ExactContactResponseKernel.TryGetAccumulatedNormalResponse( |
| | 370 | 168 | | first, |
| | 370 | 169 | | second, |
| | 370 | 170 | | spatialNormal, |
| | 370 | 171 | | restitution, |
| | 370 | 172 | | restitutionVelocityThreshold, |
| | 370 | 173 | | accumulatedImpulse, |
| | 370 | 174 | | positiveImpulseScale, |
| | 370 | 175 | | negativeImpulseScale, |
| | 370 | 176 | | out response, impact); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | internal static Vector3d ToSpatial(Vector2d vector) => |
| | 1334418 | 180 | | new(vector.X, Fixed64.Zero, vector.Y); |
| | | 181 | | |
| | | 182 | | internal static Vector2d ToPlanar(Vector3d vector) => |
| | 179555 | 183 | | new(vector.X, vector.Z); |
| | | 184 | | |
| | | 185 | | internal static Fixed64 ToPlanarAngular(Vector3d vector) |
| | | 186 | | { |
| | | 187 | | // Exact response operands preflight the planar delta before publication. |
| | | 188 | | System.Diagnostics.Debug.Assert(vector.Y != Fixed64.MinValue); |
| | 177277 | 189 | | return -vector.Y; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal static bool TryGetImpulseVelocityDeltas( |
| | | 193 | | SolidBody2D? bodyA, |
| | | 194 | | in ExactLever3D relativeContactPointA, |
| | | 195 | | SolidBody2D? bodyB, |
| | | 196 | | in ExactLever3D relativeContactPointB, |
| | | 197 | | Vector2d firstAxis, |
| | | 198 | | Fixed64 firstScale, |
| | | 199 | | Vector2d secondAxis, |
| | | 200 | | Fixed64 secondScale, |
| | | 201 | | out Vector2d linearVelocityDeltaA, |
| | | 202 | | out Fixed64 angularVelocityDeltaA, |
| | | 203 | | out Vector2d linearVelocityDeltaB, |
| | | 204 | | out Fixed64 angularVelocityDeltaB) |
| | | 205 | | { |
| | 4 | 206 | | Vector3d first = ToSpatial(firstAxis); |
| | 4 | 207 | | Vector3d second = ToSpatial(secondAxis); |
| | 4 | 208 | | bool firstResolved = TryGetParticipantVelocityDeltas( |
| | 4 | 209 | | bodyA, |
| | 4 | 210 | | relativeContactPointA, |
| | 4 | 211 | | -first, |
| | 4 | 212 | | firstScale, |
| | 4 | 213 | | -second, |
| | 4 | 214 | | secondScale, |
| | 4 | 215 | | out linearVelocityDeltaA, |
| | 4 | 216 | | out angularVelocityDeltaA); |
| | 4 | 217 | | bool secondResolved = TryGetParticipantVelocityDeltas( |
| | 4 | 218 | | bodyB, |
| | 4 | 219 | | relativeContactPointB, |
| | 4 | 220 | | first, |
| | 4 | 221 | | firstScale, |
| | 4 | 222 | | second, |
| | 4 | 223 | | secondScale, |
| | 4 | 224 | | out linearVelocityDeltaB, |
| | 4 | 225 | | out angularVelocityDeltaB); |
| | 4 | 226 | | return firstResolved & secondResolved; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | internal static bool TryGetParticipantVelocityDeltas( |
| | | 230 | | SolidBody2D? body, |
| | | 231 | | in ExactLever3D lever, |
| | | 232 | | Vector3d firstAxis, |
| | | 233 | | Fixed64 firstScale, |
| | | 234 | | Vector3d secondAxis, |
| | | 235 | | Fixed64 secondScale, |
| | | 236 | | out Vector2d linearVelocityDelta, |
| | | 237 | | out Fixed64 angularVelocityDelta) |
| | | 238 | | { |
| | 14 | 239 | | linearVelocityDelta = Vector2d.Zero; |
| | 14 | 240 | | angularVelocityDelta = Fixed64.Zero; |
| | 14 | 241 | | if (body?.HasSolverMobility != true) |
| | 6 | 242 | | return true; |
| | | 243 | | |
| | 8 | 244 | | Vector3d spatialLinear = Vector3d.Zero; |
| | 8 | 245 | | bool linearResolved = !body.CanTranslate |
| | 8 | 246 | | || Vector3d.TryScaledLinearCombination( |
| | 8 | 247 | | ToSpatial(body.ProjectLinearMotion(ToPlanar(firstAxis))), |
| | 8 | 248 | | firstScale, |
| | 8 | 249 | | ToSpatial(body.ProjectLinearMotion(ToPlanar(secondAxis))), |
| | 8 | 250 | | secondScale, |
| | 8 | 251 | | Vector3d.Zero, |
| | 8 | 252 | | Fixed64.Zero, |
| | 8 | 253 | | body.EffectiveInverseMass, |
| | 8 | 254 | | out spatialLinear); |
| | 8 | 255 | | Vector3d spatialAngular = Vector3d.Zero; |
| | 8 | 256 | | bool angularResolved = !body.CanRotate |
| | 8 | 257 | | || ExactLever3D.TryGetTransformedWeightedCrossProduct( |
| | 8 | 258 | | lever, |
| | 8 | 259 | | firstAxis, |
| | 8 | 260 | | firstScale, |
| | 8 | 261 | | secondAxis, |
| | 8 | 262 | | secondScale, |
| | 8 | 263 | | Vector3d.Zero, |
| | 8 | 264 | | Fixed64.Zero, |
| | 8 | 265 | | CreateInverseInertia(body), |
| | 8 | 266 | | out spatialAngular); |
| | 8 | 267 | | linearVelocityDelta = ToPlanar(spatialLinear); |
| | 8 | 268 | | bool planarResolved = Fixed64.TrySubtract(Fixed64.Zero, spatialAngular.Y, out angularVelocityDelta); |
| | 8 | 269 | | return linearResolved & angularResolved & planarResolved; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | internal static Fixed3x3 CreateInverseInertia(SolidBody2D? body) => |
| | 254256 | 273 | | body?.CanRotate == true |
| | 254256 | 274 | | ? new Fixed3x3( |
| | 254256 | 275 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 254256 | 276 | | Fixed64.Zero, body.EffectiveInverseMomentOfInertia, Fixed64.Zero, |
| | 254256 | 277 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero) |
| | 254256 | 278 | | : Fixed3x3.Zero; |
| | | 279 | | } |