| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | using Trailblazer.Support; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation.Motor; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Handles the scout's movement, including speed limits, acceleration, and velocity calculations. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// This locomotion module governs how the scout accelerates, decelerates, and interacts with terrain slopes. |
| | | 12 | | /// It tracks position changes and velocity updates for consistent movement behavior. |
| | | 13 | | /// </remarks> |
| | | 14 | | public class MoveLocomotion : ILocomotion |
| | | 15 | | { |
| | | 16 | | #region Constants |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// The minimum velocity threshold below which movement is considered negligible. |
| | | 20 | | /// </summary> |
| | 1 | 21 | | public static readonly Fixed64 VelocityEpsilon = Fixed64.FromRaw(0x418938L); //0.001f; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Default maximum walking speed. |
| | | 25 | | /// </summary> |
| | 1 | 26 | | public static readonly Fixed64 DefaultMaxSlowSpeed = (Fixed64)0.1f;//1d; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Default maximum jogging speed. |
| | | 30 | | /// </summary> |
| | 1 | 31 | | public static readonly Fixed64 DefaultMaxModerateSpeed = (Fixed64)0.25d;//2d; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Default maximum sprinting speed. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public static readonly Fixed64 DefaultMaxFastSpeed = (Fixed64)0.5d;//3d; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Default maximum sideways movement speed. |
| | | 40 | | /// </summary> |
| | 1 | 41 | | public static readonly Fixed64 DefaultMaxSidewaysSpeed = (Fixed64)0.15d;//2d; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Default maximum backward movement speed. |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public static readonly Fixed64 DefaultMaxBackwardsSpeed = (Fixed64)0.15d;//2d; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Default maximum acceleration when moving on the ground. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <remarks> |
| | | 52 | | /// This is timescaled, with a default TimeDelta of `0.03125` this resolves to 1. |
| | | 53 | | /// </remarks> |
| | 1 | 54 | | public static readonly Fixed64 DefaultMaxGroundAcceleration = (Fixed64)32; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Default maximum acceleration when moving in the air. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <remarks> |
| | | 60 | | /// This is timescaled, with a default TimeDelta of `0.03125` this resolves to `0.625`. |
| | | 61 | | /// </remarks> |
| | 1 | 62 | | public static readonly Fixed64 DefaultMaxAirAcceleration = (Fixed64)20; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Default slope speed modifier curve, determining speed adjustments based on incline. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// - Full downward slope (-90°) retains full speed. |
| | | 69 | | /// - Flat ground (0°) retains full speed. |
| | | 70 | | /// - Full upward slope (90°) reduces speed to zero. |
| | | 71 | | /// </remarks> |
| | 1 | 72 | | public static readonly FixedCurve DefaultSlopeSpeedModifier = new(FixedCurveMode.Linear, |
| | 1 | 73 | | new FixedCurveKey(-90, 1.5), // Full downward slope boosts speed 1.5x |
| | 1 | 74 | | new FixedCurveKey(-45, 1.2), // Moderate downward slope boosts speed 1.2x |
| | 1 | 75 | | new FixedCurveKey(0, 1), // Flat ground, normal speed |
| | 1 | 76 | | new FixedCurveKey(45, 0.8), // Moderate uphill slows down |
| | 1 | 77 | | new FixedCurveKey(90, 0) // Full uphill completely stops movement |
| | 1 | 78 | | ); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// The default maximum slope angle before a surface is considered too steep for normal control. |
| | | 82 | | /// </summary> |
| | 1 | 83 | | public static readonly Fixed64 DefaultSlopeLimit = Fixed64.FromRaw(0x2D00000000L); // 45f; |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// The default passive drag factor applied while moving through water. |
| | | 87 | | /// </summary> |
| | 1 | 88 | | public static readonly Fixed64 DefaultWaterDragFactor = Fixed64.FromRaw(0x10000000L); // ~0.0625 |
| | | 89 | | |
| | | 90 | | #endregion |
| | | 91 | | |
| | | 92 | | #region Configuration State |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Determines whether movement mechanics are enabled. |
| | | 96 | | /// </summary> |
| | 824 | 97 | | private bool _isEnabled = true; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// The maximum speed when walking. |
| | | 101 | | /// </summary> |
| | 824 | 102 | | public Fixed64 MaxSlowSpeed = DefaultMaxSlowSpeed; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// The maximum speed when jogging. |
| | | 106 | | /// </summary> |
| | 824 | 107 | | public Fixed64 MaxModerateSpeed = DefaultMaxModerateSpeed; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// The maximum speed when sprinting. |
| | | 111 | | /// </summary> |
| | 824 | 112 | | public Fixed64 MaxFastSpeed = DefaultMaxFastSpeed; |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// The maximum speed when moving sideways. |
| | | 116 | | /// </summary> |
| | 824 | 117 | | public Fixed64 MaxSidewaysSpeed = DefaultMaxSidewaysSpeed; |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// The maximum speed when moving backward. |
| | | 121 | | /// </summary> |
| | 824 | 122 | | public Fixed64 MaxBackwardsSpeed = DefaultMaxBackwardsSpeed; |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// The maximum acceleration when moving on the ground. |
| | | 126 | | /// Higher values result in quicker acceleration. |
| | | 127 | | /// </summary> |
| | 824 | 128 | | public Fixed64 MaxGroundAcceleration = DefaultMaxGroundAcceleration; |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// The maximum acceleration when moving in the air. |
| | | 132 | | /// </summary> |
| | 824 | 133 | | public Fixed64 MaxAirAcceleration = DefaultMaxAirAcceleration; |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// A global multiplier applied to movement speed. |
| | | 137 | | /// </summary> |
| | 824 | 138 | | public Fixed64 MoveSpeedMultiplier = Fixed64.One; |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Determines whether movement speed is adjusted based on the slope of the terrain. |
| | | 142 | | /// </summary> |
| | 824 | 143 | | public bool ModifySpeedOnSlope = true; |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// A curve controlling how speed is affected by terrain slope. |
| | | 147 | | /// </summary> |
| | 824 | 148 | | public FixedCurve SlopeSpeedMultiplier = DefaultSlopeSpeedModifier; |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// The slope angle threshold at which a surface becomes too steep for normal movement control. |
| | | 152 | | /// </summary> |
| | 824 | 153 | | public Fixed64 SlopeLimit = DefaultSlopeLimit; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Passive drag applied whenever the motor is in water, even if the water locomotion is absent or inactive. |
| | | 157 | | /// </summary> |
| | 824 | 158 | | public Fixed64 WaterDragFactor = DefaultWaterDragFactor; |
| | | 159 | | |
| | | 160 | | #endregion |
| | | 161 | | |
| | | 162 | | #region Transient State |
| | | 163 | | |
| | | 164 | | /// <inheritdoc cref="ILocomotion.IsEnabled"/> |
| | | 165 | | public bool IsEnabled |
| | | 166 | | { |
| | 20 | 167 | | get => _isEnabled; |
| | | 168 | | set |
| | | 169 | | { |
| | 2 | 170 | | _isEnabled = value; |
| | 2 | 171 | | if (!_isEnabled) |
| | 1 | 172 | | this.ClearTransientState(); |
| | 2 | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// The scout’s current velocity in world space. |
| | | 178 | | /// </summary> |
| | | 179 | | [Transient] |
| | | 180 | | public Vector3d FrameVelocity { get; set; } |
| | | 181 | | |
| | | 182 | | #endregion |
| | | 183 | | |
| | | 184 | | #region Serialization |
| | | 185 | | |
| | | 186 | | /// <inheritdoc /> |
| | | 187 | | public void RecordData(IChronicler chronicler) |
| | | 188 | | { |
| | 106 | 189 | | RecordValues.Look(chronicler, ref _isEnabled, "IsEnabled", true); |
| | 106 | 190 | | RecordValues.Look(chronicler, ref MaxSlowSpeed, "MaxSlowSpeed", DefaultMaxSlowSpeed); |
| | 106 | 191 | | RecordValues.Look(chronicler, ref MaxModerateSpeed, "MaxModerateSpeed", DefaultMaxModerateSpeed); |
| | 106 | 192 | | RecordValues.Look(chronicler, ref MaxFastSpeed, "MaxFastSpeed", DefaultMaxFastSpeed); |
| | 106 | 193 | | RecordValues.Look(chronicler, ref MaxSidewaysSpeed, "MaxSidewaysSpeed", DefaultMaxSidewaysSpeed); |
| | 106 | 194 | | RecordValues.Look(chronicler, ref MaxBackwardsSpeed, "MaxBackwardsSpeed", DefaultMaxBackwardsSpeed); |
| | 106 | 195 | | RecordValues.Look(chronicler, ref MaxGroundAcceleration, "MaxGroundAcceleration", DefaultMaxGroundAcceleration); |
| | 106 | 196 | | RecordValues.Look(chronicler, ref MaxAirAcceleration, "MaxAirAcceleration", DefaultMaxAirAcceleration); |
| | 106 | 197 | | RecordValues.Look(chronicler, ref MoveSpeedMultiplier, "MoveSpeedMultiplier", Fixed64.One); |
| | 106 | 198 | | RecordValues.Look(chronicler, ref ModifySpeedOnSlope, "ModifySpeedOnSlope", true); |
| | 106 | 199 | | RecordValues.Look(chronicler, ref SlopeSpeedMultiplier, "SlopeSpeedMultiplier", DefaultSlopeSpeedModifier); |
| | 106 | 200 | | RecordValues.Look(chronicler, ref SlopeLimit, "SlopeLimit", DefaultSlopeLimit); |
| | 106 | 201 | | RecordValues.Look(chronicler, ref WaterDragFactor, "WaterDragFactor", DefaultWaterDragFactor); |
| | | 202 | | |
| | 106 | 203 | | Vector3d frameVelocity = FrameVelocity; |
| | 106 | 204 | | RecordValues.Look(chronicler, ref frameVelocity, "FrameVelocity", Vector3d.Zero); |
| | | 205 | | |
| | 106 | 206 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 207 | | { |
| | 53 | 208 | | FrameVelocity = frameVelocity; |
| | | 209 | | |
| | 53 | 210 | | if (!_isEnabled) |
| | 2 | 211 | | this.ClearTransientState(); |
| | | 212 | | } |
| | 106 | 213 | | } |
| | | 214 | | |
| | | 215 | | #endregion |
| | | 216 | | } |