| | | 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 FixedMathSharp; |
| | | 9 | | using Gravitas.CollisionHandling; |
| | | 10 | | using System; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Constraints; |
| | | 13 | | |
| | | 14 | | internal static class JointSolver3D |
| | | 15 | | { |
| | | 16 | | internal const int MaxRowsPerJoint = 12; |
| | 1 | 17 | | private static readonly Fixed64 BiasFactor = Fixed64.One / (Fixed64)5; |
| | 1 | 18 | | private static readonly Fixed64 RowEpsilon = Fixed64.Epsilon; |
| | | 19 | | // 4,096 raw Q32.32 units, approximately 9.536743e-7 radians. |
| | 1 | 20 | | private static readonly Fixed64 QuaternionTwistVectorEpsilon = Fixed64.FromRaw(4_096); |
| | | 21 | | |
| | | 22 | | internal static void Solve(Joint3D joint, bool applyCachedImpulse) |
| | | 23 | | { |
| | 49730 | 24 | | Span<JointConstraintRow3D> rows = stackalloc JointConstraintRow3D[MaxRowsPerJoint]; |
| | 49730 | 25 | | int rowCount = BuildRows( |
| | 49730 | 26 | | joint, |
| | 49730 | 27 | | rows, |
| | 49730 | 28 | | out Fixed64 linearAnchorErrorMagnitude, |
| | 49730 | 29 | | out Fixed64 angularLimitErrorMagnitude, |
| | 49730 | 30 | | out Fixed64 motorErrorMagnitude); |
| | | 31 | | |
| | 49730 | 32 | | Fixed64 incrementalImpulseMagnitude = Fixed64.Zero; |
| | 49730 | 33 | | Fixed64 motorImpulseMagnitude = Fixed64.Zero; |
| | 49730 | 34 | | int clampedRowCount = 0; |
| | 421404 | 35 | | for (int i = 0; i < rowCount; i++) |
| | | 36 | | { |
| | 160972 | 37 | | JointConstraintRow3D row = rows[i]; |
| | 160972 | 38 | | if (applyCachedImpulse) |
| | | 39 | | { |
| | 16287 | 40 | | row.AccumulatedImpulse = joint.GetCachedImpulse(row.CacheIndex); |
| | 16287 | 41 | | ApplyImpulse(joint.BodyA, joint.BodyB, row, row.AccumulatedImpulse); |
| | | 42 | | } |
| | | 43 | | |
| | 160972 | 44 | | Fixed64 impulse = SolveRow(joint.BodyA, joint.BodyB, row, out bool clamped); |
| | 160972 | 45 | | row.AccumulatedImpulse += impulse; |
| | 160972 | 46 | | rows[i] = row; |
| | 160972 | 47 | | joint.SetCachedImpulse(row.CacheIndex, row.AccumulatedImpulse); |
| | 160972 | 48 | | Fixed64 rowImpulseMagnitude = impulse.Abs(); |
| | 160972 | 49 | | incrementalImpulseMagnitude += rowImpulseMagnitude; |
| | 160972 | 50 | | if (row.Kind == JointConstraintRowKind3D.Motor) |
| | 3096 | 51 | | motorImpulseMagnitude += rowImpulseMagnitude; |
| | 160972 | 52 | | if (clamped) |
| | 1180 | 53 | | clampedRowCount++; |
| | | 54 | | } |
| | | 55 | | |
| | 49730 | 56 | | Fixed64 impulseMagnitude = Fixed64.Zero; |
| | 421404 | 57 | | for (int i = 0; i < rowCount; i++) |
| | 160972 | 58 | | impulseMagnitude += rows[i].AccumulatedImpulse.Abs(); |
| | | 59 | | |
| | 971036 | 60 | | for (int i = rowCount; i < MaxRowsPerJoint; i++) |
| | 435788 | 61 | | joint.SetCachedImpulse(i, Fixed64.Zero); |
| | | 62 | | |
| | 49730 | 63 | | joint.LastSolvedRowCount = rowCount; |
| | 49730 | 64 | | joint.AccumulatedImpulseMagnitude += incrementalImpulseMagnitude; |
| | 49730 | 65 | | joint.LastSolveMetrics = new JointSolveMetrics3D( |
| | 49730 | 66 | | rowCount, |
| | 49730 | 67 | | linearAnchorErrorMagnitude, |
| | 49730 | 68 | | angularLimitErrorMagnitude, |
| | 49730 | 69 | | impulseMagnitude, |
| | 49730 | 70 | | incrementalImpulseMagnitude, |
| | 49730 | 71 | | motorImpulseMagnitude, |
| | 49730 | 72 | | motorErrorMagnitude, |
| | 49730 | 73 | | clampedRowCount); |
| | 49730 | 74 | | if (incrementalImpulseMagnitude > Fixed64.Zero) |
| | 48069 | 75 | | joint.Context.Diagnostics.EmitJointImpulse(joint, joint.LastSolveMetrics); |
| | 49730 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static int BuildRows( |
| | | 79 | | Joint3D joint, |
| | | 80 | | Span<JointConstraintRow3D> rows, |
| | | 81 | | out Fixed64 linearAnchorErrorMagnitude, |
| | | 82 | | out Fixed64 angularLimitErrorMagnitude, |
| | | 83 | | out Fixed64 motorErrorMagnitude) |
| | | 84 | | { |
| | 49730 | 85 | | int count = 0; |
| | 49730 | 86 | | angularLimitErrorMagnitude = Fixed64.Zero; |
| | 49730 | 87 | | motorErrorMagnitude = Fixed64.Zero; |
| | 49730 | 88 | | SolidBody bodyA = joint.BodyA; |
| | 49730 | 89 | | SolidBody bodyB = joint.BodyB; |
| | | 90 | | |
| | 49730 | 91 | | FixedQuaternion worldRotationA = (bodyA.Rotation * joint.LocalFrameA.Rotation).Normalized; |
| | 49730 | 92 | | FixedQuaternion worldRotationB = (bodyB.Rotation * joint.LocalFrameB.Rotation).Normalized; |
| | 49730 | 93 | | if (!bodyA.Rotation.TryRotate( |
| | 49730 | 94 | | joint.LocalFrameA.Position, |
| | 49730 | 95 | | out Vector3d anchorOffsetA) |
| | 49730 | 96 | | || !bodyB.Rotation.TryRotate( |
| | 49730 | 97 | | joint.LocalFrameB.Position, |
| | 49730 | 98 | | out Vector3d anchorOffsetB) |
| | 49730 | 99 | | || !bodyA.TryGetOffsetFromCenterOfMass( |
| | 49730 | 100 | | new ContactAnchor(bodyA.Position3d, anchorOffsetA), |
| | 49730 | 101 | | out Vector3d relativeAnchorA) |
| | 49730 | 102 | | || !bodyB.TryGetOffsetFromCenterOfMass( |
| | 49730 | 103 | | new ContactAnchor(bodyB.Position3d, anchorOffsetB), |
| | 49730 | 104 | | out Vector3d relativeAnchorB) |
| | 49730 | 105 | | || !Vector3d.TrySubtractSums( |
| | 49730 | 106 | | bodyB.Position3d, |
| | 49730 | 107 | | anchorOffsetB, |
| | 49730 | 108 | | bodyA.Position3d, |
| | 49730 | 109 | | anchorOffsetA, |
| | 49730 | 110 | | out Vector3d anchorError)) |
| | | 111 | | { |
| | 30 | 112 | | linearAnchorErrorMagnitude = Fixed64.Zero; |
| | 30 | 113 | | GravitasLogger.Channel.Write( |
| | 30 | 114 | | DiagnosticLevel.Error, |
| | 30 | 115 | | "A 3D joint's anchors cannot be represented in response space.", |
| | 30 | 116 | | nameof(JointSolver3D)); |
| | 30 | 117 | | return 0; |
| | | 118 | | } |
| | | 119 | | |
| | 49700 | 120 | | linearAnchorErrorMagnitude = anchorError.Magnitude; |
| | | 121 | | |
| | 49700 | 122 | | AddLinearAnchorRow(rows, ref count, Vector3d.Right, anchorError, relativeAnchorA, relativeAnchorB); |
| | 49700 | 123 | | AddLinearAnchorRow(rows, ref count, Vector3d.Up, anchorError, relativeAnchorA, relativeAnchorB); |
| | 49700 | 124 | | AddLinearAnchorRow(rows, ref count, Vector3d.Forward, anchorError, relativeAnchorA, relativeAnchorB); |
| | | 125 | | |
| | 49700 | 126 | | AddAngularRows(joint, rows, ref count, worldRotationA, worldRotationB, ref angularLimitErrorMagnitude); |
| | 49700 | 127 | | AddMotorRows(joint, rows, ref count, worldRotationA, worldRotationB, out motorErrorMagnitude); |
| | 49700 | 128 | | return count; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static void AddLinearAnchorRow( |
| | | 132 | | Span<JointConstraintRow3D> rows, |
| | | 133 | | ref int count, |
| | | 134 | | Vector3d axis, |
| | | 135 | | Vector3d anchorError, |
| | | 136 | | Vector3d relativeAnchorA, |
| | | 137 | | Vector3d relativeAnchorB) |
| | | 138 | | { |
| | 149100 | 139 | | Fixed64 error = Vector3d.Dot(anchorError, axis); |
| | 149100 | 140 | | rows[count] = new JointConstraintRow3D( |
| | 149100 | 141 | | JointConstraintRowKind3D.Linear, |
| | 149100 | 142 | | axis, |
| | 149100 | 143 | | relativeAnchorA, |
| | 149100 | 144 | | relativeAnchorB, |
| | 149100 | 145 | | error * BiasFactor, |
| | 149100 | 146 | | Fixed64.Zero, |
| | 149100 | 147 | | Fixed64.MinValue, |
| | 149100 | 148 | | Fixed64.MaxValue, |
| | 149100 | 149 | | count); |
| | 149100 | 150 | | count++; |
| | 149100 | 151 | | } |
| | | 152 | | |
| | | 153 | | private static void AddAngularRows( |
| | | 154 | | Joint3D joint, |
| | | 155 | | Span<JointConstraintRow3D> rows, |
| | | 156 | | ref int count, |
| | | 157 | | FixedQuaternion worldRotationA, |
| | | 158 | | FixedQuaternion worldRotationB, |
| | | 159 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 160 | | { |
| | 49700 | 161 | | Vector3d error = GetAngularError(worldRotationA, worldRotationB); |
| | 49700 | 162 | | switch (joint.Type) |
| | | 163 | | { |
| | | 164 | | case JointType3D.Fixed: |
| | 168 | 165 | | AddAngularErrorRows(rows, ref count, error, Fixed64.Zero, Fixed64.MaxValue); |
| | 168 | 166 | | break; |
| | | 167 | | case JointType3D.Hinge: |
| | 8824 | 168 | | AddAxisAlignmentRows(rows, ref count, worldRotationA * Vector3d.Right, worldRotationB * Vector3d.Right); |
| | 8824 | 169 | | AddHingeLimitRow(joint, rows, ref count, error, worldRotationA * Vector3d.Right, ref angularLimitErrorMa |
| | 8824 | 170 | | break; |
| | | 171 | | case JointType3D.ConeTwist: |
| | 72 | 172 | | AddConeTwistRows(joint, rows, ref count, worldRotationA, worldRotationB, ref angularLimitErrorMagnitude) |
| | | 173 | | break; |
| | | 174 | | } |
| | 72 | 175 | | } |
| | | 176 | | |
| | | 177 | | private static void AddMotorRows( |
| | | 178 | | Joint3D joint, |
| | | 179 | | Span<JointConstraintRow3D> rows, |
| | | 180 | | ref int count, |
| | | 181 | | FixedQuaternion worldRotationA, |
| | | 182 | | FixedQuaternion worldRotationB, |
| | | 183 | | out Fixed64 motorErrorMagnitude) |
| | | 184 | | { |
| | 49700 | 185 | | motorErrorMagnitude = Fixed64.Zero; |
| | 49700 | 186 | | JointMotor3D motor = joint.Motor; |
| | 49700 | 187 | | if (!motor.IsEnabled) |
| | 46180 | 188 | | return; |
| | | 189 | | |
| | 3520 | 190 | | FixedQuaternion targetWorldB = (worldRotationA * motor.TargetLocalRotation).Normalized; |
| | 3520 | 191 | | Vector3d motorError = GetAngularError(targetWorldB, worldRotationB); |
| | 3520 | 192 | | motorErrorMagnitude = motorError.Magnitude; |
| | 3520 | 193 | | AddMotorErrorRows(rows, ref count, motorError, motor.AngularDriveStrength, motor.AngularDriveDamping, motor.Maxi |
| | 3520 | 194 | | } |
| | | 195 | | |
| | | 196 | | private static void AddAxisAlignmentRows( |
| | | 197 | | Span<JointConstraintRow3D> rows, |
| | | 198 | | ref int count, |
| | | 199 | | Vector3d axisA, |
| | | 200 | | Vector3d axisB) |
| | | 201 | | { |
| | 8824 | 202 | | Vector3d cross = Vector3d.Cross(axisA.Normalized, axisB.Normalized); |
| | 8824 | 203 | | Fixed64 magnitude = cross.Magnitude; |
| | 8824 | 204 | | if (magnitude <= RowEpsilon) |
| | 296 | 205 | | return; |
| | | 206 | | |
| | 8528 | 207 | | AddAngularRow(rows, ref count, cross / magnitude, magnitude, Fixed64.Zero, Fixed64.MaxValue); |
| | 8528 | 208 | | } |
| | | 209 | | |
| | | 210 | | private static void AddHingeLimitRow( |
| | | 211 | | Joint3D joint, |
| | | 212 | | Span<JointConstraintRow3D> rows, |
| | | 213 | | ref int count, |
| | | 214 | | Vector3d angularError, |
| | | 215 | | Vector3d hingeAxis, |
| | | 216 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 217 | | { |
| | 8824 | 218 | | if (joint.Limits.Kind != JointLimitKind3D.Hinge) |
| | 8 | 219 | | return; |
| | | 220 | | |
| | 8816 | 221 | | Vector3d axis = hingeAxis.Normalized; |
| | 8816 | 222 | | Fixed64 twist = Vector3d.Dot(angularError, axis); |
| | 8816 | 223 | | Fixed64 max = joint.Limits.MaxHingeAngle; |
| | 8816 | 224 | | if (twist.Abs() <= max) |
| | 8784 | 225 | | return; |
| | | 226 | | |
| | 32 | 227 | | Fixed64 limitedError = twist > Fixed64.Zero ? twist - max : twist + max; |
| | 32 | 228 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 32 | 229 | | AddAngularRow(rows, ref count, axis, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 32 | 230 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | 32 | 231 | | } |
| | | 232 | | |
| | | 233 | | private static void AddConeTwistRows( |
| | | 234 | | Joint3D joint, |
| | | 235 | | Span<JointConstraintRow3D> rows, |
| | | 236 | | ref int count, |
| | | 237 | | FixedQuaternion worldRotationA, |
| | | 238 | | FixedQuaternion worldRotationB, |
| | | 239 | | ref Fixed64 angularLimitErrorMagnitude) |
| | | 240 | | { |
| | 72 | 241 | | Vector3d forwardA = (worldRotationA * Vector3d.Forward).Normalized; |
| | 72 | 242 | | Vector3d forwardB = (worldRotationB * Vector3d.Forward).Normalized; |
| | 72 | 243 | | if (joint.Limits.Kind != JointLimitKind3D.ConeTwist) |
| | 8 | 244 | | return; |
| | | 245 | | |
| | 64 | 246 | | Fixed64 dot = FixedMath.Clamp(Vector3d.Dot(forwardA, forwardB), -Fixed64.One, Fixed64.One); |
| | 64 | 247 | | Fixed64 swing = FixedMath.Acos(dot); |
| | 64 | 248 | | if (swing > joint.Limits.MaxConeAngle) |
| | | 249 | | { |
| | 18 | 250 | | Vector3d swingAxis = Vector3d.Cross(forwardA, forwardB); |
| | 18 | 251 | | if (swingAxis.MagnitudeSquared <= RowEpsilon) |
| | 2 | 252 | | swingAxis = Perpendicular(forwardA); |
| | | 253 | | |
| | 18 | 254 | | Fixed64 limitedError = swing - joint.Limits.MaxConeAngle; |
| | 18 | 255 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 18 | 256 | | AddAngularRow(rows, ref count, swingAxis.Normalized, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 18 | 257 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | | 258 | | } |
| | | 259 | | |
| | 64 | 260 | | Fixed64 twist = GetSignedTwistAngle(worldRotationA, worldRotationB, forwardA); |
| | 64 | 261 | | Fixed64 maxTwist = joint.Limits.MaxTwistAngle; |
| | 64 | 262 | | if (twist.Abs() > maxTwist) |
| | | 263 | | { |
| | 38 | 264 | | Fixed64 limitedError = twist > Fixed64.Zero ? twist - maxTwist : twist + maxTwist; |
| | 38 | 265 | | angularLimitErrorMagnitude += limitedError.Abs(); |
| | 38 | 266 | | AddAngularRow(rows, ref count, forwardA, limitedError, Fixed64.Zero, Fixed64.MaxValue); |
| | 38 | 267 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, limitedError); |
| | | 268 | | } |
| | 64 | 269 | | } |
| | | 270 | | |
| | | 271 | | private static void AddAngularErrorRows( |
| | | 272 | | Span<JointConstraintRow3D> rows, |
| | | 273 | | ref int count, |
| | | 274 | | Vector3d error, |
| | | 275 | | Fixed64 damping, |
| | | 276 | | Fixed64 maxImpulse) |
| | | 277 | | { |
| | 168 | 278 | | AddAngularRow(rows, ref count, Vector3d.Right, Vector3d.Dot(error, Vector3d.Right), damping, maxImpulse); |
| | 168 | 279 | | AddAngularRow(rows, ref count, Vector3d.Up, Vector3d.Dot(error, Vector3d.Up), damping, maxImpulse); |
| | 168 | 280 | | AddAngularRow(rows, ref count, Vector3d.Forward, Vector3d.Dot(error, Vector3d.Forward), damping, maxImpulse); |
| | 168 | 281 | | } |
| | | 282 | | |
| | | 283 | | private static void AddMotorErrorRows( |
| | | 284 | | Span<JointConstraintRow3D> rows, |
| | | 285 | | ref int count, |
| | | 286 | | Vector3d error, |
| | | 287 | | Fixed64 strength, |
| | | 288 | | Fixed64 damping, |
| | | 289 | | Fixed64 maxImpulse) |
| | | 290 | | { |
| | 3520 | 291 | | AddMotorRow(rows, ref count, Vector3d.Right, Vector3d.Dot(error, Vector3d.Right) * strength, damping, maxImpulse |
| | 3520 | 292 | | AddMotorRow(rows, ref count, Vector3d.Up, Vector3d.Dot(error, Vector3d.Up) * strength, damping, maxImpulse); |
| | 3520 | 293 | | AddMotorRow(rows, ref count, Vector3d.Forward, Vector3d.Dot(error, Vector3d.Forward) * strength, damping, maxImp |
| | 3520 | 294 | | } |
| | | 295 | | |
| | | 296 | | private static void AddAngularRow( |
| | | 297 | | Span<JointConstraintRow3D> rows, |
| | | 298 | | ref int count, |
| | | 299 | | Vector3d axis, |
| | | 300 | | Fixed64 error, |
| | | 301 | | Fixed64 damping, |
| | | 302 | | Fixed64 maxImpulse) |
| | | 303 | | { |
| | 9120 | 304 | | if (error.Abs() <= RowEpsilon || axis.MagnitudeSquared <= RowEpsilon) |
| | 344 | 305 | | return; |
| | | 306 | | |
| | 8776 | 307 | | rows[count] = new JointConstraintRow3D( |
| | 8776 | 308 | | JointConstraintRowKind3D.Angular, |
| | 8776 | 309 | | axis.Normalized, |
| | 8776 | 310 | | Vector3d.Zero, |
| | 8776 | 311 | | Vector3d.Zero, |
| | 8776 | 312 | | error * BiasFactor, |
| | 8776 | 313 | | damping, |
| | 8776 | 314 | | -maxImpulse, |
| | 8776 | 315 | | maxImpulse, |
| | 8776 | 316 | | count); |
| | 8776 | 317 | | count++; |
| | 8776 | 318 | | } |
| | | 319 | | |
| | | 320 | | private static void AddMotorRow( |
| | | 321 | | Span<JointConstraintRow3D> rows, |
| | | 322 | | ref int count, |
| | | 323 | | Vector3d axis, |
| | | 324 | | Fixed64 biasVelocity, |
| | | 325 | | Fixed64 damping, |
| | | 326 | | Fixed64 maxImpulse) |
| | | 327 | | { |
| | 10560 | 328 | | if (biasVelocity.Abs() <= RowEpsilon) |
| | 7464 | 329 | | return; |
| | | 330 | | |
| | 3096 | 331 | | rows[count] = new JointConstraintRow3D( |
| | 3096 | 332 | | JointConstraintRowKind3D.Motor, |
| | 3096 | 333 | | axis, |
| | 3096 | 334 | | Vector3d.Zero, |
| | 3096 | 335 | | Vector3d.Zero, |
| | 3096 | 336 | | biasVelocity, |
| | 3096 | 337 | | damping, |
| | 3096 | 338 | | -maxImpulse, |
| | 3096 | 339 | | maxImpulse, |
| | 3096 | 340 | | count); |
| | 3096 | 341 | | count++; |
| | 3096 | 342 | | } |
| | | 343 | | |
| | | 344 | | private static Vector3d GetAngularError(FixedQuaternion reference, FixedQuaternion current) |
| | | 345 | | { |
| | 53220 | 346 | | FixedQuaternion error = (current * reference.Inverse()).Normalized; |
| | 53220 | 347 | | return FixedQuaternion.QuaternionLog(error); |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | private static Fixed64 GetSignedTwistAngle( |
| | | 351 | | FixedQuaternion worldRotationA, |
| | | 352 | | FixedQuaternion worldRotationB, |
| | | 353 | | Vector3d twistAxis) |
| | | 354 | | { |
| | 64 | 355 | | FixedQuaternion relative = (worldRotationB * worldRotationA.Inverse()).Normalized; |
| | 64 | 356 | | Vector3d relativeVector = new(relative.X, relative.Y, relative.Z); |
| | 64 | 357 | | Fixed64 projectedTwist = Vector3d.Dot(relativeVector, twistAxis); |
| | 64 | 358 | | if (projectedTwist.Abs() < QuaternionTwistVectorEpsilon |
| | 64 | 359 | | && relative.W.Abs() < QuaternionTwistVectorEpsilon) |
| | | 360 | | { |
| | 2 | 361 | | return Fixed64.Zero; |
| | | 362 | | } |
| | | 363 | | |
| | 62 | 364 | | return NormalizeAngle(Fixed64.Two * FixedMath.Atan2(projectedTwist, relative.W)); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | private static Fixed64 NormalizeAngle(Fixed64 angle) |
| | | 368 | | { |
| | 62 | 369 | | angle %= Fixed64.TwoPi; |
| | 62 | 370 | | if (angle < -Fixed64.Pi) |
| | 1 | 371 | | angle += Fixed64.TwoPi; |
| | 61 | 372 | | else if (angle >= Fixed64.Pi) |
| | 2 | 373 | | angle -= Fixed64.TwoPi; |
| | 62 | 374 | | return angle; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | private static Vector3d Perpendicular(Vector3d axis) |
| | | 378 | | { |
| | 2 | 379 | | Vector3d candidate = Vector3d.Cross(axis, Vector3d.Up); |
| | 2 | 380 | | return candidate.MagnitudeSquared > RowEpsilon |
| | 2 | 381 | | ? candidate |
| | 2 | 382 | | : Vector3d.Cross(axis, Vector3d.Right); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | private static Fixed64 SolveRow( |
| | | 386 | | SolidBody bodyA, |
| | | 387 | | SolidBody bodyB, |
| | | 388 | | JointConstraintRow3D row, |
| | | 389 | | out bool clampedToBounds) |
| | | 390 | | { |
| | 160972 | 391 | | clampedToBounds = false; |
| | 160972 | 392 | | Fixed64 denominator = ComputeDenominator(bodyA, bodyB, row); |
| | 160972 | 393 | | if (denominator <= Fixed64.Epsilon) |
| | 32 | 394 | | return Fixed64.Zero; |
| | | 395 | | |
| | 160940 | 396 | | Fixed64 velocity = ComputeRelativeVelocity(bodyA, bodyB, row); |
| | 160940 | 397 | | Fixed64 lambda = -(velocity + row.BiasVelocity + velocity * row.Damping) / denominator; |
| | 160940 | 398 | | Fixed64 unclamped = row.AccumulatedImpulse + lambda; |
| | 160940 | 399 | | Fixed64 clamped = FixedMath.Clamp(unclamped, row.LowerImpulse, row.UpperImpulse); |
| | 160940 | 400 | | clampedToBounds = clamped != unclamped; |
| | 160940 | 401 | | lambda = clamped - row.AccumulatedImpulse; |
| | 160940 | 402 | | if (lambda == Fixed64.Zero) |
| | 45639 | 403 | | return Fixed64.Zero; |
| | | 404 | | |
| | 115301 | 405 | | ApplyImpulse(bodyA, bodyB, row, lambda); |
| | 115301 | 406 | | return lambda; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | private static Fixed64 ComputeRelativeVelocity(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row) |
| | | 410 | | { |
| | 160940 | 411 | | if (row.Kind == JointConstraintRowKind3D.Linear) |
| | | 412 | | { |
| | 149100 | 413 | | Vector3d velocityA = bodyA.LinearVelocity + Vector3d.Cross(bodyA.AngularVelocity, row.RelativeAnchorA); |
| | 149100 | 414 | | Vector3d velocityB = bodyB.LinearVelocity + Vector3d.Cross(bodyB.AngularVelocity, row.RelativeAnchorB); |
| | 149100 | 415 | | return Vector3d.Dot(velocityB - velocityA, row.Axis); |
| | | 416 | | } |
| | | 417 | | |
| | 11840 | 418 | | return Vector3d.Dot(bodyB.AngularVelocity - bodyA.AngularVelocity, row.Axis); |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | private static Fixed64 ComputeDenominator(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row) |
| | | 422 | | { |
| | 160972 | 423 | | if (row.Kind != JointConstraintRowKind3D.Linear) |
| | | 424 | | { |
| | 11872 | 425 | | Vector3d angularA = bodyA.ApplyConstrainedInverseInertia(row.Axis); |
| | 11872 | 426 | | Vector3d angularB = bodyB.ApplyConstrainedInverseInertia(row.Axis); |
| | 11872 | 427 | | return Vector3d.Dot(row.Axis, angularA + angularB); |
| | | 428 | | } |
| | | 429 | | |
| | 149100 | 430 | | Fixed64 inverseMass = bodyA.GetConstrainedInverseMass(row.Axis) + bodyB.GetConstrainedInverseMass(row.Axis); |
| | 149100 | 431 | | Vector3d torqueA = Vector3d.Cross(row.RelativeAnchorA, row.Axis); |
| | 149100 | 432 | | Vector3d torqueB = Vector3d.Cross(row.RelativeAnchorB, row.Axis); |
| | 149100 | 433 | | Vector3d linearAngularA = bodyA.ApplyConstrainedInverseInertia(torqueA); |
| | 149100 | 434 | | Vector3d linearAngularB = bodyB.ApplyConstrainedInverseInertia(torqueB); |
| | 149100 | 435 | | return inverseMass |
| | 149100 | 436 | | + Vector3d.Dot(torqueA, linearAngularA) |
| | 149100 | 437 | | + Vector3d.Dot(torqueB, linearAngularB); |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | private static void ApplyImpulse(SolidBody bodyA, SolidBody bodyB, JointConstraintRow3D row, Fixed64 lambda) |
| | | 441 | | { |
| | 131588 | 442 | | if (lambda == Fixed64.Zero) |
| | 5145 | 443 | | return; |
| | | 444 | | |
| | 126443 | 445 | | Vector3d impulse = row.Axis * lambda; |
| | 126443 | 446 | | if (row.Kind == JointConstraintRowKind3D.Linear) |
| | | 447 | | { |
| | 113998 | 448 | | bodyA.ApplyCollisionLinearVelocityDelta(-impulse * bodyA.GetConstrainedInverseMass(row.Axis)); |
| | 113998 | 449 | | bodyB.ApplyCollisionLinearVelocityDelta(impulse * bodyB.GetConstrainedInverseMass(row.Axis)); |
| | | 450 | | |
| | 113998 | 451 | | Vector3d angularImpulseA = bodyA.ApplyConstrainedInverseInertia(Vector3d.Cross(row.RelativeAnchorA, -impulse |
| | 113998 | 452 | | Vector3d angularImpulseB = bodyB.ApplyConstrainedInverseInertia(Vector3d.Cross(row.RelativeAnchorB, impulse) |
| | 113998 | 453 | | bodyA.ApplyCollisionAngularVelocityDelta(angularImpulseA); |
| | 113998 | 454 | | bodyB.ApplyCollisionAngularVelocityDelta(angularImpulseB); |
| | 113998 | 455 | | return; |
| | | 456 | | } |
| | | 457 | | |
| | 12445 | 458 | | bodyA.ApplyCollisionAngularVelocityDelta(bodyA.ApplyConstrainedInverseInertia(-impulse)); |
| | 12445 | 459 | | bodyB.ApplyCollisionAngularVelocityDelta(bodyB.ApplyConstrainedInverseInertia(impulse)); |
| | 12445 | 460 | | } |
| | | 461 | | } |