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