| | | 1 | | //======================================================================= |
| | | 2 | | // NavMotor.Traversal.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-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 | | |
| | | 12 | | namespace Trailblazer.Navigation.Motor; |
| | | 13 | | |
| | | 14 | | public partial class NavMotor |
| | | 15 | | { |
| | | 16 | | #region Request Traversal |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Processes a movement request and applies necessary forces. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// This method opens a traversal phase for the current frame so duplicate force accumulation is rejected |
| | | 23 | | /// until the host calls <see cref="FinalizeTraversal"/> or <see cref="AbortTraversalFrame"/>. |
| | | 24 | | /// Movement forces such as gravity, jump, and platform adjustments are applied. |
| | | 25 | | /// </remarks> |
| | | 26 | | /// <param name="request">The movement request containing desired movement parameters</param> |
| | | 27 | | /// <param name="velocityDelta">The resulting velocity change to apply to the object</param> |
| | | 28 | | /// <param name="positionDelta">The resulting position change from platform movement to apply to the object</param> |
| | | 29 | | /// <param name="rotationDelta">The resulting rotation change from platform movement to apply to the object</param> |
| | | 30 | | public bool TryTraversal( |
| | | 31 | | TrekRequest request, |
| | | 32 | | out Vector3d velocityDelta, |
| | | 33 | | out Vector3d positionDelta, |
| | | 34 | | out FixedQuaternion rotationDelta) |
| | | 35 | | { |
| | 2192 | 36 | | ResetTraversalOutputs(out velocityDelta, out positionDelta, out rotationDelta); |
| | 2192 | 37 | | if (!TryBeginTraversalFrame()) |
| | 6 | 38 | | return false; |
| | | 39 | | |
| | 2184 | 40 | | PrepareTraversalState(request); |
| | 2184 | 41 | | ResolveTraversalForces(request); |
| | | 42 | | |
| | 2184 | 43 | | velocityDelta = ResolveTraversalVelocityDelta(); |
| | 2184 | 44 | | ResolvePlatformTraversal(request, ref positionDelta, ref rotationDelta); |
| | 2184 | 45 | | return true; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 49 | | private static void ResetTraversalOutputs( |
| | | 50 | | out Vector3d velocityDelta, |
| | | 51 | | out Vector3d positionDelta, |
| | | 52 | | out FixedQuaternion rotationDelta) |
| | | 53 | | { |
| | 2192 | 54 | | velocityDelta = Vector3d.Zero; |
| | 2192 | 55 | | positionDelta = Vector3d.Zero; |
| | 2192 | 56 | | rotationDelta = FixedQuaternion.Identity; |
| | 2192 | 57 | | } |
| | | 58 | | |
| | | 59 | | private bool TryBeginTraversalFrame() |
| | | 60 | | { |
| | 2192 | 61 | | if (!IsInitialized) |
| | 1 | 62 | | return false; |
| | | 63 | | |
| | 2191 | 64 | | if (TraversalInProgress) |
| | | 65 | | { |
| | 7 | 66 | | SwiftThrowHelper.ThrowIfTrue( |
| | 7 | 67 | | FrameCount != _pendingTraversalFrame, |
| | 7 | 68 | | message: $"NavMotor traversal from frame {_pendingTraversalFrame} was never finalized or aborted before |
| | | 69 | | |
| | 5 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | 2184 | 73 | | TraversalInProgress = true; |
| | 2184 | 74 | | _pendingTraversalFrame = FrameCount; |
| | 2184 | 75 | | return true; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private void PrepareTraversalState(TrekRequest request) |
| | | 79 | | { |
| | 2184 | 80 | | TrailblazerLogger.DebugChannel.Info( |
| | 2184 | 81 | | $"NavMotor State: Grounded={IsOnSolid}, InAir={IsInGas}, InWater={IsInLiquid}, Flying={IsFlying}, InLimbo={I |
| | | 82 | | |
| | | 83 | | // Calculate the slope angle for the current frame based on the movement direction and surface normal. |
| | 2184 | 84 | | FrameSlopeAngle = CurrentState.GetSignedSlopeAngle(request.Direction); |
| | | 85 | | |
| | | 86 | | // Store the current velocity for manipulation. |
| | 2184 | 87 | | _forceOutput = Handler.Move.FrameVelocity; |
| | | 88 | | |
| | | 89 | | // Update platform velocity prior to applying jump force. |
| | 2184 | 90 | | PlatformModule.UpdatePlatformVelocity(); |
| | | 91 | | |
| | 2184 | 92 | | if (InLimbo) |
| | 55 | 93 | | Handler.IsInControl = false; |
| | | 94 | | |
| | 2184 | 95 | | if (JumpModule?.IsCoolingDown == true) |
| | 102 | 96 | | JumpModule.UpdateCooldown(); |
| | | 97 | | |
| | 2184 | 98 | | UpdateClimbState(request); |
| | 2184 | 99 | | UpdateFlightState(request); |
| | 2184 | 100 | | UpdateSwimState(request); |
| | 2184 | 101 | | } |
| | | 102 | | |
| | | 103 | | #region Traversal Force Resolution |
| | | 104 | | |
| | | 105 | | private void ResolveTraversalForces(TrekRequest request) |
| | | 106 | | { |
| | 2184 | 107 | | ComputeMovementForces(request); |
| | | 108 | | |
| | | 109 | | // Reset this before applying gravity. |
| | 2184 | 110 | | if (JumpModule != null && (!request.IsRequestingJump || !JumpModule.CanJump)) |
| | 2134 | 111 | | JumpModule.IsHoldingJump = false; |
| | | 112 | | |
| | 2184 | 113 | | ApplyEnvironmentalForces(); |
| | 2184 | 114 | | ApplyJumpForce(request); |
| | 2184 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Computes the movement forces based on the traversal state and applies them to the object. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <remarks> |
| | | 121 | | /// This method determines whether the object is in control, calculates velocity adjustments, |
| | | 122 | | /// and applies constraints such as slope resistance and airborne drag. |
| | | 123 | | /// </remarks> |
| | | 124 | | private void ComputeMovementForces(TrekRequest frameRequest) |
| | | 125 | | { |
| | 2184 | 126 | | UpdateControlState(); |
| | | 127 | | |
| | 2184 | 128 | | if (InLimbo) |
| | 22 | 129 | | return; |
| | | 130 | | |
| | 2162 | 131 | | ResetDownwardMomentumIfNeeded(); |
| | | 132 | | |
| | 2162 | 133 | | Vector3d desiredVelocity = GetDesiredVelocity(frameRequest); |
| | | 134 | | |
| | 2162 | 135 | | if (TryApplyStationaryGroundFriction(frameRequest.Direction)) |
| | 5 | 136 | | return; |
| | | 137 | | |
| | 2157 | 138 | | ApplyDesiredVelocity(desiredVelocity); |
| | 2157 | 139 | | } |
| | | 140 | | |
| | | 141 | | private void UpdateControlState() |
| | | 142 | | { |
| | 2184 | 143 | | if (IsClimbing) |
| | | 144 | | { |
| | 66 | 145 | | SetSlidingState(false); |
| | 66 | 146 | | Handler.IsInControl = true; |
| | 66 | 147 | | return; |
| | | 148 | | } |
| | | 149 | | |
| | 2118 | 150 | | if (IsOnSolid) |
| | | 151 | | { |
| | 266 | 152 | | if (IsFlying) |
| | | 153 | | { |
| | 1 | 154 | | SetSlidingState(false); |
| | 1 | 155 | | Handler.IsInControl = true; |
| | | 156 | | } |
| | | 157 | | else |
| | | 158 | | { |
| | 265 | 159 | | bool isTooSteep = IsTooSteep(FrameSlopeAngle); |
| | 265 | 160 | | SetSlidingState(SlideModule?.IsEnabled == true && isTooSteep); |
| | 265 | 161 | | Handler.IsInControl = !isTooSteep; |
| | | 162 | | } |
| | | 163 | | |
| | 265 | 164 | | return; |
| | | 165 | | } |
| | | 166 | | |
| | 1852 | 167 | | Handler.IsInControl = IsFlying || !InLimbo; |
| | 1852 | 168 | | } |
| | | 169 | | |
| | | 170 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 171 | | private void SetSlidingState(bool isSliding) |
| | | 172 | | { |
| | 332 | 173 | | if (SlideModule != null) |
| | 327 | 174 | | SlideModule.IsSliding = isSliding; |
| | 332 | 175 | | } |
| | | 176 | | |
| | | 177 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 178 | | private void ResetDownwardMomentumIfNeeded() |
| | | 179 | | { |
| | 2162 | 180 | | if (IsClimbing) |
| | | 181 | | { |
| | 66 | 182 | | _forceOutput = Vector3d.Zero; |
| | 66 | 183 | | return; |
| | | 184 | | } |
| | | 185 | | |
| | 2096 | 186 | | if ((!IsOnSolid || WasInGas) && !IsFlying) |
| | 1823 | 187 | | _forceOutput.Y = Fixed64.Zero; |
| | 2096 | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Determines the desired velocity based on input direction, movement constraints, and traversal state. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <returns>The computed velocity vector that the object should move toward.</returns> |
| | | 194 | | private Vector3d GetDesiredVelocity(TrekRequest frameRequest) |
| | | 195 | | { |
| | 2162 | 196 | | if (IsFlying) |
| | 11 | 197 | | return GetFlightVelocity(frameRequest); |
| | | 198 | | |
| | 2151 | 199 | | if (ClimbModule?.IsMantling == true) |
| | 20 | 200 | | return GetMantleVelocity(frameRequest.Origin); |
| | | 201 | | |
| | 2131 | 202 | | if (IsClimbing) |
| | 46 | 203 | | return GetClimbVelocity(frameRequest); |
| | | 204 | | |
| | 2085 | 205 | | Vector3d result = GetControlledSurfaceVelocity(frameRequest); |
| | 2085 | 206 | | if (IsInLiquid) |
| | 179 | 207 | | return ResolveLiquidVelocity(frameRequest, result); |
| | | 208 | | |
| | 1906 | 209 | | result = ApplyPlatformTransferVelocity(result); |
| | 1906 | 210 | | return ApplyGroundVelocityConstraints(result); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Computes the desired world-space velocity while controlled flight is active. |
| | | 215 | | /// </summary> |
| | | 216 | | private Vector3d GetFlightVelocity(TrekRequest frameRequest) |
| | | 217 | | { |
| | 11 | 218 | | if (frameRequest.Rate == TrekRate.Stationary) |
| | 4 | 219 | | return Vector3d.Zero; |
| | | 220 | | |
| | 7 | 221 | | FlyLocomotion flyModule = FlyModule!; |
| | 7 | 222 | | Fixed64 speedMultiplier = GetFlightSpeedMultiplier(frameRequest.Rate); |
| | 7 | 223 | | if (speedMultiplier <= Fixed64.Zero) |
| | 1 | 224 | | return Vector3d.Zero; |
| | | 225 | | |
| | 6 | 226 | | Fixed3x3 transposedMatrix = frameRequest.Rotation.ToMatrix3x3(); |
| | 6 | 227 | | Vector3d desiredLocalDirection = Fixed3x3.InverseTransformDirection(transposedMatrix, frameRequest.Direction); |
| | 6 | 228 | | Vector3d desiredLocalVelocity = Vector3d.Zero; |
| | | 229 | | |
| | 6 | 230 | | Vector3d horizontalInput = new(desiredLocalDirection.X, Fixed64.Zero, desiredLocalDirection.Z); |
| | 6 | 231 | | Fixed64 horizontalMagnitude = FixedMath.Clamp01(horizontalInput.Magnitude); |
| | 6 | 232 | | if (horizontalMagnitude > Fixed64.Zero) |
| | 3 | 233 | | desiredLocalVelocity += horizontalInput.Normalized * (flyModule.MaxFlySpeed * speedMultiplier * horizontalMa |
| | | 234 | | |
| | 6 | 235 | | Fixed64 verticalInput = FixedMath.Clamp(desiredLocalDirection.Y, -Fixed64.One, Fixed64.One); |
| | 6 | 236 | | if (verticalInput > Fixed64.Zero) |
| | 1 | 237 | | desiredLocalVelocity.Y = verticalInput * flyModule.MaxAscendSpeed * speedMultiplier; |
| | 5 | 238 | | else if (verticalInput < Fixed64.Zero) |
| | 1 | 239 | | desiredLocalVelocity.Y = verticalInput.Abs() * -flyModule.MaxDescendSpeed * speedMultiplier; |
| | | 240 | | |
| | 6 | 241 | | return Fixed3x3.TransformDirection(transposedMatrix, desiredLocalVelocity); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private Vector3d GetClimbVelocity(TrekRequest frameRequest) |
| | | 245 | | { |
| | 46 | 246 | | if (frameRequest.Rate == TrekRate.Stationary) |
| | 2 | 247 | | return Vector3d.Zero; |
| | | 248 | | |
| | 44 | 249 | | ClimbLocomotion climbModule = ClimbModule!; |
| | 44 | 250 | | Vector3d upAxis = climbModule.AttachedUpDirection != Vector3d.Zero |
| | 44 | 251 | | ? climbModule.AttachedUpDirection.Normalized |
| | 44 | 252 | | : Vector3d.Up; |
| | 44 | 253 | | Vector3d outwardNormal = climbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 44 | 254 | | ? climbModule.AttachedSurfaceNormal.Normalized |
| | 44 | 255 | | : Vector3d.Backward; |
| | 44 | 256 | | Vector3d lateralAxis = Vector3d.Cross(upAxis, outwardNormal); |
| | 44 | 257 | | if (lateralAxis == Vector3d.Zero) |
| | 1 | 258 | | lateralAxis = Vector3d.Cross(Vector3d.Up, outwardNormal); |
| | 44 | 259 | | if (lateralAxis == Vector3d.Zero) |
| | 1 | 260 | | lateralAxis = Vector3d.Right; |
| | 44 | 261 | | lateralAxis = lateralAxis.Normalized; |
| | | 262 | | |
| | 44 | 263 | | Fixed64 verticalAmount = Vector3d.Dot(frameRequest.Direction, upAxis); |
| | 44 | 264 | | if (!climbModule.ActiveAllowDescent && verticalAmount < Fixed64.Zero) |
| | 1 | 265 | | verticalAmount = Fixed64.Zero; |
| | | 266 | | |
| | 44 | 267 | | Fixed64 lateralAmount = climbModule.ActiveAllowLateralTraverse |
| | 44 | 268 | | ? Vector3d.Dot(frameRequest.Direction, lateralAxis) |
| | 44 | 269 | | : Fixed64.Zero; |
| | | 270 | | |
| | 44 | 271 | | Vector3d climbDirection = (upAxis * verticalAmount) + (lateralAxis * lateralAmount); |
| | 44 | 272 | | Fixed64 inputMagnitude = FixedMath.Clamp01(climbDirection.Magnitude); |
| | 44 | 273 | | if (inputMagnitude <= Fixed64.Zero) |
| | 6 | 274 | | return Vector3d.Zero; |
| | | 275 | | |
| | 38 | 276 | | return climbDirection.Normalized * (climbModule.MaxClimbSpeed * inputMagnitude); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private Vector3d GetMantleVelocity(Vector3d origin) |
| | | 280 | | { |
| | 20 | 281 | | ClimbLocomotion climbModule = ClimbModule!; |
| | 20 | 282 | | if (!climbModule.MantleTargetPosition.HasValue) |
| | 1 | 283 | | return Vector3d.Zero; |
| | | 284 | | |
| | 19 | 285 | | Vector3d mantleTargetPosition = climbModule.MantleTargetPosition.GetValueOrDefault(); |
| | 19 | 286 | | Vector3d toTarget = mantleTargetPosition - origin; |
| | 19 | 287 | | if (toTarget == Vector3d.Zero) |
| | 1 | 288 | | return Vector3d.Zero; |
| | | 289 | | |
| | 18 | 290 | | Fixed64 distance = toTarget.Magnitude; |
| | 18 | 291 | | if (distance <= climbModule.ClimbStartTolerance) |
| | 1 | 292 | | return Vector3d.Zero; |
| | | 293 | | |
| | 17 | 294 | | return toTarget.Normalized * climbModule.MaxClimbSpeed; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | private Vector3d GetControlledSurfaceVelocity(TrekRequest frameRequest) |
| | | 298 | | { |
| | 2085 | 299 | | if (SlideModule?.IsSliding == true) |
| | 27 | 300 | | return GetSlidingVelocity(frameRequest); |
| | | 301 | | |
| | 2058 | 302 | | return Handler.IsInControl && frameRequest.Rate != TrekRate.Stationary |
| | 2058 | 303 | | ? GetHorizontalVelocity(frameRequest) |
| | 2058 | 304 | | : Vector3d.Zero; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private Vector3d GetSlidingVelocity(TrekRequest frameRequest) |
| | | 308 | | { |
| | 27 | 309 | | SlideLocomotion slideModule = SlideModule!; |
| | | 310 | | |
| | 27 | 311 | | Vector3d slideDirection = new Vector3d( |
| | 27 | 312 | | CurrentState.SurfaceNormal.X, |
| | 27 | 313 | | Fixed64.Zero, |
| | 27 | 314 | | CurrentState.SurfaceNormal.Z).Normalized; |
| | 27 | 315 | | Vector3d projectedMoveDir = Vector3d.Project(frameRequest.Direction, slideDirection); |
| | 27 | 316 | | Vector3d speedContribution = projectedMoveDir * slideModule.SpeedControl; |
| | 27 | 317 | | Vector3d sidewaysContribution = (frameRequest.Direction - projectedMoveDir) * slideModule.SidewaysControl; |
| | 27 | 318 | | return slideDirection + ((speedContribution + sidewaysContribution) * slideModule.SlidingSpeed); |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | private Vector3d GetHorizontalVelocity(TrekRequest frameRequest) |
| | | 322 | | { |
| | 164 | 323 | | Fixed3x3 transposedMatrix = frameRequest.Rotation.ToMatrix3x3(); |
| | 164 | 324 | | Vector3d desiredLocalDirection = Fixed3x3.InverseTransformDirection(transposedMatrix, frameRequest.Direction); |
| | 164 | 325 | | Fixed64 speed = MaxHoritzontalSpeedInDirection(desiredLocalDirection, frameRequest.Rate); |
| | | 326 | | |
| | 164 | 327 | | speed *= Handler.Move.MoveSpeedMultiplier; |
| | | 328 | | |
| | | 329 | | // Modify max speed on slopes based on slope speed multiplier curve |
| | 164 | 330 | | if (Handler.Move.ModifySpeedOnSlope && IsOnSlope(FrameSlopeAngle)) |
| | 34 | 331 | | speed *= Handler.Move.SlopeSpeedMultiplier.Evaluate(FrameSlopeAngle); |
| | | 332 | | |
| | 164 | 333 | | return Fixed3x3.TransformDirection(transposedMatrix, desiredLocalDirection * speed); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Calculates the maximum allowable speed in a given movement direction. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="desiredMovementDirection">The movement direction to evaluate.</param> |
| | | 340 | | /// <param name="rate">The rate that dictates the maximum speed allowed.</param> |
| | | 341 | | /// <returns>The maximum speed possible in the specified direction.</returns> |
| | | 342 | | public Fixed64 MaxHoritzontalSpeedInDirection(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 343 | | { |
| | 190 | 344 | | if (desiredMovementDirection == Vector3d.Zero) |
| | 4 | 345 | | return Fixed64.Zero; |
| | | 346 | | |
| | 186 | 347 | | if (IsFlying) |
| | 10 | 348 | | return GetFlightHorizontalSpeed(desiredMovementDirection, rate); |
| | | 349 | | |
| | 176 | 350 | | if (IsInLiquid) |
| | 26 | 351 | | return GetLiquidHorizontalSpeed(desiredMovementDirection); |
| | | 352 | | |
| | 150 | 353 | | return GetGroundHorizontalSpeed(desiredMovementDirection, rate); |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private Fixed64 GetFlightHorizontalSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 357 | | { |
| | 10 | 358 | | FlyLocomotion flyModule = FlyModule!; |
| | 10 | 359 | | if (!flyModule.IsEnabled || !flyModule.CanFly) |
| | 3 | 360 | | return Fixed64.Zero; |
| | | 361 | | |
| | 7 | 362 | | Fixed64 horizontalMagnitude = FixedMath.Clamp01(new Vector3d( |
| | 7 | 363 | | desiredMovementDirection.X, |
| | 7 | 364 | | Fixed64.Zero, |
| | 7 | 365 | | desiredMovementDirection.Z).Magnitude); |
| | 7 | 366 | | return horizontalMagnitude * flyModule.MaxFlySpeed * GetFlightSpeedMultiplier(rate); |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | private Fixed64 GetFlightSpeedMultiplier(TrekRate rate) |
| | | 370 | | { |
| | 14 | 371 | | if (rate == TrekRate.Stationary) |
| | 1 | 372 | | return Fixed64.Zero; |
| | | 373 | | |
| | 13 | 374 | | Fixed64 maxFastSpeed = Handler.Move.MaxFastSpeed; |
| | 13 | 375 | | if (rate == TrekRate.Fast || maxFastSpeed <= Fixed64.Zero) |
| | 9 | 376 | | return Fixed64.One; |
| | | 377 | | |
| | 4 | 378 | | return FixedMath.Clamp(GetScaledFlightSpeedMultiplier(rate, maxFastSpeed), Fixed64.Zero, Fixed64.One); |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | private Fixed64 GetLiquidHorizontalSpeed(Vector3d desiredMovementDirection) |
| | | 382 | | { |
| | 26 | 383 | | if (WaterModule?.IsEnabled != true |
| | 26 | 384 | | || !WaterModule.CanSwim |
| | 26 | 385 | | || !WaterModule.IsSwimming) |
| | | 386 | | { |
| | 9 | 387 | | return Fixed64.Zero; |
| | | 388 | | } |
| | | 389 | | |
| | 17 | 390 | | Fixed64 ellipseMultiplier = WaterModule.MaxSwimSpeed / WaterModule.MaxSwimSidewaysSpeed; |
| | 17 | 391 | | if (ellipseMultiplier <= Fixed64.Zero) |
| | 1 | 392 | | return Fixed64.Zero; |
| | | 393 | | |
| | 16 | 394 | | return GetEllipticalHorizontalSpeed( |
| | 16 | 395 | | desiredMovementDirection, |
| | 16 | 396 | | ellipseMultiplier, |
| | 16 | 397 | | WaterModule.MaxSwimSidewaysSpeed, |
| | 16 | 398 | | Fixed64.One); |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | private Fixed64 GetGroundHorizontalSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 402 | | { |
| | 150 | 403 | | Fixed64 maxSpeed = GetGroundDirectionalMaxSpeed(desiredMovementDirection, rate); |
| | 150 | 404 | | Fixed64 ellipseMultiplier = maxSpeed / Handler.Move.MaxSidewaysSpeed; |
| | 150 | 405 | | if (ellipseMultiplier <= Fixed64.Zero) |
| | 2 | 406 | | return Fixed64.Zero; |
| | | 407 | | |
| | 148 | 408 | | return GetEllipticalHorizontalSpeed( |
| | 148 | 409 | | desiredMovementDirection, |
| | 148 | 410 | | ellipseMultiplier, |
| | 148 | 411 | | Handler.Move.MaxSidewaysSpeed, |
| | 148 | 412 | | GetAirborneControlMultiplier()); |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | private Fixed64 GetAirborneControlMultiplier() |
| | | 416 | | { |
| | 148 | 417 | | if (IsOnSolid) |
| | 124 | 418 | | return Fixed64.One; |
| | | 419 | | |
| | 24 | 420 | | if (IsJumping) |
| | 2 | 421 | | return JumpModule!.JumpControlMultiplier; |
| | | 422 | | |
| | 22 | 423 | | if (IsFalling) |
| | 21 | 424 | | return Handler.Fall.FallControlMultiplier; |
| | | 425 | | |
| | 1 | 426 | | return Fixed64.One; |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | private static Fixed64 GetEllipticalHorizontalSpeed( |
| | | 430 | | Vector3d desiredMovementDirection, |
| | | 431 | | Fixed64 zAxisEllipseMultiplier, |
| | | 432 | | Fixed64 sidewaysSpeed, |
| | | 433 | | Fixed64 controlMultiplier) |
| | | 434 | | { |
| | 164 | 435 | | Vector3d normalized = new Vector3d( |
| | 164 | 436 | | desiredMovementDirection.X, |
| | 164 | 437 | | Fixed64.Zero, |
| | 164 | 438 | | desiredMovementDirection.Z / zAxisEllipseMultiplier).Normalized; |
| | 164 | 439 | | Fixed64 length = new Vector3d( |
| | 164 | 440 | | normalized.X, |
| | 164 | 441 | | Fixed64.Zero, |
| | 164 | 442 | | normalized.Z * zAxisEllipseMultiplier).Magnitude; |
| | 164 | 443 | | return length * sidewaysSpeed * controlMultiplier; |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | private Fixed64 GetGroundDirectionalMaxSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 447 | | { |
| | 150 | 448 | | if (desiredMovementDirection.Z < Fixed64.Zero) |
| | 4 | 449 | | return Handler.Move.MaxBackwardsSpeed; |
| | | 450 | | |
| | 146 | 451 | | return rate switch |
| | 146 | 452 | | { |
| | 82 | 453 | | TrekRate.Slow => Handler.Move.MaxSlowSpeed, |
| | 57 | 454 | | TrekRate.Moderate => Handler.Move.MaxModerateSpeed, |
| | 6 | 455 | | TrekRate.Fast => Handler.Move.MaxFastSpeed, |
| | 1 | 456 | | _ => Fixed64.Zero |
| | 146 | 457 | | }; |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | private Vector3d ResolveLiquidVelocity(TrekRequest frameRequest, Vector3d desiredVelocity) |
| | | 461 | | { |
| | 179 | 462 | | if (WaterModule?.IsEnabled != true |
| | 179 | 463 | | || !WaterModule.CanSwim |
| | 179 | 464 | | || !WaterModule.IsSwimming) |
| | | 465 | | { |
| | 161 | 466 | | desiredVelocity = Vector3d.Zero; |
| | | 467 | | } |
| | | 468 | | |
| | 179 | 469 | | if (WaterModule?.IsSwimming == true && frameRequest.Direction.Y != Fixed64.Zero) |
| | 10 | 470 | | desiredVelocity.Y = frameRequest.Direction.Y * WaterModule.MaxSwimSpeed; |
| | | 471 | | |
| | 179 | 472 | | if (desiredVelocity != Vector3d.Zero) |
| | 14 | 473 | | desiredVelocity *= FixedMath.Clamp01(Fixed64.One - Handler.Move.WaterDragFactor); |
| | | 474 | | |
| | 179 | 475 | | return desiredVelocity; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private Vector3d ApplyPlatformTransferVelocity(Vector3d desiredVelocity) |
| | | 479 | | { |
| | 1906 | 480 | | PlatformLocomotion platformModule = PlatformModule; |
| | 1906 | 481 | | if (platformModule.MovementTransfer == MotionTransfer.PermaTransfer) |
| | | 482 | | { |
| | 2 | 483 | | desiredVelocity += platformModule.FramePlatformVelocity; |
| | 2 | 484 | | desiredVelocity.Y = Fixed64.Zero; |
| | | 485 | | } |
| | | 486 | | |
| | 1906 | 487 | | return desiredVelocity; |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | private Vector3d ApplyGroundVelocityConstraints(Vector3d desiredVelocity) |
| | | 491 | | { |
| | 1906 | 492 | | if (!IsOnSolid || desiredVelocity == Vector3d.Zero) |
| | 1758 | 493 | | return desiredVelocity; |
| | | 494 | | |
| | 148 | 495 | | Fixed64 surfaceFriction = CurrentState.GroundState?.SurfaceFriction ?? Fixed64.Zero; |
| | 148 | 496 | | desiredVelocity *= Fixed64.One - surfaceFriction; |
| | | 497 | | |
| | | 498 | | // Flat or host-defined "solid but no sampled normal" surfaces should preserve the raw ground vector. |
| | | 499 | | // Sliding also already produces a slope-aware direction, so re-projecting it here distorts the result. |
| | 148 | 500 | | if (CurrentState.SurfaceNormal == Vector3d.Zero |
| | 148 | 501 | | || CurrentState.SlopeAngle == Fixed64.Zero |
| | 148 | 502 | | || SlideModule?.IsSliding == true) |
| | | 503 | | { |
| | 98 | 504 | | return desiredVelocity; |
| | | 505 | | } |
| | | 506 | | |
| | 50 | 507 | | Vector3d sideways = Vector3d.Cross(Vector3d.Up, desiredVelocity); |
| | 50 | 508 | | Vector3d adjustedVelocity = Vector3d.Cross(sideways, CurrentState.SurfaceNormal).Normalized * desiredVelocity.Ma |
| | 50 | 509 | | if (Fixed64.Sign(adjustedVelocity.Y) != Fixed64.Sign(FrameSlopeAngle)) |
| | 49 | 510 | | adjustedVelocity.Y *= -1; |
| | | 511 | | |
| | 50 | 512 | | return adjustedVelocity; |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | private bool TryApplyStationaryGroundFriction(Vector3d desiredDirection) |
| | | 516 | | { |
| | 2162 | 517 | | if (!IsOnSolid || IsFlying || desiredDirection != Vector3d.Zero) |
| | 2023 | 518 | | return false; |
| | | 519 | | |
| | 139 | 520 | | Fixed64 friction = CurrentState.GroundState?.SurfaceFriction ?? Fixed64.Zero; |
| | 139 | 521 | | if (friction <= Fixed64.Zero || _forceOutput == Vector3d.Zero) |
| | 134 | 522 | | return false; |
| | | 523 | | |
| | 5 | 524 | | _forceOutput *= Fixed64.One - friction; |
| | 5 | 525 | | return true; |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | private void ApplyDesiredVelocity(Vector3d desiredVelocity) |
| | | 529 | | { |
| | 2157 | 530 | | if (desiredVelocity == _forceOutput) |
| | 1933 | 531 | | return; |
| | | 532 | | |
| | 224 | 533 | | Fixed64 maxVelocityChange = GetMaxAcceleration() * DeltaTime; |
| | 224 | 534 | | Vector3d velocityChange = (desiredVelocity - _forceOutput).ClampMagnitude(maxVelocityChange); |
| | | 535 | | System.Diagnostics.Debug.Assert( |
| | | 536 | | IsOnSolid || Handler.IsInControl, |
| | | 537 | | "UpdateControlState rejects limbo before desired velocity can be applied."); |
| | | 538 | | |
| | 224 | 539 | | _forceOutput += velocityChange; |
| | 224 | 540 | | if (IsOnSolid && !IsFlying) |
| | 132 | 541 | | _forceOutput.Y = FixedMath.Min(_forceOutput.Y, Fixed64.Zero); |
| | 224 | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <summary> |
| | | 545 | | /// Retrieves the maximum acceleration value based on the object’s current traversal state. |
| | | 546 | | /// </summary> |
| | | 547 | | /// <returns>The acceleration limit depending on whether the object is grounded, airborne, or swimming.</returns> |
| | | 548 | | public Fixed64 GetMaxAcceleration() |
| | | 549 | | { |
| | 239 | 550 | | SwiftThrowHelper.ThrowIfTrue( |
| | 239 | 551 | | CurrentState == null, |
| | 239 | 552 | | message: "NavMotor must be initialized before querying max acceleration."); |
| | | 553 | | |
| | 238 | 554 | | if (IsInLiquid) |
| | 19 | 555 | | return WaterModule?.IsEnabled == true |
| | 19 | 556 | | && WaterModule.CanSwim |
| | 19 | 557 | | && WaterModule.IsSwimming |
| | 19 | 558 | | ? WaterModule.MaxSwimAcceleration |
| | 19 | 559 | | : Handler.Move.MaxAirAcceleration; |
| | | 560 | | |
| | 219 | 561 | | if (IsClimbing) |
| | 58 | 562 | | return ClimbModule!.IsEnabled && ClimbModule.CanClimb |
| | 58 | 563 | | ? ClimbModule.MaxClimbAcceleration |
| | 58 | 564 | | : Handler.Move.MaxAirAcceleration; |
| | | 565 | | |
| | 161 | 566 | | if (IsFlying) |
| | 8 | 567 | | return FlyModule!.IsEnabled && FlyModule.CanFly |
| | 8 | 568 | | ? FlyModule.MaxFlyAcceleration |
| | 8 | 569 | | : Handler.Move.MaxAirAcceleration; |
| | | 570 | | |
| | 284 | 571 | | if (IsOnSolid) return Handler.Move.MaxGroundAcceleration; |
| | | 572 | | |
| | 22 | 573 | | if (IsJumping || IsFalling || IsInGas) |
| | 21 | 574 | | return Handler.Move.MaxAirAcceleration; |
| | | 575 | | |
| | 1 | 576 | | throw new InvalidOperationException( |
| | 1 | 577 | | $"Cannot resolve max acceleration while traversal medium is {CurrentState.Medium}. NavMotor requires a known |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | private void ApplyEnvironmentalForces() |
| | | 581 | | { |
| | 2184 | 582 | | Fixed64 gravityStep = Handler.Forces.GravityForce * DeltaTime; |
| | | 583 | | |
| | 2184 | 584 | | if (IsFlying) |
| | | 585 | | { |
| | 11 | 586 | | Fixed64 gravityCompensation = FixedMath.Clamp( |
| | 11 | 587 | | FlyModule!.GravityCompensation, |
| | 11 | 588 | | Fixed64.Zero, |
| | 11 | 589 | | Fixed64.One); |
| | 11 | 590 | | _forceOutput.Y -= gravityStep * (Fixed64.One - gravityCompensation); |
| | 11 | 591 | | return; |
| | | 592 | | } |
| | | 593 | | |
| | 2173 | 594 | | if (IsClimbing) |
| | | 595 | | { |
| | 66 | 596 | | Fixed64 gravityCompensation = FixedMath.Clamp( |
| | 66 | 597 | | ClimbModule!.GravityCompensationWhileClimbing, |
| | 66 | 598 | | Fixed64.Zero, |
| | 66 | 599 | | Fixed64.One); |
| | 66 | 600 | | _forceOutput.Y -= gravityStep * (Fixed64.One - gravityCompensation); |
| | 66 | 601 | | return; |
| | | 602 | | } |
| | | 603 | | |
| | 2107 | 604 | | if (IsOnSolid) |
| | | 605 | | { |
| | 265 | 606 | | _forceOutput.Y = FixedMath.Min(Fixed64.Zero, _forceOutput.Y) - gravityStep; |
| | 265 | 607 | | return; |
| | | 608 | | } |
| | | 609 | | |
| | 1842 | 610 | | if (IsInLiquid) |
| | | 611 | | { |
| | | 612 | | // Apply buoyancy if we can swim, otherwise apply gravity as normal. |
| | | 613 | | // Even if we can swim, we still apply gravity but reduce it based on the buoyancy factor to |
| | | 614 | | // create a more natural sinking effect when not actively swimming upwards. |
| | 179 | 615 | | if (WaterModule?.IsEnabled == true) |
| | 175 | 616 | | _forceOutput.Y += gravityStep * (WaterModule.BuoyancyFactor - Fixed64.One); |
| | | 617 | | else |
| | 4 | 618 | | _forceOutput.Y = Handler.Move.FrameVelocity.Y - gravityStep; |
| | | 619 | | |
| | 4 | 620 | | return; |
| | | 621 | | } |
| | | 622 | | |
| | 1664 | 623 | | if (!IsInGas) return; |
| | | 624 | | |
| | 1662 | 625 | | _forceOutput.Y = Handler.Move.FrameVelocity.Y - gravityStep; |
| | | 626 | | |
| | | 627 | | // Ensure velocity does not exceed terminal fall speed |
| | 1662 | 628 | | Fixed64 terminalFallSpeed = Handler.Move.FrameVelocity.Y + (_forceOutput.Y * DeltaTime); |
| | 1662 | 629 | | if (terminalFallSpeed < -Handler.Forces.TerminalVelocity) |
| | 7 | 630 | | _forceOutput.Y = -Handler.Forces.TerminalVelocity - Handler.Move.FrameVelocity.Y; |
| | | 631 | | |
| | | 632 | | // When jumping up we don't apply gravity for some time when the user is holding the jump button. |
| | | 633 | | // This allows for more control over jump height by pressing the button longer. |
| | 1662 | 634 | | JumpLocomotion? jumpModule = JumpModule; |
| | 1662 | 635 | | if (IsJumping && jumpModule!.IsHoldingJump) |
| | | 636 | | { |
| | | 637 | | // Calculate the duration that the extra jump force should have effect. |
| | | 638 | | // If we're still less than that duration after the jumping time, apply the force. |
| | 3 | 639 | | Fixed64 extraJumpLimit = (jumpModule.JumpStartTime + jumpModule.ExtraJumpHeight) / GetVerticalJumpSpeed(); |
| | | 640 | | |
| | | 641 | | // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards. |
| | 3 | 642 | | if (TotalTime <= extraJumpLimit) |
| | 2 | 643 | | _forceOutput += jumpModule.FrameJumpDirection * gravityStep; |
| | | 644 | | } |
| | 1662 | 645 | | } |
| | | 646 | | |
| | | 647 | | private void ApplyJumpForce(TrekRequest request) |
| | | 648 | | { |
| | 2184 | 649 | | if (!CanApplyJumpForce(request)) |
| | 2152 | 650 | | return; |
| | | 651 | | |
| | 32 | 652 | | Vector3d jumpForce = IsInLiquid |
| | 32 | 653 | | ? GetWaterBreachJumpForce() |
| | 32 | 654 | | : IsClimbing |
| | 32 | 655 | | ? GetClimbDetachJumpForce() |
| | 32 | 656 | | : GetGroundJumpForce(); |
| | 32 | 657 | | CommitJumpForce(jumpForce); |
| | 32 | 658 | | } |
| | | 659 | | |
| | | 660 | | private bool CanApplyJumpForce(TrekRequest request) |
| | | 661 | | { |
| | 2184 | 662 | | if (!(JumpModule?.IsEnabled == true |
| | 2184 | 663 | | && Handler.IsInControl |
| | 2184 | 664 | | && request.IsRequestingJump)) |
| | | 665 | | { |
| | 2070 | 666 | | return false; |
| | | 667 | | } |
| | | 668 | | |
| | 114 | 669 | | if (IsFlying || IsFalling) |
| | 17 | 670 | | return false; |
| | | 671 | | |
| | 97 | 672 | | if (IsClimbing && !ClimbModule!.ActiveAllowDetachJump) |
| | 1 | 673 | | return false; |
| | | 674 | | |
| | 96 | 675 | | if (IsInLiquid && !(WaterModule?.CanBreachWater ?? false)) |
| | 2 | 676 | | return false; |
| | | 677 | | |
| | 94 | 678 | | if (!JumpModule.CanJump) |
| | 60 | 679 | | return false; |
| | | 680 | | |
| | 34 | 681 | | return request.CanAffordJump; |
| | | 682 | | } |
| | | 683 | | |
| | | 684 | | private Vector3d GetWaterBreachJumpForce() |
| | | 685 | | { |
| | 2 | 686 | | JumpLocomotion jumpModule = JumpModule!; |
| | 2 | 687 | | WaterLocomotion waterModule = WaterModule!; |
| | | 688 | | |
| | 2 | 689 | | jumpModule.FrameJumpDirection = Vector3d.Up; |
| | 2 | 690 | | Events.OnStartWaterBreach?.Invoke(); |
| | 2 | 691 | | return jumpModule.FrameJumpDirection * (GetVerticalJumpSpeed() * waterModule.BreachJumpMultiplier); |
| | | 692 | | } |
| | | 693 | | |
| | | 694 | | private Vector3d GetGroundJumpForce() |
| | | 695 | | { |
| | 28 | 696 | | JumpLocomotion jumpModule = JumpModule!; |
| | | 697 | | |
| | 28 | 698 | | EnsureJumpDirectionInitialized(); |
| | 28 | 699 | | Events.OnStartJump?.Invoke(jumpModule.AvoidGroundingTimer); |
| | 28 | 700 | | return jumpModule.FrameJumpDirection * GetVerticalJumpSpeed(); |
| | | 701 | | } |
| | | 702 | | |
| | | 703 | | private Vector3d GetClimbDetachJumpForce() |
| | | 704 | | { |
| | 2 | 705 | | JumpLocomotion jumpModule = JumpModule!; |
| | 2 | 706 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 707 | | |
| | 2 | 708 | | Vector3d upward = climbModule.AttachedUpDirection != Vector3d.Zero |
| | 2 | 709 | | ? climbModule.AttachedUpDirection.Normalized |
| | 2 | 710 | | : Vector3d.Up; |
| | 2 | 711 | | Vector3d outward = climbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 2 | 712 | | ? climbModule.AttachedSurfaceNormal.Normalized |
| | 2 | 713 | | : Vector3d.Backward; |
| | 2 | 714 | | jumpModule.FrameJumpDirection = Vector3d.Slerp(upward, outward, jumpModule.PerpendicularJumpAmount); |
| | 2 | 715 | | Events.OnStartJump?.Invoke(jumpModule.AvoidGroundingTimer); |
| | 2 | 716 | | return jumpModule.FrameJumpDirection * GetVerticalJumpSpeed(); |
| | | 717 | | } |
| | | 718 | | |
| | | 719 | | private void EnsureJumpDirectionInitialized() |
| | | 720 | | { |
| | 28 | 721 | | JumpLocomotion jumpModule = JumpModule!; |
| | 28 | 722 | | if (jumpModule.IsJumping) |
| | 4 | 723 | | return; |
| | | 724 | | |
| | | 725 | | System.Diagnostics.Debug.Assert( |
| | | 726 | | !IsTooSteep(FrameSlopeAngle), |
| | | 727 | | "Ground-jump admission requires control, which rejects surfaces above the slope limit."); |
| | | 728 | | |
| | 24 | 729 | | jumpModule.FrameJumpDirection = Vector3d.Slerp( |
| | 24 | 730 | | Vector3d.Up, |
| | 24 | 731 | | CurrentState.SurfaceNormal, |
| | 24 | 732 | | jumpModule.PerpendicularJumpAmount); |
| | 24 | 733 | | } |
| | | 734 | | |
| | | 735 | | private void CommitJumpForce(Vector3d jumpForce) |
| | | 736 | | { |
| | 32 | 737 | | if (IsClimbing) |
| | 2 | 738 | | StopClimb(wasForced: false); |
| | | 739 | | |
| | 32 | 740 | | JumpLocomotion jumpModule = JumpModule!; |
| | | 741 | | |
| | 32 | 742 | | jumpModule.RegisterJump(); |
| | | 743 | | |
| | | 744 | | // Remove any existing downward force before the jump impulse is applied. |
| | 32 | 745 | | _forceOutput.Y = FixedMath.Max(Fixed64.Zero, _forceOutput.Y); |
| | 32 | 746 | | _forceOutput += jumpForce; |
| | 32 | 747 | | } |
| | | 748 | | |
| | | 749 | | private void UpdateClimbState(TrekRequest request) |
| | | 750 | | { |
| | 2184 | 751 | | if (ClimbModule?.IsEnabled != true) |
| | 10 | 752 | | return; |
| | | 753 | | |
| | 2174 | 754 | | if (ClimbModule.IsMantling) |
| | 11 | 755 | | return; |
| | | 756 | | |
| | 2163 | 757 | | bool canAttemptClimb = request.IsRequestingClimb |
| | 2163 | 758 | | && ClimbModule.CanClimb |
| | 2163 | 759 | | && !IsInLiquid |
| | 2163 | 760 | | && CurrentState.Medium != TraversalMedium.Unknown; |
| | 2163 | 761 | | if (!canAttemptClimb) |
| | | 762 | | { |
| | 2088 | 763 | | if (ClimbModule.IsClimbing) |
| | 2 | 764 | | StopClimb(wasForced: false); |
| | | 765 | | |
| | 2088 | 766 | | return; |
| | | 767 | | } |
| | | 768 | | |
| | 75 | 769 | | if (ClimbModule.ClimbResolver == null |
| | 75 | 770 | | || !ClimbModule.ClimbResolver.TryResolveClimbAffordance(request, CurrentState, out ClimbAffordanceSnapshot s |
| | | 771 | | { |
| | 15 | 772 | | if (ClimbModule.IsClimbing) |
| | 1 | 773 | | StopClimb(wasForced: true); |
| | | 774 | | |
| | 15 | 775 | | return; |
| | | 776 | | } |
| | | 777 | | |
| | 60 | 778 | | if (ClimbModule.IsClimbing) |
| | | 779 | | { |
| | 25 | 780 | | bool canContinue = snapshot.CanContinueClimb |
| | 25 | 781 | | && IsCompatibleClimbAffordance(snapshot); |
| | 25 | 782 | | if (!canContinue) |
| | | 783 | | { |
| | 4 | 784 | | StopClimb(wasForced: true); |
| | 4 | 785 | | return; |
| | | 786 | | } |
| | | 787 | | |
| | 21 | 788 | | ClimbModule.ApplyClimbSnapshot(snapshot); |
| | | 789 | | |
| | 21 | 790 | | if (ShouldStartMantle(request, snapshot)) |
| | 9 | 791 | | StartMantle(snapshot); |
| | | 792 | | |
| | 21 | 793 | | return; |
| | | 794 | | } |
| | | 795 | | |
| | 35 | 796 | | bool canStart = snapshot.CanStartClimb; |
| | 35 | 797 | | if (!canStart) |
| | 1 | 798 | | return; |
| | | 799 | | |
| | 34 | 800 | | StartClimb(snapshot); |
| | 34 | 801 | | } |
| | | 802 | | |
| | | 803 | | private bool IsCompatibleClimbAffordance(ClimbAffordanceSnapshot snapshot) |
| | | 804 | | { |
| | 24 | 805 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 806 | | |
| | 24 | 807 | | if (climbModule.AttachmentId.HasValue && snapshot.AffordanceId.HasValue) |
| | 19 | 808 | | return climbModule.AttachmentId.Value == snapshot.AffordanceId.Value; |
| | | 809 | | |
| | 5 | 810 | | if (snapshot.Kind != climbModule.ActiveClimbKind) |
| | 1 | 811 | | return false; |
| | | 812 | | |
| | 4 | 813 | | if (!HasCompatibleClimbAxes(snapshot)) |
| | 2 | 814 | | return false; |
| | | 815 | | |
| | 2 | 816 | | Fixed64 tolerance = GetClimbContinuityTolerance(); |
| | 2 | 817 | | return (snapshot.AttachmentPoint - climbModule.AttachmentPoint).MagnitudeSquared <= tolerance * tolerance; |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | private bool HasCompatibleClimbAxes(ClimbAffordanceSnapshot snapshot) |
| | | 821 | | { |
| | 4 | 822 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 823 | | |
| | 4 | 824 | | if (climbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 4 | 825 | | && snapshot.SurfaceNormal != Vector3d.Zero |
| | 4 | 826 | | && Vector3d.Dot(climbModule.AttachedSurfaceNormal.Normalized, snapshot.SurfaceNormal.Normalized) <= Fixed64. |
| | | 827 | | { |
| | 1 | 828 | | return false; |
| | | 829 | | } |
| | | 830 | | |
| | 3 | 831 | | if (climbModule.AttachedUpDirection != Vector3d.Zero |
| | 3 | 832 | | && snapshot.UpDirection != Vector3d.Zero |
| | 3 | 833 | | && Vector3d.Dot(climbModule.AttachedUpDirection.Normalized, snapshot.UpDirection.Normalized) <= Fixed64.Zero |
| | | 834 | | { |
| | 1 | 835 | | return false; |
| | | 836 | | } |
| | | 837 | | |
| | 2 | 838 | | return true; |
| | | 839 | | } |
| | | 840 | | |
| | | 841 | | private Fixed64 GetClimbContinuityTolerance() |
| | | 842 | | { |
| | 2 | 843 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 844 | | |
| | 2 | 845 | | Fixed64 frameTravelAllowance = climbModule.MaxClimbSpeed * DeltaTime; |
| | 2 | 846 | | return climbModule.ClimbStartTolerance + frameTravelAllowance; |
| | | 847 | | } |
| | | 848 | | |
| | | 849 | | private void StartClimb(ClimbAffordanceSnapshot snapshot) |
| | | 850 | | { |
| | 34 | 851 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 852 | | |
| | 34 | 853 | | climbModule.ApplyClimbSnapshot(snapshot); |
| | 34 | 854 | | climbModule.IsClimbing = true; |
| | 34 | 855 | | climbModule.IsMantling = false; |
| | | 856 | | |
| | 34 | 857 | | if (IsFalling) |
| | 1 | 858 | | Handler.ClearTransientState<FallLocomotion>(); |
| | | 859 | | |
| | 34 | 860 | | if (JumpModule != null) |
| | | 861 | | { |
| | 33 | 862 | | JumpModule.IsJumping = false; |
| | 33 | 863 | | JumpModule.IsHoldingJump = false; |
| | | 864 | | } |
| | | 865 | | |
| | 34 | 866 | | if (FlyModule != null) |
| | 33 | 867 | | FlyModule.IsFlying = false; |
| | | 868 | | |
| | 34 | 869 | | if (SlideModule != null) |
| | 33 | 870 | | SlideModule.IsSliding = false; |
| | | 871 | | |
| | 34 | 872 | | Events.OnStartClimb?.Invoke(snapshot); |
| | 3 | 873 | | } |
| | | 874 | | |
| | | 875 | | private bool ShouldStartMantle(TrekRequest request, ClimbAffordanceSnapshot snapshot) |
| | | 876 | | { |
| | 21 | 877 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 878 | | |
| | 21 | 879 | | if (snapshot.Kind != ClimbAffordanceKind.Ledge |
| | 21 | 880 | | || !climbModule.ActiveAllowMantle |
| | 21 | 881 | | || request.Rate == TrekRate.Stationary) |
| | | 882 | | { |
| | 11 | 883 | | return false; |
| | | 884 | | } |
| | | 885 | | |
| | 10 | 886 | | Vector3d upAxis = climbModule.AttachedUpDirection != Vector3d.Zero |
| | 10 | 887 | | ? climbModule.AttachedUpDirection.Normalized |
| | 10 | 888 | | : Vector3d.Up; |
| | 10 | 889 | | return Vector3d.Dot(request.Direction, upAxis) > Fixed64.Zero; |
| | | 890 | | } |
| | | 891 | | |
| | | 892 | | private void StartMantle(ClimbAffordanceSnapshot snapshot) |
| | | 893 | | { |
| | 9 | 894 | | ClimbLocomotion climbModule = ClimbModule!; |
| | | 895 | | |
| | 9 | 896 | | climbModule.ApplyClimbSnapshot(snapshot); |
| | 9 | 897 | | climbModule.IsMantling = true; |
| | 9 | 898 | | Events.OnStartMantle?.Invoke(); |
| | 1 | 899 | | } |
| | | 900 | | |
| | | 901 | | private void UpdateFlightState(TrekRequest request) |
| | | 902 | | { |
| | 2184 | 903 | | if (FlyModule?.IsEnabled != true) |
| | 10 | 904 | | return; |
| | | 905 | | |
| | 2174 | 906 | | bool shouldFly = request.IsRequestingFlight |
| | 2174 | 907 | | && FlyModule.CanFly |
| | 2174 | 908 | | && !IsClimbing |
| | 2174 | 909 | | && !IsInLiquid |
| | 2174 | 910 | | && CurrentState.Medium != TraversalMedium.Unknown; |
| | | 911 | | |
| | 2174 | 912 | | if (!shouldFly) |
| | | 913 | | { |
| | 2163 | 914 | | FlyModule.IsFlying = false; |
| | 2163 | 915 | | return; |
| | | 916 | | } |
| | | 917 | | |
| | 11 | 918 | | FlyModule.IsFlying = true; |
| | | 919 | | |
| | 11 | 920 | | if (IsFalling) |
| | 1 | 921 | | Handler.ClearTransientState<FallLocomotion>(); |
| | | 922 | | |
| | 11 | 923 | | if (JumpModule != null) |
| | | 924 | | { |
| | 10 | 925 | | JumpModule.IsJumping = false; |
| | 10 | 926 | | JumpModule.IsHoldingJump = false; |
| | | 927 | | } |
| | | 928 | | |
| | 11 | 929 | | if (SlideModule != null) |
| | 10 | 930 | | SlideModule.IsSliding = false; |
| | 11 | 931 | | } |
| | | 932 | | |
| | | 933 | | private void UpdateSwimState(TrekRequest request) |
| | | 934 | | { |
| | 2184 | 935 | | WaterLocomotion? waterModule = WaterModule; |
| | 2184 | 936 | | if (waterModule?.IsEnabled != true) |
| | 12 | 937 | | return; |
| | | 938 | | |
| | 2172 | 939 | | waterModule.RequestedSwimThisTraversal = request.IsRequestingSwim; |
| | 2172 | 940 | | waterModule.IsSwimming = IsInLiquid |
| | 2172 | 941 | | && waterModule.CanSwim |
| | 2172 | 942 | | && request.IsRequestingSwim; |
| | 2172 | 943 | | } |
| | | 944 | | |
| | | 945 | | #endregion |
| | | 946 | | |
| | | 947 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 948 | | private Vector3d ResolveTraversalVelocityDelta() |
| | | 949 | | { |
| | 2184 | 950 | | return _forceOutput != Vector3d.Zero |
| | 2184 | 951 | | ? _forceOutput * DeltaTime |
| | 2184 | 952 | | : Vector3d.Zero; |
| | | 953 | | } |
| | | 954 | | |
| | | 955 | | private void ResolvePlatformTraversal( |
| | | 956 | | TrekRequest request, |
| | | 957 | | ref Vector3d positionDelta, |
| | | 958 | | ref FixedQuaternion rotationDelta) |
| | | 959 | | { |
| | 2184 | 960 | | PlatformLocomotion platformModule = PlatformModule; |
| | | 961 | | |
| | | 962 | | // Do not apply platform movement if we just jumped or if the platform is not actively driving us. |
| | 2184 | 963 | | bool isMovingWithPlatform = platformModule.IsActive |
| | 2184 | 964 | | && !IsFlying |
| | 2184 | 965 | | && !IsClimbing |
| | 2184 | 966 | | && (IsOnSolid || platformModule.IsLockedToPlatform); |
| | 2184 | 967 | | if (!isMovingWithPlatform || IsJumping) |
| | 1981 | 968 | | return; |
| | | 969 | | |
| | 203 | 970 | | platformModule.GetPlatformInfluence( |
| | 203 | 971 | | request.FootPosition ?? request.Origin, |
| | 203 | 972 | | request.Rotation, |
| | 203 | 973 | | out positionDelta, |
| | 203 | 974 | | out rotationDelta); |
| | 203 | 975 | | } |
| | | 976 | | |
| | | 977 | | #endregion |
| | | 978 | | } |