| | | 1 | | //======================================================================= |
| | | 2 | | // JointSolver2D.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 System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.Constraints; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Builds and solves planar joint rows, applying cached impulses and recording solve metrics. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class JointSolver2D |
| | | 19 | | { |
| | | 20 | | internal const int MaxRowsPerJoint = 8; |
| | | 21 | | |
| | | 22 | | private const int CacheLinearX = 0; |
| | | 23 | | private const int CacheLinearY = 1; |
| | | 24 | | private const int CacheAngular = 2; |
| | | 25 | | private const int CacheLimit = 3; |
| | | 26 | | private const int CacheMotor = 4; |
| | | 27 | | |
| | | 28 | | // Fraction of structural error targeted by the next fixed step. |
| | 1 | 29 | | private static readonly Fixed64 BiasFactor = Fixed64.FromFraction(2, 5); |
| | 1 | 30 | | private static readonly Fixed64 RowEpsilon = Fixed64.Epsilon; |
| | | 31 | | |
| | | 32 | | internal static void Solve(Joint2D joint, bool applyCachedImpulse) |
| | | 33 | | { |
| | 1742 | 34 | | Span<JointConstraintRow2D> rows = stackalloc JointConstraintRow2D[MaxRowsPerJoint]; |
| | 1742 | 35 | | int rowCount = BuildRows( |
| | 1742 | 36 | | joint, |
| | 1742 | 37 | | rows, |
| | 1742 | 38 | | out Fixed64 linearAnchorErrorMagnitude, |
| | 1742 | 39 | | out Fixed64 angularErrorMagnitude, |
| | 1742 | 40 | | out Fixed64 limitErrorMagnitude, |
| | 1742 | 41 | | out Fixed64 motorErrorMagnitude); |
| | 1742 | 42 | | if (rowCount == 0 || !TryPrepareBiasVelocities(joint, rows[..rowCount])) |
| | | 43 | | { |
| | 44 | 44 | | joint.LastSolvedRowCount = 0; |
| | 44 | 45 | | joint.LastSolveMetrics = default; |
| | 44 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 1698 | 49 | | Fixed64 incrementalImpulseMagnitude = Fixed64.Zero; |
| | 1698 | 50 | | Fixed64 motorImpulseMagnitude = Fixed64.Zero; |
| | 1698 | 51 | | int clampedRowCount = 0; |
| | 10524 | 52 | | for (int i = 0; i < rowCount; i++) |
| | | 53 | | { |
| | 3564 | 54 | | JointConstraintRow2D row = rows[i]; |
| | 3564 | 55 | | if (applyCachedImpulse) |
| | | 56 | | { |
| | 447 | 57 | | row.AccumulatedImpulse = joint.GetCachedImpulse(row.CacheIndex); |
| | 447 | 58 | | ApplyImpulse(joint.BodyA, joint.BodyB, row, row.AccumulatedImpulse); |
| | | 59 | | } |
| | | 60 | | |
| | 3564 | 61 | | Fixed64 impulse = SolveRow(joint.BodyA, joint.BodyB, row, out bool clamped); |
| | 3564 | 62 | | row.AccumulatedImpulse += impulse; |
| | 3564 | 63 | | rows[i] = row; |
| | 3564 | 64 | | joint.SetCachedImpulse(row.CacheIndex, row.AccumulatedImpulse); |
| | 3564 | 65 | | Fixed64 rowImpulseMagnitude = impulse.Abs(); |
| | 3564 | 66 | | incrementalImpulseMagnitude += rowImpulseMagnitude; |
| | 3564 | 67 | | if (row.Kind == JointConstraintRowKind2D.Motor) |
| | 80 | 68 | | motorImpulseMagnitude += rowImpulseMagnitude; |
| | 3564 | 69 | | if (clamped) |
| | 99 | 70 | | clampedRowCount++; |
| | | 71 | | } |
| | | 72 | | |
| | 1698 | 73 | | Fixed64 impulseMagnitude = Fixed64.Zero; |
| | 10524 | 74 | | for (int i = 0; i < rowCount; i++) |
| | 3564 | 75 | | impulseMagnitude += rows[i].AccumulatedImpulse.Abs(); |
| | | 76 | | |
| | 30564 | 77 | | for (int i = 0; i < MaxRowsPerJoint; i++) |
| | | 78 | | { |
| | 13584 | 79 | | bool rowPrepared = false; |
| | 72468 | 80 | | for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) |
| | | 81 | | { |
| | 26214 | 82 | | if (rows[rowIndex].CacheIndex == i) |
| | | 83 | | { |
| | 3564 | 84 | | rowPrepared = true; |
| | 3564 | 85 | | break; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | 13584 | 89 | | if (!rowPrepared) |
| | 10020 | 90 | | joint.SetCachedImpulse(i, Fixed64.Zero); |
| | | 91 | | } |
| | | 92 | | |
| | 1698 | 93 | | joint.LastSolvedRowCount = rowCount; |
| | 1698 | 94 | | joint.AccumulatedImpulseMagnitude += incrementalImpulseMagnitude; |
| | 1698 | 95 | | joint.LastSolveMetrics = new JointSolveMetrics2D( |
| | 1698 | 96 | | rowCount, |
| | 1698 | 97 | | linearAnchorErrorMagnitude, |
| | 1698 | 98 | | angularErrorMagnitude, |
| | 1698 | 99 | | limitErrorMagnitude, |
| | 1698 | 100 | | impulseMagnitude, |
| | 1698 | 101 | | incrementalImpulseMagnitude, |
| | 1698 | 102 | | motorImpulseMagnitude, |
| | 1698 | 103 | | motorErrorMagnitude, |
| | 1698 | 104 | | clampedRowCount); |
| | 1698 | 105 | | if (incrementalImpulseMagnitude > Fixed64.Zero) |
| | 608 | 106 | | joint.Context.Diagnostics.EmitJointImpulse(joint, joint.LastSolveMetrics); |
| | 1698 | 107 | | } |
| | | 108 | | |
| | | 109 | | private static bool TryPrepareBiasVelocities(Joint2D joint, Span<JointConstraintRow2D> rows) |
| | | 110 | | { |
| | 10554 | 111 | | foreach (ref JointConstraintRow2D row in rows) |
| | | 112 | | { |
| | | 113 | | // Structural error is corrected per fixed step. Motor gains already target velocity. |
| | 3576 | 114 | | if (row.Kind != JointConstraintRowKind2D.Motor && !Fixed64.TryMultiplyDivide( |
| | 3576 | 115 | | row.BiasVelocity, joint.Context.InvDeltaTime, Fixed64.One, out row.BiasVelocity)) |
| | | 116 | | { |
| | 12 | 117 | | if (ComputeDenominator(joint.BodyA, joint.BodyB, row) <= RowEpsilon) |
| | | 118 | | { |
| | 6 | 119 | | row.BiasVelocity = Fixed64.Zero; |
| | 6 | 120 | | continue; |
| | | 121 | | } |
| | 6 | 122 | | GravitasLogger.Channel.Write(DiagnosticLevel.Error, |
| | 6 | 123 | | "A 2D joint's correction velocity cannot be represented.", nameof(JointSolver2D)); |
| | 6 | 124 | | return false; |
| | | 125 | | } |
| | | 126 | | } |
| | 1698 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static int BuildRows( |
| | | 131 | | Joint2D joint, |
| | | 132 | | Span<JointConstraintRow2D> rows, |
| | | 133 | | out Fixed64 linearAnchorErrorMagnitude, |
| | | 134 | | out Fixed64 angularErrorMagnitude, |
| | | 135 | | out Fixed64 limitErrorMagnitude, |
| | | 136 | | out Fixed64 motorErrorMagnitude) |
| | | 137 | | { |
| | 1742 | 138 | | int count = 0; |
| | 1742 | 139 | | limitErrorMagnitude = Fixed64.Zero; |
| | 1742 | 140 | | motorErrorMagnitude = Fixed64.Zero; |
| | | 141 | | |
| | 1742 | 142 | | SolidBody2D bodyA = joint.BodyA; |
| | 1742 | 143 | | SolidBody2D bodyB = joint.BodyB; |
| | 1742 | 144 | | Fixed64 frameAngleA = bodyA.Rotation + joint.LocalFrameA.Angle; |
| | 1742 | 145 | | Fixed64 frameAngleB = bodyB.Rotation + joint.LocalFrameB.Angle; |
| | 1742 | 146 | | if (!Vector2d.TryRotate( |
| | 1742 | 147 | | joint.LocalFrameA.Anchor, |
| | 1742 | 148 | | bodyA.Rotation, |
| | 1742 | 149 | | out Vector2d anchorOffsetA) |
| | 1742 | 150 | | || !Vector2d.TryRotate( |
| | 1742 | 151 | | joint.LocalFrameB.Anchor, |
| | 1742 | 152 | | bodyB.Rotation, |
| | 1742 | 153 | | out Vector2d anchorOffsetB) |
| | 1742 | 154 | | || !bodyA.TryGetOffsetFromCenterOfMass( |
| | 1742 | 155 | | new ContactAnchor2D(bodyA.Position, anchorOffsetA), |
| | 1742 | 156 | | out Vector2d relativeAnchorA) |
| | 1742 | 157 | | || !bodyB.TryGetOffsetFromCenterOfMass( |
| | 1742 | 158 | | new ContactAnchor2D(bodyB.Position, anchorOffsetB), |
| | 1742 | 159 | | out Vector2d relativeAnchorB) |
| | 1742 | 160 | | || !Vector2d.TrySubtractSums( |
| | 1742 | 161 | | bodyB.Position, |
| | 1742 | 162 | | anchorOffsetB, |
| | 1742 | 163 | | bodyA.Position, |
| | 1742 | 164 | | anchorOffsetA, |
| | 1742 | 165 | | out Vector2d anchorError)) |
| | | 166 | | { |
| | 30 | 167 | | linearAnchorErrorMagnitude = Fixed64.Zero; |
| | 30 | 168 | | angularErrorMagnitude = Fixed64.Zero; |
| | 30 | 169 | | GravitasLogger.Channel.Write( |
| | 30 | 170 | | DiagnosticLevel.Error, |
| | 30 | 171 | | "A 2D joint's anchors cannot be represented in response space.", |
| | 30 | 172 | | nameof(JointSolver2D)); |
| | 30 | 173 | | return 0; |
| | | 174 | | } |
| | | 175 | | |
| | 1712 | 176 | | linearAnchorErrorMagnitude = anchorError.Magnitude; |
| | 1712 | 177 | | angularErrorMagnitude = NormalizeAngle(frameAngleB - frameAngleA).Abs(); |
| | | 178 | | |
| | 1712 | 179 | | switch (joint.Type) |
| | | 180 | | { |
| | | 181 | | case JointType2D.Distance: |
| | 272 | 182 | | AddDistanceRow(joint, rows, ref count, anchorError, bodyB.Position - bodyA.Position, frameAngleA, relati |
| | 272 | 183 | | break; |
| | | 184 | | case JointType2D.Pin: |
| | 1072 | 185 | | AddPlanarAnchorRows(rows, ref count, anchorError, relativeAnchorA, relativeAnchorB); |
| | 1072 | 186 | | AddAngularLimitRow(joint, rows, ref count, frameAngleA, frameAngleB, ref limitErrorMagnitude); |
| | 1072 | 187 | | break; |
| | | 188 | | case JointType2D.Weld: |
| | 192 | 189 | | AddPlanarAnchorRows(rows, ref count, anchorError, relativeAnchorA, relativeAnchorB); |
| | 192 | 190 | | AddAngularErrorRow(rows, ref count, NormalizeAngle(frameAngleB - frameAngleA), Fixed64.Zero, Fixed64.Max |
| | 192 | 191 | | AddAngularLimitRow(joint, rows, ref count, frameAngleA, frameAngleB, ref limitErrorMagnitude); |
| | 192 | 192 | | break; |
| | | 193 | | case JointType2D.Prismatic: |
| | 176 | 194 | | AddPrismaticRows(joint, rows, ref count, anchorError, relativeAnchorA, relativeAnchorB, frameAngleA, fra |
| | | 195 | | break; |
| | | 196 | | } |
| | | 197 | | |
| | 1712 | 198 | | AddMotorRow(joint, rows, ref count, anchorError, frameAngleA, frameAngleB, relativeAnchorA, relativeAnchorB, out |
| | 1712 | 199 | | return count; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private static void AddDistanceRow( |
| | | 203 | | Joint2D joint, |
| | | 204 | | Span<JointConstraintRow2D> rows, |
| | | 205 | | ref int count, |
| | | 206 | | Vector2d anchorError, |
| | | 207 | | Vector2d bodyDelta, |
| | | 208 | | Fixed64 frameAngleA, |
| | | 209 | | Vector2d relativeAnchorA, |
| | | 210 | | Vector2d relativeAnchorB) |
| | | 211 | | { |
| | 272 | 212 | | Fixed64 magnitudeSquared = anchorError.MagnitudeSquared; |
| | 272 | 213 | | Fixed64 targetDistance = joint.Limits.TargetDistance; |
| | 272 | 214 | | if (magnitudeSquared <= RowEpsilon && targetDistance <= RowEpsilon) |
| | 8 | 215 | | return; |
| | | 216 | | |
| | 264 | 217 | | Fixed64 distance = magnitudeSquared > RowEpsilon ? FixedMath.Sqrt(magnitudeSquared) : Fixed64.Zero; |
| | | 218 | | Vector2d axis; |
| | 264 | 219 | | if (distance > RowEpsilon) |
| | | 220 | | { |
| | 248 | 221 | | axis = anchorError / distance; |
| | | 222 | | } |
| | 16 | 223 | | else if (bodyDelta.MagnitudeSquared > RowEpsilon) |
| | | 224 | | { |
| | 8 | 225 | | axis = bodyDelta.Normalized; |
| | | 226 | | } |
| | | 227 | | else |
| | | 228 | | { |
| | 8 | 229 | | axis = Vector2d.Rotate(Vector2d.Right, frameAngleA).Normalized; |
| | | 230 | | } |
| | | 231 | | |
| | 264 | 232 | | AddLinearRow( |
| | 264 | 233 | | rows, |
| | 264 | 234 | | ref count, |
| | 264 | 235 | | axis, |
| | 264 | 236 | | relativeAnchorA, |
| | 264 | 237 | | relativeAnchorB, |
| | 264 | 238 | | (distance - targetDistance) * BiasFactor, |
| | 264 | 239 | | Fixed64.Zero, |
| | 264 | 240 | | Fixed64.MinValue, |
| | 264 | 241 | | Fixed64.MaxValue, |
| | 264 | 242 | | CacheLinearX); |
| | 264 | 243 | | } |
| | | 244 | | |
| | | 245 | | private static void AddPlanarAnchorRows( |
| | | 246 | | Span<JointConstraintRow2D> rows, |
| | | 247 | | ref int count, |
| | | 248 | | Vector2d anchorError, |
| | | 249 | | Vector2d relativeAnchorA, |
| | | 250 | | Vector2d relativeAnchorB) |
| | | 251 | | { |
| | 1264 | 252 | | AddLinearRow( |
| | 1264 | 253 | | rows, |
| | 1264 | 254 | | ref count, |
| | 1264 | 255 | | Vector2d.Right, |
| | 1264 | 256 | | relativeAnchorA, |
| | 1264 | 257 | | relativeAnchorB, |
| | 1264 | 258 | | Vector2d.Dot(anchorError, Vector2d.Right) * BiasFactor, |
| | 1264 | 259 | | Fixed64.Zero, |
| | 1264 | 260 | | Fixed64.MinValue, |
| | 1264 | 261 | | Fixed64.MaxValue, |
| | 1264 | 262 | | CacheLinearX); |
| | 1264 | 263 | | AddLinearRow( |
| | 1264 | 264 | | rows, |
| | 1264 | 265 | | ref count, |
| | 1264 | 266 | | Vector2d.Forward, |
| | 1264 | 267 | | relativeAnchorA, |
| | 1264 | 268 | | relativeAnchorB, |
| | 1264 | 269 | | Vector2d.Dot(anchorError, Vector2d.Forward) * BiasFactor, |
| | 1264 | 270 | | Fixed64.Zero, |
| | 1264 | 271 | | Fixed64.MinValue, |
| | 1264 | 272 | | Fixed64.MaxValue, |
| | 1264 | 273 | | CacheLinearY); |
| | 1264 | 274 | | } |
| | | 275 | | |
| | | 276 | | private static void AddPrismaticRows( |
| | | 277 | | Joint2D joint, |
| | | 278 | | Span<JointConstraintRow2D> rows, |
| | | 279 | | ref int count, |
| | | 280 | | Vector2d anchorError, |
| | | 281 | | Vector2d relativeAnchorA, |
| | | 282 | | Vector2d relativeAnchorB, |
| | | 283 | | Fixed64 frameAngleA, |
| | | 284 | | Fixed64 frameAngleB, |
| | | 285 | | ref Fixed64 limitErrorMagnitude) |
| | | 286 | | { |
| | 176 | 287 | | Vector2d axis = Vector2d.Rotate(Vector2d.Right, frameAngleA).Normalized; |
| | 176 | 288 | | Vector2d normal = Perpendicular(axis); |
| | 176 | 289 | | AddLinearRow( |
| | 176 | 290 | | rows, |
| | 176 | 291 | | ref count, |
| | 176 | 292 | | normal, |
| | 176 | 293 | | relativeAnchorA, |
| | 176 | 294 | | relativeAnchorB, |
| | 176 | 295 | | Vector2d.Dot(anchorError, normal) * BiasFactor, |
| | 176 | 296 | | Fixed64.Zero, |
| | 176 | 297 | | Fixed64.MinValue, |
| | 176 | 298 | | Fixed64.MaxValue, |
| | 176 | 299 | | CacheLinearX); |
| | | 300 | | |
| | 176 | 301 | | AddAngularErrorRow(rows, ref count, NormalizeAngle(frameAngleB - frameAngleA), Fixed64.Zero, Fixed64.MaxValue, C |
| | | 302 | | |
| | 176 | 303 | | if (joint.Limits.Kind != JointLimitKind2D.Slider) |
| | 48 | 304 | | return; |
| | | 305 | | |
| | 128 | 306 | | Fixed64 translation = Vector2d.Dot(anchorError, axis); |
| | 128 | 307 | | if (translation < joint.Limits.MinTranslation) |
| | | 308 | | { |
| | 40 | 309 | | Fixed64 error = translation - joint.Limits.MinTranslation; |
| | 40 | 310 | | limitErrorMagnitude += error.Abs(); |
| | 40 | 311 | | AddLinearRow( |
| | 40 | 312 | | rows, |
| | 40 | 313 | | ref count, |
| | 40 | 314 | | axis, |
| | 40 | 315 | | relativeAnchorA, |
| | 40 | 316 | | relativeAnchorB, |
| | 40 | 317 | | error * BiasFactor, |
| | 40 | 318 | | Fixed64.Zero, |
| | 40 | 319 | | Fixed64.Zero, |
| | 40 | 320 | | Fixed64.MaxValue, |
| | 40 | 321 | | CacheLimit); |
| | 40 | 322 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, error); |
| | | 323 | | } |
| | 88 | 324 | | else if (translation > joint.Limits.MaxTranslation) |
| | | 325 | | { |
| | 24 | 326 | | Fixed64 error = translation - joint.Limits.MaxTranslation; |
| | 24 | 327 | | limitErrorMagnitude += error.Abs(); |
| | 24 | 328 | | AddLinearRow( |
| | 24 | 329 | | rows, |
| | 24 | 330 | | ref count, |
| | 24 | 331 | | axis, |
| | 24 | 332 | | relativeAnchorA, |
| | 24 | 333 | | relativeAnchorB, |
| | 24 | 334 | | error * BiasFactor, |
| | 24 | 335 | | Fixed64.Zero, |
| | 24 | 336 | | Fixed64.MinValue, |
| | 24 | 337 | | Fixed64.Zero, |
| | 24 | 338 | | CacheLimit); |
| | 24 | 339 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, error); |
| | | 340 | | } |
| | 88 | 341 | | } |
| | | 342 | | |
| | | 343 | | private static void AddAngularLimitRow( |
| | | 344 | | Joint2D joint, |
| | | 345 | | Span<JointConstraintRow2D> rows, |
| | | 346 | | ref int count, |
| | | 347 | | Fixed64 frameAngleA, |
| | | 348 | | Fixed64 frameAngleB, |
| | | 349 | | ref Fixed64 limitErrorMagnitude) |
| | | 350 | | { |
| | 1264 | 351 | | if (joint.Limits.Kind != JointLimitKind2D.Angular) |
| | 1152 | 352 | | return; |
| | | 353 | | |
| | 112 | 354 | | Fixed64 angle = NormalizeAngle(frameAngleB - frameAngleA); |
| | 112 | 355 | | if (angle < joint.Limits.MinAngle) |
| | | 356 | | { |
| | 32 | 357 | | Fixed64 error = angle - joint.Limits.MinAngle; |
| | 32 | 358 | | limitErrorMagnitude += error.Abs(); |
| | 32 | 359 | | AddAngularRow( |
| | 32 | 360 | | rows, |
| | 32 | 361 | | ref count, |
| | 32 | 362 | | error * BiasFactor, |
| | 32 | 363 | | Fixed64.Zero, |
| | 32 | 364 | | Fixed64.Zero, |
| | 32 | 365 | | Fixed64.MaxValue, |
| | 32 | 366 | | CacheLimit); |
| | 32 | 367 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, error); |
| | | 368 | | } |
| | 80 | 369 | | else if (angle > joint.Limits.MaxAngle) |
| | | 370 | | { |
| | 64 | 371 | | Fixed64 error = angle - joint.Limits.MaxAngle; |
| | 64 | 372 | | limitErrorMagnitude += error.Abs(); |
| | 64 | 373 | | AddAngularRow( |
| | 64 | 374 | | rows, |
| | 64 | 375 | | ref count, |
| | 64 | 376 | | error * BiasFactor, |
| | 64 | 377 | | Fixed64.Zero, |
| | 64 | 378 | | Fixed64.MinValue, |
| | 64 | 379 | | Fixed64.Zero, |
| | 64 | 380 | | CacheLimit); |
| | 64 | 381 | | joint.Context.Diagnostics.EmitJointLimitReached(joint, error); |
| | | 382 | | } |
| | 80 | 383 | | } |
| | | 384 | | |
| | | 385 | | private static void AddMotorRow( |
| | | 386 | | Joint2D joint, |
| | | 387 | | Span<JointConstraintRow2D> rows, |
| | | 388 | | ref int count, |
| | | 389 | | Vector2d anchorError, |
| | | 390 | | Fixed64 frameAngleA, |
| | | 391 | | Fixed64 frameAngleB, |
| | | 392 | | Vector2d relativeAnchorA, |
| | | 393 | | Vector2d relativeAnchorB, |
| | | 394 | | out Fixed64 motorErrorMagnitude) |
| | | 395 | | { |
| | 1712 | 396 | | motorErrorMagnitude = Fixed64.Zero; |
| | 1712 | 397 | | JointMotor2D motor = joint.Motor; |
| | 1712 | 398 | | if (!motor.IsEnabled) |
| | 1632 | 399 | | return; |
| | | 400 | | |
| | 80 | 401 | | if (motor.Kind == JointMotorKind2D.Angular) |
| | | 402 | | { |
| | 32 | 403 | | Fixed64 error = NormalizeAngle(frameAngleB - frameAngleA - motor.Target); |
| | 32 | 404 | | motorErrorMagnitude = error.Abs(); |
| | 32 | 405 | | AddAngularRow( |
| | 32 | 406 | | rows, |
| | 32 | 407 | | ref count, |
| | 32 | 408 | | error * motor.DriveStrength, |
| | 32 | 409 | | motor.Damping, |
| | 32 | 410 | | -motor.MaximumMotorImpulse, |
| | 32 | 411 | | motor.MaximumMotorImpulse, |
| | 32 | 412 | | CacheMotor, |
| | 32 | 413 | | JointConstraintRowKind2D.Motor); |
| | 32 | 414 | | return; |
| | | 415 | | } |
| | | 416 | | |
| | 48 | 417 | | Vector2d axis = Vector2d.Rotate(Vector2d.Right, frameAngleA).Normalized; |
| | 48 | 418 | | Fixed64 errorTranslation = Vector2d.Dot(anchorError, axis) - motor.Target; |
| | 48 | 419 | | motorErrorMagnitude = errorTranslation.Abs(); |
| | 48 | 420 | | AddLinearRow( |
| | 48 | 421 | | rows, |
| | 48 | 422 | | ref count, |
| | 48 | 423 | | axis, |
| | 48 | 424 | | relativeAnchorA, |
| | 48 | 425 | | relativeAnchorB, |
| | 48 | 426 | | errorTranslation * motor.DriveStrength, |
| | 48 | 427 | | motor.Damping, |
| | 48 | 428 | | -motor.MaximumMotorImpulse, |
| | 48 | 429 | | motor.MaximumMotorImpulse, |
| | 48 | 430 | | CacheMotor, |
| | 48 | 431 | | JointConstraintRowKind2D.Motor); |
| | 48 | 432 | | } |
| | | 433 | | |
| | | 434 | | private static void AddAngularErrorRow( |
| | | 435 | | Span<JointConstraintRow2D> rows, |
| | | 436 | | ref int count, |
| | | 437 | | Fixed64 error, |
| | | 438 | | Fixed64 damping, |
| | | 439 | | Fixed64 maxImpulse, |
| | | 440 | | int cacheIndex) |
| | | 441 | | { |
| | 368 | 442 | | AddAngularRow( |
| | 368 | 443 | | rows, |
| | 368 | 444 | | ref count, |
| | 368 | 445 | | error * BiasFactor, |
| | 368 | 446 | | damping, |
| | 368 | 447 | | -maxImpulse, |
| | 368 | 448 | | maxImpulse, |
| | 368 | 449 | | cacheIndex); |
| | 368 | 450 | | } |
| | | 451 | | |
| | | 452 | | private static void AddLinearRow( |
| | | 453 | | Span<JointConstraintRow2D> rows, |
| | | 454 | | ref int count, |
| | | 455 | | Vector2d axis, |
| | | 456 | | Vector2d relativeAnchorA, |
| | | 457 | | Vector2d relativeAnchorB, |
| | | 458 | | Fixed64 biasVelocity, |
| | | 459 | | Fixed64 damping, |
| | | 460 | | Fixed64 lowerImpulse, |
| | | 461 | | Fixed64 upperImpulse, |
| | | 462 | | int cacheIndex, |
| | | 463 | | JointConstraintRowKind2D kind = JointConstraintRowKind2D.Linear) |
| | | 464 | | { |
| | 3080 | 465 | | Vector2d normalizedAxis = axis.MagnitudeSquared == Fixed64.One |
| | 3080 | 466 | | ? axis |
| | 3080 | 467 | | : axis.Normalized; |
| | 3080 | 468 | | rows[count] = new JointConstraintRow2D( |
| | 3080 | 469 | | kind, |
| | 3080 | 470 | | normalizedAxis, |
| | 3080 | 471 | | relativeAnchorA, |
| | 3080 | 472 | | relativeAnchorB, |
| | 3080 | 473 | | biasVelocity, |
| | 3080 | 474 | | damping, |
| | 3080 | 475 | | lowerImpulse, |
| | 3080 | 476 | | upperImpulse, |
| | 3080 | 477 | | cacheIndex); |
| | 3080 | 478 | | count++; |
| | 3080 | 479 | | } |
| | | 480 | | |
| | | 481 | | private static void AddAngularRow( |
| | | 482 | | Span<JointConstraintRow2D> rows, |
| | | 483 | | ref int count, |
| | | 484 | | Fixed64 biasVelocity, |
| | | 485 | | Fixed64 damping, |
| | | 486 | | Fixed64 lowerImpulse, |
| | | 487 | | Fixed64 upperImpulse, |
| | | 488 | | int cacheIndex, |
| | | 489 | | JointConstraintRowKind2D kind = JointConstraintRowKind2D.Angular) |
| | | 490 | | { |
| | 496 | 491 | | rows[count] = new JointConstraintRow2D( |
| | 496 | 492 | | kind, |
| | 496 | 493 | | Vector2d.Zero, |
| | 496 | 494 | | Vector2d.Zero, |
| | 496 | 495 | | Vector2d.Zero, |
| | 496 | 496 | | biasVelocity, |
| | 496 | 497 | | damping, |
| | 496 | 498 | | lowerImpulse, |
| | 496 | 499 | | upperImpulse, |
| | 496 | 500 | | cacheIndex); |
| | 496 | 501 | | count++; |
| | 496 | 502 | | } |
| | | 503 | | |
| | | 504 | | private static Fixed64 SolveRow( |
| | | 505 | | SolidBody2D bodyA, |
| | | 506 | | SolidBody2D bodyB, |
| | | 507 | | JointConstraintRow2D row, |
| | | 508 | | out bool clampedToBounds) |
| | | 509 | | { |
| | 3564 | 510 | | clampedToBounds = false; |
| | 3564 | 511 | | Fixed64 denominator = ComputeDenominator(bodyA, bodyB, row); |
| | 3564 | 512 | | if (denominator <= Fixed64.Epsilon) |
| | 44 | 513 | | return Fixed64.Zero; |
| | | 514 | | |
| | 3520 | 515 | | Fixed64 velocity = ComputeRelativeVelocity(bodyA, bodyB, row); |
| | 3520 | 516 | | Fixed64 lambda = -(velocity + row.BiasVelocity + velocity * row.Damping) / denominator; |
| | 3520 | 517 | | Fixed64 unclamped = row.AccumulatedImpulse + lambda; |
| | 3520 | 518 | | Fixed64 clamped = FixedMath.Clamp(unclamped, row.LowerImpulse, row.UpperImpulse); |
| | 3520 | 519 | | clampedToBounds = clamped != unclamped; |
| | 3520 | 520 | | lambda = clamped - row.AccumulatedImpulse; |
| | 3520 | 521 | | if (lambda == Fixed64.Zero) |
| | 2882 | 522 | | return Fixed64.Zero; |
| | | 523 | | |
| | 638 | 524 | | ApplyImpulse(bodyA, bodyB, row, lambda); |
| | 638 | 525 | | return lambda; |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | private static Fixed64 ComputeRelativeVelocity(SolidBody2D bodyA, SolidBody2D bodyB, JointConstraintRow2D row) |
| | | 529 | | { |
| | 3520 | 530 | | if (row.Kind == JointConstraintRowKind2D.Linear || row.Axis != Vector2d.Zero) |
| | | 531 | | { |
| | 3056 | 532 | | Vector2d velocityA = bodyA.LinearVelocity + AngularVelocityAtPoint(bodyA.AngularVelocity, row.RelativeAnchor |
| | 3056 | 533 | | Vector2d velocityB = bodyB.LinearVelocity + AngularVelocityAtPoint(bodyB.AngularVelocity, row.RelativeAnchor |
| | 3056 | 534 | | return Vector2d.Dot(velocityB - velocityA, row.Axis); |
| | | 535 | | } |
| | | 536 | | |
| | 464 | 537 | | return bodyB.AngularVelocity - bodyA.AngularVelocity; |
| | | 538 | | } |
| | | 539 | | |
| | | 540 | | private static Fixed64 ComputeDenominator(SolidBody2D bodyA, SolidBody2D bodyB, JointConstraintRow2D row) |
| | | 541 | | { |
| | 3576 | 542 | | if (row.Kind != JointConstraintRowKind2D.Linear && row.Axis == Vector2d.Zero) |
| | 496 | 543 | | return bodyA.EffectiveInverseMomentOfInertia + bodyB.EffectiveInverseMomentOfInertia; |
| | | 544 | | |
| | 3080 | 545 | | Fixed64 inverseMass = bodyA.GetConstrainedInverseMass(row.Axis) + bodyB.GetConstrainedInverseMass(row.Axis); |
| | 3080 | 546 | | Fixed64 torqueA = Cross(row.RelativeAnchorA, row.Axis); |
| | 3080 | 547 | | Fixed64 torqueB = Cross(row.RelativeAnchorB, row.Axis); |
| | 3080 | 548 | | return inverseMass |
| | 3080 | 549 | | + torqueA * torqueA * bodyA.EffectiveInverseMomentOfInertia |
| | 3080 | 550 | | + torqueB * torqueB * bodyB.EffectiveInverseMomentOfInertia; |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | private static void ApplyImpulse(SolidBody2D bodyA, SolidBody2D bodyB, JointConstraintRow2D row, Fixed64 lambda) |
| | | 554 | | { |
| | 1085 | 555 | | if (lambda == Fixed64.Zero) |
| | 396 | 556 | | return; |
| | | 557 | | |
| | 689 | 558 | | if (row.Kind != JointConstraintRowKind2D.Linear && row.Axis == Vector2d.Zero) |
| | | 559 | | { |
| | 61 | 560 | | bodyA.ApplyCollisionAngularVelocityDelta(-lambda * bodyA.EffectiveInverseMomentOfInertia); |
| | 61 | 561 | | bodyB.ApplyCollisionAngularVelocityDelta(lambda * bodyB.EffectiveInverseMomentOfInertia); |
| | 61 | 562 | | return; |
| | | 563 | | } |
| | | 564 | | |
| | 628 | 565 | | Vector2d impulse = row.Axis * lambda; |
| | 628 | 566 | | bodyA.ApplyCollisionLinearVelocityDelta(-impulse * bodyA.GetConstrainedInverseMass(row.Axis)); |
| | 628 | 567 | | bodyB.ApplyCollisionLinearVelocityDelta(impulse * bodyB.GetConstrainedInverseMass(row.Axis)); |
| | | 568 | | |
| | 628 | 569 | | Fixed64 angularA = Cross(row.RelativeAnchorA, -impulse) * bodyA.EffectiveInverseMomentOfInertia; |
| | 628 | 570 | | Fixed64 angularB = Cross(row.RelativeAnchorB, impulse) * bodyB.EffectiveInverseMomentOfInertia; |
| | 628 | 571 | | bodyA.ApplyCollisionAngularVelocityDelta(angularA); |
| | 628 | 572 | | bodyB.ApplyCollisionAngularVelocityDelta(angularB); |
| | 628 | 573 | | } |
| | | 574 | | |
| | | 575 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 576 | | private static Vector2d AngularVelocityAtPoint(Fixed64 angularVelocity, Vector2d relativeAnchor) => |
| | 6112 | 577 | | new(-angularVelocity * relativeAnchor.Y, angularVelocity * relativeAnchor.X); |
| | | 578 | | |
| | | 579 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 176 | 580 | | private static Vector2d Perpendicular(Vector2d vector) => new(-vector.Y, vector.X); |
| | | 581 | | |
| | | 582 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7416 | 583 | | private static Fixed64 Cross(Vector2d left, Vector2d right) => left.X * right.Y - left.Y * right.X; |
| | | 584 | | |
| | | 585 | | private static Fixed64 NormalizeAngle(Fixed64 angle) |
| | | 586 | | { |
| | 2224 | 587 | | angle %= Fixed64.TwoPi; |
| | 2224 | 588 | | if (angle < -Fixed64.Pi) |
| | 32 | 589 | | angle += Fixed64.TwoPi; |
| | 2192 | 590 | | else if (angle > Fixed64.Pi) |
| | 32 | 591 | | angle -= Fixed64.TwoPi; |
| | 2224 | 592 | | return angle; |
| | | 593 | | } |
| | | 594 | | } |