ulvis.paste.net

Paste Search Dynamic
Recent pastes
ApplyVelocityBraking
  1. void UFAMovementComponent::ApplyVelocityBraking(float DeltaTime, float Friction, float BrakingDeceleration) {
  2.  
  3.     //Added
  4.     float Speed = Velocity.Size2D();
  5.  
  6.  
  7.     if (Velocity.IsZero() || !HasValidData() || HasAnimRootMotion() || DeltaTime < MIN_TICK_TIME)
  8.     {
  9.         return;
  10.     }
  11.  
  12.     const float FrictionFactor = FMath::Max(0.f, BrakingFrictionFactor);
  13.     Friction = FMath::Max(0.f, Friction * FrictionFactor);
  14.     // Added
  15.     BrakingDeceleration = FMath::Max(BrakingDeceleration, Speed);
  16.  
  17.     BrakingDeceleration = FMath::Max(0.f, BrakingDeceleration);
  18.  
  19.  
  20.     const bool bZeroFriction = (Friction == 0.f);
  21.     const bool bZeroBraking = (BrakingDeceleration == 0.f);
  22.  
  23.     if (bZeroFriction && bZeroBraking)
  24.     {
  25.         return;
  26.     }
  27.  
  28.     const FVector OldVel = Velocity;
  29.  
  30.     // subdivide braking to get reasonably consistent results at lower frame rates
  31.     // (important for packet loss situations w/ networking)
  32.     float RemainingTime = DeltaTime;
  33.     const float MaxTimeStep = FMath::Clamp(BrakingSubStepTime, 1.0f / 75.0f, 1.0f / 20.0f);
  34.  
  35.     // Decelerate to brake to a stop
  36.     const FVector RevAccel = (bZeroBraking ? FVector::ZeroVector : (-BrakingDeceleration * Velocity.GetSafeNormal()));
  37.     while (RemainingTime >= MIN_TICK_TIME)
  38.     {
  39.         // Zero friction uses constant deceleration, so no need for iteration.
  40.         const float dt = ((RemainingTime > MaxTimeStep && !bZeroFriction) ? FMath::Min(MaxTimeStep, RemainingTime * 0.5f) : RemainingTime);
  41.         RemainingTime -= dt;
  42.  
  43.         // apply friction and braking
  44.         Velocity = Velocity + ((-Friction) * Velocity + RevAccel) * dt;
  45.  
  46.         // Don't reverse direction
  47.         if ((Velocity | OldVel) <= 0.f)
  48.         {
  49.             Velocity = FVector::ZeroVector;
  50.             return;
  51.         }
  52.     }
  53.  
  54.     // Clamp to zero if nearly zero, or if below min threshold and braking.
  55.     const float VSizeSq = Velocity.SizeSquared();
  56.     if (VSizeSq <= KINDA_SMALL_NUMBER || (!bZeroBraking && VSizeSq <= FMath::Square(BRAKE_TO_STOP_VELOCITY)))
  57.     {
  58.         Velocity = FVector::ZeroVector;
  59.     }
  60.  
Parsed in 0.014 seconds