| | | 1 | | //======================================================================= |
| | | 2 | | // JointSolver3D.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; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.Constraints; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Builds and solves spatial joint rows, applying cached impulses and recording solve metrics. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class JointSolver3D |
| | | 19 | | { |
| | | 20 | | internal const int MaxRowsPerJoint = 12; |
| | | 21 | | // Fraction of structural error targeted by the next fixed step. |
| | 1 | 22 | | private static readonly Fixed64 BiasFactor = Fixed64.FromFraction(2, 5); |
| | 1 | 23 | | private static readonly Fixed64 RowEpsilon = Fixed64.Epsilon; |
| | | 24 | | // 4,096 raw Q32.32 units, approximately 9.536743e-7 radians. |
| | 1 | 25 | | private static readonly Fixed64 QuaternionTwistVectorEpsilon = Fixed64.FromRaw(4_096); |
| | | 26 | | |
| | | 27 | | internal static void Solve(Joint3D joint, bool applyCachedImpulse, Fixed64 linearCorrectionRate) |
| | | 28 | | { |
| | 125395 | 29 | | Span<JointConstraintRow3D> rows = stackalloc JointConstraintRow3D[MaxRowsPerJoint]; |
| | 125395 | 30 | | int rowCount = BuildRows( |
| | 125395 | 31 | | joint, |
| | 125395 | 32 | | rows, |
| | 125395 | 33 | | out Fixed64 linearAnchorErrorMagnitude, |
| | 125395 | 34 | | out Fixed64 angularLimitErrorMagnitude, |
| | 125395 | 35 | | out Fixed64 motorErrorMagnitude); |
| | 125395 | 36 | | if (!TryPrepareBiasVelocities(joint, rows[..rowCount], linearCorrectionRate)) |
| | | 37 | | { |
| | 6 | 38 | | joint.LastSolvedRowCount = 0; |
| | 6 | 39 | | joint.LastSolveMetrics = default; |
| | 6 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 125389 | 43 | | Fixed64 incrementalImpulseMagnitude = Fixed64.Zero; |
| | 125389 | 44 | | Fixed64 motorImpulseMagnitude = Fixed64.Zero; |
| | 125389 | 45 | | int clampedRowCount = 0; |
| | 1026724 | 46 | | for (int i = 0; i < rowCount; i++) |
| | | 47 | | { |
| | 387973 | 48 | | JointConstraintRow3D row = rows[i]; |
| | 387973 | 49 | | if (applyCachedImpulse) |
| | | 50 | | { |
| | 35760 | 51 | | row.AccumulatedImpulse = ApplyImpulse(joint.BodyA, joint.BodyB, row, |
| | 35760 | 52 | | joint.GetCachedImpulse(row.CacheIndex)); |
| | | 53 | | } |
| | | 54 | | |
| | 387973 | 55 | | Fixed64 impulse = SolveRow(joint.BodyA, joint.BodyB, row, out bool clamped); |
| | 387973 | 56 | | row.AccumulatedImpulse += impulse; |
| | 387973 | 57 | | rows[i] = row; |
| | 387973 | 58 | | joint.SetCachedImpulse(row.CacheIndex, row.AccumulatedImpulse); |
| | 387973 | 59 | | Fixed64 rowImpulseMagnitude = impulse.Abs(); |
| | 387973 | 60 | | incrementalImpulseMagnitude += rowImpulseMagnitude; |
| | 387973 | 61 | | if (row.Kind == JointConstraintRowKind3D.Motor) |
| | 3136 | 62 | | motorImpulseMagnitude += rowImpulseMagnitude; |
| | 387973 | 63 | | if (clamped) |
| | 1237 | 64 | | clampedRowCount++; |
| | | 65 | | } |
| | | 66 | | |
| | 125389 | 67 | | Fixed64 impulseMagnitude = Fixed64.Zero; |
| | 1026724 | 68 | | for (int i = 0; i < rowCount; i++) |
| | 387973 | 69 | | impulseMagnitude += rows[i].AccumulatedImpulse.Abs(); |
| | | 70 | | |
| | 2484168 | 71 | | for (int i = rowCount; i < MaxRowsPerJoint; i++) |
| | 1116695 | 72 | | joint.SetCachedImpulse(i, Fixed64.Zero); |
| | | 73 | | |
| | 125389 | 74 | | joint.LastSolvedRowCount = rowCount; |
| | 125389 | 75 | | joint.AccumulatedImpulseMagnitude += incrementalImpulseMagnitude; |
| | 125389 | 76 | | joint.LastSolveMetrics = new JointSolveMetrics3D( |
| | 125389 | 77 | | rowCount, |
| | 125389 | 78 | | linearAnchorErrorMagnitude, |
| | 125389 | 79 | | angularLimitErrorMagnitude, |
| | 125389 | 80 | | impulseMagnitude, |
| | 125389 | 81 | | incrementalImpulseMagnitude, |
| | 125389 | 82 | | motorImpulseMagnitude, |
| | 125389 | 83 | | motorErrorMagnitude, |
| | 125389 | 84 | | clampedRowCount); |
| | 125389 | 85 | | if (incrementalImpulseMagnitude > Fixed64.Zero) |
| | 74265 | 86 | | joint.Context.Diagnostics.EmitJointImpulse(joint, joint.LastSolveMetrics); |
| | 125389 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static bool TryPrepareBiasVelocities(Joint3D joint, Span<JointConstraintRow3D> rows, |
| | | 90 | | Fixed64 linearCorrectionRate) |
| | | 91 | | { |
| | 1026754 | 92 | | foreach (ref JointConstraintRow3D row in rows) |
| | | 93 | | { |
| | | 94 | | // Structural error is corrected per fixed step. Motor gains already target velocity. |
| | 387985 | 95 | | if (row.Kind != JointConstraintRowKind3D.Motor && !Fixed64.TryMultiplyDivide( |
| | 387985 | 96 | | row.BiasVelocity, |
| | 387985 | 97 | | row.Kind == JointConstraintRowKind3D.Linear ? linearCorrectionRate : joint.Context.InvDeltaTime, |
| | 387985 | 98 | | Fixed64.One, out row.BiasVelocity)) |
| | | 99 | | { |
| | 12 | 100 | | if (ComputeDenominator(joint.BodyA, joint.BodyB, row) <= RowEpsilon) |
| | | 101 | | { |
| | 6 | 102 | | row.BiasVelocity = Fixed64.Zero; |
| | 6 | 103 | | continue; |
| | | 104 | | } |
| | 6 | 105 | | GravitasLogger.Channel.Write(DiagnosticLevel.Error, |
| | 6 | 106 | | "A 3D joint's correction velocity cannot be represented.", nameof(JointSolver3D)); |
| | 6 | 107 | | return false; |
| | | 108 | | } |
| | | 109 | | } |
| | 125389 | 110 | | return true; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | internal static bool TryGetLinearCorrectionDistance(Joint3D joint, out Fixed64 distance) |
| | | 114 | | { |
| | 11498 | 115 | | distance = Fixed64.Zero; |
| | 11498 | 116 | | if (!TryPrepareAnchors(joint, out Vector3d relativeA, out Vector3d relativeB, out Vector3d error)) |
| | 6 | 117 | | return false; |
| | 11492 | 118 | | var row = new JointConstraintRow3D |
| | 11492 | 119 | | { |
| | 11492 | 120 | | Kind = JointConstraintRowKind3D.Linear, |
| | 11492 | 121 | | RelativeAnchorA = relativeA, |
| | 11492 | 122 | | RelativeAnchorB = relativeB |
| | 11492 | 123 | | }; |
| | 91936 | 124 | | for (int axis = 0; axis < 3; axis++) |
| | | 125 | | { |
| | 34476 | 126 | | row.Axis = axis == 0 ? Vector3d.Right : axis == 1 ? Vector3d.Up : Vector3d.Forward; |
| | | 127 | | // Frozen, unresponsive errors cannot consume the connected island's repair budget. |
| | 34476 | 128 | | if (ComputeDenominator(joint.BodyA, joint.BodyB, row) <= RowEpsilon) |
| | 259 | 129 | | error[axis] = Fixed64.Zero; |
| | | 130 | | } |
| | 11492 | 131 | | return Vector3d.TryGetMagnitude(error, out distance); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private static bool TryPrepareAnchors(Joint3D joint, out Vector3d relativeAnchorA, |
| | | 135 | | out Vector3d relativeAnchorB, out Vector3d anchorError) |
| | | 136 | | { |
| | 136893 | 137 | | relativeAnchorA = relativeAnchorB = anchorError = default; |
| | 273786 | 138 | | SolidBody bodyA = joint.BodyA, bodyB = joint.BodyB; |
| | 136893 | 139 | | return bodyA.Rotation.TryRotate(joint.LocalFrameA.Position, out Vector3d anchorOffsetA) |
| | 136893 | 140 | | && bodyB.Rotation.TryRotate(joint.LocalFrameB.Position, out Vector3d anchorOffsetB) |
| | 136893 | 141 | | && bodyA.TryGetOffsetFromCenterOfMass(new ContactAnchor(bodyA.Position3d, anchorOffsetA), out relativeAnchor |
| | 136893 | 142 | | && bodyB.TryGetOffsetFromCenterOfMass(new ContactAnchor(bodyB.Position3d, anchorOffsetB), out relativeAnchor |
| | 136893 | 143 | | && Vector3d.TrySubtractSums(bodyB.Position3d, anchorOffsetB, bodyA.Position3d, anchorOffsetA, out anchorErro |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private static int BuildRows( |
| | | 147 | | Joint3D joint, |
| | | 148 | | Span<JointConstraintRow3D> rows, |
| | | 149 | | out Fixed64 linearAnchorErrorMagnitude, |
| | | 150 | | out Fixed64 angularLimitErrorMagnitude, |
| | | 151 | | out Fixed64 motorErrorMagnitude) |
| | | 152 | | { |
| | 125395 | 153 | | int count = 0; |
| | 125395 | 154 | | angularLimitErrorMagnitude = Fixed64.Zero; |
| | 125395 | 155 | | motorErrorMagnitude = Fixed64.Zero; |
| | 125395 | 156 | | SolidBody bodyA = joint.BodyA; |
| | 125395 | 157 | | SolidBody bodyB = joint.BodyB; |
| | | 158 | | |
| | 125395 | 159 | | FixedQuaternion worldRotationA = (bodyA.Rotation * joint.LocalFrameA.Rotation).Normalized; |
| | 125395 | 160 | | FixedQuaternion worldRotationB = (bodyB.Rotation * joint.LocalFrameB.Rotation).Normalized; |
| | 125395 | 161 | | if (!TryPrepareAnchors(joint, out Vector3d relativeAnchorA, |
| | 125395 | 162 | | out Vector3d relativeAnchorB, out Vector3d anchorError)) |
| | | 163 | | { |
| | 36 | 164 | | linearAnchorErrorMagnitude = Fixed64.Zero; |
| | 36 | 165 | | GravitasLogger.Channel.Write( |
| | 36 | 166 | | DiagnosticLevel.Error, |
| | 36 | 167 | | "A 3D joint's anchors cannot be represented in response space.", |
| | 36 | 168 | | nameof(JointSolver3D)); |
| | 36 | 169 | | return 0; |
| | | 170 | | } |
| | | 171 | | |
| | 125359 | 172 | | linearAnchorErrorMagnitude = anchorError.Magnitude; |
| | | 173 | | |
| | 125359 | 174 | | AddLinearAnchorRow(rows, ref count, Vector3d.Right, anchorError, relativeAnchorA, relativeAnchorB); |
| | 125359 | 175 | | AddLinearAnchorRow(rows, ref count, Vector3d.Up, anchorError, relativeAnchorA, relativeAnchorB); |
| | 125359 | 176 | | AddLinearAnchorRow(rows, ref count, Vector3d.Forward, anchorError, relativeAnchorA, relativeAnchorB); |
| | | 177 | | |
| | 125359 | 178 | | AddAngularRows(joint, rows, ref count, worldRotationA, worldRotationB, ref angularLimitErrorMagnitude); |
| | 125359 | 179 | | AddMotorRows(joint, rows, ref count, worldRotationA, worldRotationB, out motorErrorMagnitude); |
| | 125359 | 180 | | return count; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private static void AddLinearAnchorRow( |
| | | 184 | | Span<JointConstraintRow3D> rows, |
| | | 185 | | ref int count, |
| | | 186 | | Vector3d axis, |
| | | 187 | | Vector3d anchorError, |
| | | 188 | | Vector3d relativeAnchorA, |
| | | 189 | | Vector3d relativeAnchorB) |
| | | 190 | | { |
| | 376077 | 191 | | Fixed64 error = Vector3d.Dot(anchorError, axis); |
| | 376077 | 192 | | rows[count] = new JointConstraintRow3D( |
| | 376077 | 193 | | JointConstraintRowKind3D.Linear, |
| | 376077 | 194 | | axis, |
| | 376077 | 195 | | relativeAnchorA, |
| | 376077 | 196 | | relativeAnchorB, |
| | 376077 | 197 | | error * BiasFactor, |
| | 376077 | 198 | | Fixed64.Zero, |
| | 376077 | 199 | | Fixed64.MinValue, |
| | 376077 | 200 | | Fixed64.MaxValue, |
| | 376077 | 201 | | count); |
| | 376077 | 202 | | count++; |
| | 376077 | 203 | | } |
| | | 204 | | |
| | | 205 | | private static void AddAngularRows( |
| | | 206 | | Joint3D joint, |
| | | 207 | | Span<JointConstraintRow3D> rows, |
| | | 208 | | ref int count, |
| | | 209 | | FixedQuaternion worldRotationA, |
| | | 210 | | FixedQuaternion worldRotationB, |
| | | 211 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 212 | | { |
| | 125359 | 213 | | Vector3d error = GetAngularError(worldRotationA, worldRotationB); |
| | 125359 | 214 | | switch (joint.Type) |
| | | 215 | | { |
| | | 216 | | case JointType3D.Fixed: |
| | 168 | 217 | | AddAngularErrorRows(rows, ref count, error, Fixed64.Zero, Fixed64.MaxValue); |
| | 168 | 218 | | break; |
| | | 219 | | case JointType3D.Hinge: |
| | 8856 | 220 | | AddAxisAlignmentRows(rows, ref count, worldRotationA * Vector3d.Right, worldRotationB * Vector3d.Right); |
| | 8856 | 221 | | AddHingeLimitRow(joint, rows, ref count, error, worldRotationA * Vector3d.Right, ref angularLimitErrorMa |
| | 8856 | 222 | | break; |
| | | 223 | | case JointType3D.ConeTwist: |
| | 72 | 224 | | AddConeTwistRows(joint, rows, ref count, worldRotationA, worldRotationB, ref angularLimitErrorMagnitude) |
| | | 225 | | break; |
| | | 226 | | } |
| | 72 | 227 | | } |
| | | 228 | | |
| | | 229 | | private static void AddMotorRows( |
| | | 230 | | Joint3D joint, |
| | | 231 | | Span<JointConstraintRow3D> rows, |
| | | 232 | | ref int count, |
| | | 233 | | FixedQuaternion worldRotationA, |
| | | 234 | | FixedQuaternion worldRotationB, |
| | | 235 | | out Fixed64 motorErrorMagnitude) |
| | | 236 | | { |
| | 125359 | 237 | | motorErrorMagnitude = Fixed64.Zero; |
| | 125359 | 238 | | JointMotor3D motor = joint.Motor; |
| | 125359 | 239 | | if (!motor.IsEnabled) |
| | 121839 | 240 | | return; |
| | | 241 | | |
| | 3520 | 242 | | FixedQuaternion targetWorldB = (worldRotationA * motor.TargetLocalRotation).Normalized; |
| | 3520 | 243 | | Vector3d motorError = GetAngularError(targetWorldB, worldRotationB); |
| | 3520 | 244 | | motorErrorMagnitude = motorError.Magnitude; |
| | 3520 | 245 | | AddMotorErrorRows(rows, ref count, motorError, motor.AngularDriveStrength, motor.AngularDriveDamping, motor.Maxi |
| | 3520 | 246 | | } |
| | | 247 | | |
| | | 248 | | private static void AddAxisAlignmentRows( |
| | | 249 | | Span<JointConstraintRow3D> rows, |
| | | 250 | | ref int count, |
| | | 251 | | Vector3d axisA, |
| | | 252 | | Vector3d axisB) |
| | | 253 | | { |
| | 8856 | 254 | | Vector3d cross = Vector3d.Cross(axisA.Normalized, axisB.Normalized); |
| | 8856 | 255 | | Fixed64 magnitude = cross.Magnitude; |
| | 8856 | 256 | | if (magnitude <= RowEpsilon) |
| | 358 | 257 | | return; |
| | | 258 | | |
| | 8498 | 259 | | AddAngularRow(rows, ref count, cross / magnitude, magnitude, Fixed64.Zero, Fixed64.MaxValue); |
| | 8498 | 260 | | } |
| | | 261 | | |
| | | 262 | | private static void AddHingeLimitRow( |
| | | 263 | | Joint3D joint, |
| | | 264 | | Span<JointConstraintRow3D> rows, |
| | | 265 | | ref int count, |
| | | 266 | | Vector3d angularError, |
| | | 267 | | Vector3d hingeAxis, |
| | | 268 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 269 | | { |
| | 8856 | 270 | | if (joint.Limits.Kind != JointLimitKind3D.Hinge) |
| | 8 | 271 | | return; |
| | | 272 | | |
| | 8848 | 273 | | Vector3d axis = hingeAxis.Normalized; |
| | 8848 | 274 | | Fixed64 twist = Vector3d.Dot(angularError, axis); |
| | 8848 | 275 | | Fixed64 max = joint.Limits.MaxHingeAngle; |
| | 8848 | 276 | | if (twist.Abs() <= max) |
| | 8784 | 277 | | return; |
| | | 278 | | |
| | 64 | 279 | | Fixed64 limitedError = twist > Fixed64.Zero ? twist - max : twist + max; |
| | 64 | 280 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 64 | 281 | | AddAngularRow(rows, ref count, axis, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 64 | 282 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | 64 | 283 | | } |
| | | 284 | | |
| | | 285 | | private static void AddConeTwistRows( |
| | | 286 | | Joint3D joint, |
| | | 287 | | Span<JointConstraintRow3D> rows, |
| | | 288 | | ref int count, |
| | | 289 | | FixedQuaternion worldRotationA, |
| | | 290 | | FixedQuaternion worldRotationB, |
| | | 291 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 292 | | { |
| | 72 | 293 | | Vector3d forwardA = (worldRotationA * Vector3d.Forward).Normalized; |
| | 72 | 294 | | Vector3d forwardB = (worldRotationB * Vector3d.Forward).Normalized; |
| | 72 | 295 | | if (joint.Limits.Kind != JointLimitKind3D.ConeTwist) |
| | 8 | 296 | | return; |
| | | 297 | | |
| | 64 | 298 | | Fixed64 dot = FixedMath.Clamp(Vector3d.Dot(forwardA, forwardB), -Fixed64.One, Fixed64.One); |
| | 64 | 299 | | Fixed64 swing = FixedMath.Acos(dot); |
| | 64 | 300 | | if (swing > joint.Limits.MaxConeAngle) |
| | | 301 | | { |
| | 18 | 302 | | Vector3d swingAxis = Vector3d.Cross(forwardA, forwardB); |
| | 18 | 303 | | if (swingAxis.MagnitudeSquared <= RowEpsilon) |
| | 2 | 304 | | swingAxis = Perpendicular(forwardA); |
| | | 305 | | |
| | 18 | 306 | | Fixed64 limitedError = swing - joint.Limits.MaxConeAngle; |
| | 18 | 307 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 18 | 308 | | AddAngularRow(rows, ref count, swingAxis.Normalized, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 18 | 309 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | | 310 | | } |
| | | 311 | | |
| | 64 | 312 | | Fixed64 twist = GetSignedTwistAngle(worldRotationA, worldRotationB, forwardA); |
| | 64 | 313 | | Fixed64 maxTwist = joint.Limits.MaxTwistAngle; |
| | 64 | 314 | | if (twist.Abs() > maxTwist) |
| | | 315 | | { |
| | 38 | 316 | | Fixed64 limitedError = twist > Fixed64.Zero ? twist - maxTwist : twist + maxTwist; |
| | 38 | 317 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 38 | 318 | | AddAngularRow(rows, ref count, forwardA, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 38 | 319 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | | 320 | | } |
| | 64 | 321 | | } |
| | | 322 | | |
| | | 323 | | private static void AddAngularErrorRows( |
| | | 324 | | Span<JointConstraintRow3D> rows, |
| | | 325 | | ref int count, |
| | | 326 | | Vector3d error, |
| | | 327 | | Fixed64 damping, |
| | | 328 | | Fixed64 maxImpulse) |
| | | 329 | | { |
| | 168 | 330 | | AddAngularRow(rows, ref count, Vector3d.Right, Vector3d.Dot(error, Vector3d.Right), damping, maxImpulse); |
| | 168 | 331 | | AddAngularRow(rows, ref count, Vector3d.Up, Vector3d.Dot(error, Vector3d.Up), damping, maxImpulse); |
| | 168 | 332 | | AddAngularRow(rows, ref count, Vector3d.Forward, Vector3d.Dot(error, Vector3d.Forward), damping, maxImpulse); |
| | 168 | 333 | | } |
| | | 334 | | |
| | | 335 | | private static void AddMotorErrorRows( |
| | | 336 | | Span<JointConstraintRow3D> rows, |
| | | 337 | | ref int count, |
| | | 338 | | Vector3d error, |
| | | 339 | | Fixed64 strength, |
| | | 340 | | Fixed64 damping, |
| | | 341 | | Fixed64 maxImpulse) |
| | | 342 | | { |
| | 3520 | 343 | | AddMotorRow(rows, ref count, Vector3d.Right, Vector3d.Dot(error, Vector3d.Right) * strength, damping, maxImpulse |
| | 3520 | 344 | | AddMotorRow(rows, ref count, Vector3d.Up, Vector3d.Dot(error, Vector3d.Up) * strength, damping, maxImpulse); |
| | 3520 | 345 | | AddMotorRow(rows, ref count, Vector3d.Forward, Vector3d.Dot(error, Vector3d.Forward) * strength, damping, maxImp |
| | 3520 | 346 | | } |
| | | 347 | | |
| | | 348 | | private static void AddAngularRow( |
| | | 349 | | Span<JointConstraintRow3D> rows, |
| | | 350 | | ref int count, |
| | | 351 | | Vector3d axis, |
| | | 352 | | Fixed64 error, |
| | | 353 | | Fixed64 damping, |
| | | 354 | | Fixed64 maxImpulse) |
| | | 355 | | { |
| | 9122 | 356 | | if (error.Abs() <= RowEpsilon || axis.MagnitudeSquared <= RowEpsilon) |
| | 344 | 357 | | return; |
| | | 358 | | |
| | 8778 | 359 | | rows[count] = new JointConstraintRow3D( |
| | 8778 | 360 | | JointConstraintRowKind3D.Angular, |
| | 8778 | 361 | | axis.Normalized, |
| | 8778 | 362 | | Vector3d.Zero, |
| | 8778 | 363 | | Vector3d.Zero, |
| | 8778 | 364 | | error * BiasFactor, |
| | 8778 | 365 | | damping, |
| | 8778 | 366 | | -maxImpulse, |
| | 8778 | 367 | | maxImpulse, |
| | 8778 | 368 | | count); |
| | 8778 | 369 | | count++; |
| | 8778 | 370 | | } |
| | | 371 | | |
| | | 372 | | private static void AddMotorRow( |
| | | 373 | | Span<JointConstraintRow3D> rows, |
| | | 374 | | ref int count, |
| | | 375 | | Vector3d axis, |
| | | 376 | | Fixed64 biasVelocity, |
| | | 377 | | Fixed64 damping, |
| | | 378 | | Fixed64 maxImpulse) |
| | | 379 | | { |
| | 10560 | 380 | | if (biasVelocity.Abs() <= RowEpsilon) |
| | 7424 | 381 | | return; |
| | | 382 | | |
| | 3136 | 383 | | rows[count] = new JointConstraintRow3D( |
| | 3136 | 384 | | JointConstraintRowKind3D.Motor, |
| | 3136 | 385 | | axis, |
| | 3136 | 386 | | Vector3d.Zero, |
| | 3136 | 387 | | Vector3d.Zero, |
| | 3136 | 388 | | biasVelocity, |
| | 3136 | 389 | | damping, |
| | 3136 | 390 | | -maxImpulse, |
| | 3136 | 391 | | maxImpulse, |
| | 3136 | 392 | | count); |
| | 3136 | 393 | | count++; |
| | 3136 | 394 | | } |
| | | 395 | | |
| | | 396 | | private static Vector3d GetAngularError(FixedQuaternion reference, FixedQuaternion current) |
| | | 397 | | { |
| | 128879 | 398 | | FixedQuaternion error = (current * reference.Inverse()).Normalized; |
| | 128879 | 399 | | return FixedQuaternion.QuaternionLog(error); |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | private static Fixed64 GetSignedTwistAngle( |
| | | 403 | | FixedQuaternion worldRotationA, |
| | | 404 | | FixedQuaternion worldRotationB, |
| | | 405 | | Vector3d twistAxis) |
| | | 406 | | { |
| | 64 | 407 | | FixedQuaternion relative = (worldRotationB * worldRotationA.Inverse()).Normalized; |
| | 64 | 408 | | Vector3d relativeVector = new(relative.X, relative.Y, relative.Z); |
| | 64 | 409 | | Fixed64 projectedTwist = Vector3d.Dot(relativeVector, twistAxis); |
| | 64 | 410 | | if (projectedTwist.Abs() < QuaternionTwistVectorEpsilon |
| | 64 | 411 | | && relative.W.Abs() < QuaternionTwistVectorEpsilon) |
| | | 412 | | { |
| | 2 | 413 | | return Fixed64.Zero; |
| | | 414 | | } |
| | | 415 | | |
| | 62 | 416 | | return NormalizeAngle(Fixed64.Two * FixedMath.Atan2(projectedTwist, relative.W)); |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | private static Fixed64 NormalizeAngle(Fixed64 angle) |
| | | 420 | | { |
| | 62 | 421 | | angle %= Fixed64.TwoPi; |
| | 62 | 422 | | if (angle < -Fixed64.Pi) |
| | 1 | 423 | | angle += Fixed64.TwoPi; |
| | 61 | 424 | | else if (angle >= Fixed64.Pi) |
| | 2 | 425 | | angle -= Fixed64.TwoPi; |
| | 62 | 426 | | return angle; |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | private static Vector3d Perpendicular(Vector3d axis) |
| | | 430 | | { |
| | 2 | 431 | | Vector3d candidate = Vector3d.Cross(axis, Vector3d.Up); |
| | 2 | 432 | | return candidate.MagnitudeSquared > RowEpsilon |
| | 2 | 433 | | ? candidate |
| | 2 | 434 | | : Vector3d.Cross(axis, Vector3d.Right); |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | private static Fixed64 SolveRow( |
| | | 438 | | SolidBody bodyA, |
| | | 439 | | SolidBody bodyB, |
| | | 440 | | JointConstraintRow3D row, |
| | | 441 | | out bool clampedToBounds) |
| | | 442 | | { |
| | 387973 | 443 | | clampedToBounds = false; |
| | 387973 | 444 | | Fixed64 denominator = ComputeDenominator(bodyA, bodyB, row); |
| | 387973 | 445 | | if (denominator <= Fixed64.Epsilon) |
| | 1585 | 446 | | return Fixed64.Zero; |
| | | 447 | | |
| | 386388 | 448 | | Fixed64 velocity = ComputeRelativeVelocity(bodyA, bodyB, row); |
| | 386388 | 449 | | Fixed64 lambda = -(velocity + row.BiasVelocity + velocity * row.Damping) / denominator; |
| | 386388 | 450 | | Fixed64 unclamped = row.AccumulatedImpulse + lambda; |
| | 386388 | 451 | | Fixed64 clamped = FixedMath.Clamp(unclamped, row.LowerImpulse, row.UpperImpulse); |
| | 386388 | 452 | | clampedToBounds = clamped != unclamped; |
| | 386388 | 453 | | lambda = clamped - row.AccumulatedImpulse; |
| | 386388 | 454 | | if (lambda == Fixed64.Zero) |
| | 216688 | 455 | | return Fixed64.Zero; |
| | | 456 | | |
| | 169700 | 457 | | return ApplyImpulse(bodyA, bodyB, row, lambda); |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | private static Fixed64 ComputeRelativeVelocity(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row) |
| | | 461 | | { |
| | 386388 | 462 | | if (row.Kind == JointConstraintRowKind3D.Linear) |
| | | 463 | | { |
| | 374506 | 464 | | Vector3d velocityA = bodyA.LinearVelocity + Vector3d.Cross(bodyA.AngularVelocity, row.RelativeAnchorA); |
| | 374506 | 465 | | Vector3d velocityB = bodyB.LinearVelocity + Vector3d.Cross(bodyB.AngularVelocity, row.RelativeAnchorB); |
| | 374506 | 466 | | return Vector3d.Dot(velocityB - velocityA, row.Axis); |
| | | 467 | | } |
| | | 468 | | |
| | 11882 | 469 | | return Vector3d.Dot(bodyB.AngularVelocity - bodyA.AngularVelocity, row.Axis); |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | private static Fixed64 ComputeDenominator(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row) |
| | | 473 | | { |
| | 422461 | 474 | | if (row.Kind != JointConstraintRowKind3D.Linear) |
| | | 475 | | { |
| | 11914 | 476 | | Vector3d angularA = bodyA.ApplyConstrainedInverseInertia(row.Axis); |
| | 11914 | 477 | | Vector3d angularB = bodyB.ApplyConstrainedInverseInertia(row.Axis); |
| | 11914 | 478 | | return Vector3d.Dot(row.Axis, angularA + angularB); |
| | | 479 | | } |
| | | 480 | | |
| | 410547 | 481 | | Fixed64 inverseMass = bodyA.GetConstrainedInverseMass(row.Axis) + bodyB.GetConstrainedInverseMass(row.Axis); |
| | 410547 | 482 | | Vector3d torqueA = Vector3d.Cross(row.RelativeAnchorA, row.Axis); |
| | 410547 | 483 | | Vector3d torqueB = Vector3d.Cross(row.RelativeAnchorB, row.Axis); |
| | 410547 | 484 | | Vector3d linearAngularA = bodyA.ApplyConstrainedInverseInertia(torqueA); |
| | 410547 | 485 | | Vector3d linearAngularB = bodyB.ApplyConstrainedInverseInertia(torqueB); |
| | 410547 | 486 | | return inverseMass |
| | 410547 | 487 | | + Vector3d.Dot(torqueA, linearAngularA) |
| | 410547 | 488 | | + Vector3d.Dot(torqueB, linearAngularB); |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | private static Fixed64 ApplyImpulse(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row, Fixed64 lambda) |
| | | 492 | | { |
| | 205460 | 493 | | if (lambda == Fixed64.Zero) |
| | 20119 | 494 | | return Fixed64.Zero; |
| | 185341 | 495 | | bool negative = lambda < Fixed64.Zero; |
| | 185341 | 496 | | if (!Fixed64.TryMultiplyDivide(lambda, negative ? -Fixed64.One : Fixed64.One, Fixed64.One, out Fixed64 magnitude |
| | 1 | 497 | | return Fixed64.Zero; |
| | | 498 | | |
| | | 499 | | // Limit the shared impulse, then prepare again from that exact scalar before publishing either body. |
| | 371526 | 500 | | for (int attempt = 0; attempt < 2; attempt++) |
| | | 501 | | { |
| | 185748 | 502 | | bool prepared = ContactResponseArithmetic3D.TryScale(row.Axis, lambda, out Vector3d impulse) |
| | 185748 | 503 | | & Vector3d.TrySubtract(Vector3d.Zero, impulse, out Vector3d opposite); |
| | 185748 | 504 | | prepared &= TryPrepareImpulseBody(bodyA, row, row.RelativeAnchorA, opposite, out Vector3d linearA, out Vecto |
| | 185748 | 505 | | & TryPrepareImpulseBody(bodyB, row, row.RelativeAnchorB, impulse, out Vector3d linearB, out Vector3d ang |
| | 185748 | 506 | | if (!prepared) |
| | 1 | 507 | | return Fixed64.Zero; |
| | | 508 | | |
| | 185747 | 509 | | Fixed64 limit = bodyA.Context.Environment.MaxSpeed; |
| | 185747 | 510 | | Fixed64 allowed = FixedMath.Min( |
| | 185747 | 511 | | FixedMath.Min(GetAllowedImpulseMagnitude(bodyA.LinearVelocity, linearA, limit, magnitude), |
| | 185747 | 512 | | GetAllowedImpulseMagnitude(bodyB.LinearVelocity, linearB, limit, magnitude)), |
| | 185747 | 513 | | FixedMath.Min(GetAllowedImpulseMagnitude(bodyA.AngularVelocity, angularA, limit, magnitude), |
| | 185747 | 514 | | GetAllowedImpulseMagnitude(bodyB.AngularVelocity, angularB, limit, magnitude))); |
| | 185747 | 515 | | if (allowed == magnitude) |
| | | 516 | | { |
| | 183079 | 517 | | bodyA.ApplyCollisionVelocityState(linearA, angularA); |
| | 183079 | 518 | | bodyB.ApplyCollisionVelocityState(linearB, angularB); |
| | 183079 | 519 | | return lambda; |
| | | 520 | | } |
| | 2668 | 521 | | if (allowed == Fixed64.Zero) |
| | 2245 | 522 | | return Fixed64.Zero; |
| | 423 | 523 | | magnitude = allowed; |
| | 423 | 524 | | lambda = negative ? -allowed : allowed; |
| | | 525 | | } |
| | 15 | 526 | | return Fixed64.Zero; |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | private static bool TryPrepareImpulseBody(SolidBody body, JointConstraintRow3D row, Vector3d relativeAnchor, |
| | | 530 | | Vector3d impulse, out Vector3d linear, out Vector3d angular) |
| | | 531 | | { |
| | 742992 | 532 | | Vector3d linearDelta = default, torque = impulse; |
| | 371496 | 533 | | bool prepared = true; |
| | 371496 | 534 | | if (row.Kind == JointConstraintRowKind3D.Linear) |
| | 346370 | 535 | | prepared = ContactResponseArithmetic3D.TryScale(impulse, body.GetConstrainedInverseMass(row.Axis), out linea |
| | 346370 | 536 | | & Vector3d.TryCross(relativeAnchor, impulse, out torque); |
| | 371496 | 537 | | prepared &= Fixed3x3.TryTransformDirection(body.GetConstrainedInverseInertiaTensor(), torque, out Vector3d angul |
| | 371496 | 538 | | return prepared & body.TryPrepareCollisionVelocityState(linearDelta, angularDelta, out linear, out angular); |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | private static Fixed64 GetAllowedImpulseMagnitude(Vector3d current, Vector3d prepared, Fixed64 limit, Fixed64 magnit |
| | | 542 | | { |
| | | 543 | | // Match the body's rounded speed admission so publication cannot apply a different radial cap. |
| | 742988 | 544 | | if (current == prepared || prepared.Magnitude <= limit) |
| | 740290 | 545 | | return magnitude; |
| | 2698 | 546 | | var bound = new FixedBoundSphere(Vector3d.Zero, limit); |
| | 2698 | 547 | | var segment = new FixedSegment(current, prepared); |
| | 2698 | 548 | | if (!segment.TryGetSphereIntersectionDistanceInterval(bound, magnitude, out _, out Fixed64 exit)) |
| | 423 | 549 | | return Fixed64.Zero; |
| | | 550 | | // The interval rounds half to even. Retreat one impulse LSB and recheck the materialized endpoint. |
| | 2275 | 551 | | return exit > Fixed64.Zero ? exit - Fixed64.FromRaw(1) : Fixed64.Zero; |
| | | 552 | | } |
| | | 553 | | } |