| | | 1 | | //======================================================================= |
| | | 2 | | // JointMotor2D.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 | | |
| | | 10 | | namespace Gravitas.Constraints; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Deterministic scalar target-drive payload for a pure 2D joint. |
| | | 14 | | /// </summary> |
| | | 15 | | public readonly struct JointMotor2D |
| | | 16 | | { |
| | | 17 | | private JointMotor2D( |
| | | 18 | | JointMotorKind2D kind, |
| | | 19 | | Fixed64 target, |
| | | 20 | | Fixed64 driveStrength, |
| | | 21 | | Fixed64 damping, |
| | | 22 | | Fixed64 maximumMotorImpulse) |
| | | 23 | | { |
| | 19 | 24 | | Kind = kind; |
| | 19 | 25 | | Target = target; |
| | 19 | 26 | | DriveStrength = driveStrength; |
| | 19 | 27 | | Damping = damping; |
| | 19 | 28 | | MaximumMotorImpulse = maximumMotorImpulse; |
| | 19 | 29 | | Validate(); |
| | 19 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets a disabled motor payload. |
| | | 34 | | /// </summary> |
| | 203 | 35 | | public static JointMotor2D Disabled => default; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the active motor family. |
| | | 39 | | /// </summary> |
| | | 40 | | public JointMotorKind2D Kind { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the target angle or slider translation for the active motor. |
| | | 44 | | /// </summary> |
| | | 45 | | public Fixed64 Target { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the deterministic spring-like drive strength. |
| | | 49 | | /// </summary> |
| | | 50 | | public Fixed64 DriveStrength { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets damping applied against the solved relative scalar velocity. |
| | | 54 | | /// </summary> |
| | | 55 | | public Fixed64 Damping { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the maximum absolute impulse this motor may apply per row and iteration. |
| | | 59 | | /// </summary> |
| | | 60 | | public Fixed64 MaximumMotorImpulse { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets whether this motor can emit solver rows. |
| | | 64 | | /// </summary> |
| | | 65 | | public bool IsEnabled => |
| | 1510 | 66 | | Kind != JointMotorKind2D.Disabled |
| | 1510 | 67 | | && DriveStrength > Fixed64.Zero |
| | 1510 | 68 | | && MaximumMotorImpulse > Fixed64.Zero; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Creates an angular motor that drives scalar relative yaw toward <paramref name="targetAngle"/>. |
| | | 72 | | /// </summary> |
| | | 73 | | public static JointMotor2D Angular( |
| | | 74 | | Fixed64 targetAngle, |
| | | 75 | | Fixed64 driveStrength, |
| | | 76 | | Fixed64 damping, |
| | | 77 | | Fixed64 maximumMotorImpulse) => |
| | 9 | 78 | | new(JointMotorKind2D.Angular, targetAngle, driveStrength, damping, maximumMotorImpulse); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Creates a linear motor that drives prismatic translation toward <paramref name="targetTranslation"/>. |
| | | 82 | | /// </summary> |
| | | 83 | | public static JointMotor2D Linear( |
| | | 84 | | Fixed64 targetTranslation, |
| | | 85 | | Fixed64 driveStrength, |
| | | 86 | | Fixed64 damping, |
| | | 87 | | Fixed64 maximumMotorImpulse) => |
| | 10 | 88 | | new(JointMotorKind2D.Linear, targetTranslation, driveStrength, damping, maximumMotorImpulse); |
| | | 89 | | |
| | | 90 | | internal void Validate() |
| | | 91 | | { |
| | 246 | 92 | | SwiftThrowHelper.ThrowIfArgument( |
| | 246 | 93 | | Kind != JointMotorKind2D.Disabled |
| | 246 | 94 | | && Kind != JointMotorKind2D.Angular |
| | 246 | 95 | | && Kind != JointMotorKind2D.Linear, |
| | 246 | 96 | | nameof(Kind), |
| | 246 | 97 | | "Unsupported 2D joint motor kind."); |
| | 246 | 98 | | SwiftThrowHelper.ThrowIfArgument(DriveStrength < Fixed64.Zero, nameof(DriveStrength), "Joint motor drive strengt |
| | 246 | 99 | | SwiftThrowHelper.ThrowIfArgument(Damping < Fixed64.Zero, nameof(Damping), "Joint motor damping cannot be negativ |
| | 246 | 100 | | SwiftThrowHelper.ThrowIfArgument(MaximumMotorImpulse < Fixed64.Zero, nameof(MaximumMotorImpulse), "Joint motor m |
| | 246 | 101 | | } |
| | | 102 | | } |