RealDocs

FMath::CriticallyDampedSmoothing

function Core Since 4.20
#include "Math/UnrealMathUtility.h"
Access: public Specifiers: staticconstexpr

Description

Smooths a value using a critically-damped spring, tracking both position and velocity. Reaches the target without overshoot and correctly handles a moving target by anticipating its velocity.

Caveats & Gotchas

  • The approximation is accurate only when InDeltaTime < 0.5 * InSmoothingTime. At larger delta times (e.g. from a frame spike) the result remains well-behaved but less accurate.
  • InOutValueRate must be stored between frames and passed back each call — it is not a scratch value. Zeroing it every frame will cause jerky motion.
  • If InTargetValueRate is discontinuous (e.g. target suddenly changes velocity), the output velocity will also be discontinuous that frame. Smooth the target velocity separately if that's unacceptable.

Signature

template< class T >
static constexpr void CriticallyDampedSmoothing(T& InOutValue, T& InOutValueRate, const T& InTargetValue, const T& InTargetValueRate, const float InDeltaTime, const float InSmoothingTime)

Parameters

Name Type Description Default
InOutValue T& The value to be smoothed. Modified in place.
InOutValueRate T& The rate of change of the value. Modified in place — persist this between frames.
InTargetValue const T& The target to smooth towards.
InTargetValueRate const T& The target rate of change. Pass zero if target is stationary.
InDeltaTime const float Elapsed time since last update.
InSmoothingTime const float Timescale over which to smooth. Larger values result in more smoothed behaviour. Can be zero.

Return Type

void

Example

Smooth camera arm length with velocity tracking C++
// In header:
float CurrentArmLength = 300.f;
float ArmLengthRate = 0.f;

// In Tick:
FMath::CriticallyDampedSmoothing(CurrentArmLength, ArmLengthRate, TargetArmLength, 0.f, DeltaTime, 0.15f);
SpringArm->TargetArmLength = CurrentArmLength;

Version History

Introduced in: 4.20

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.