melonJS
    Preparing search index...

    Function damp

    • Frame-rate-independent exponential damping toward a target.

      After total elapsed time t_total (in seconds), the result satisfies result = current + (target - current) * (1 - exp(-lambda * t_total)) regardless of how t_total was split across dt calls. Same algorithm as THREE.MathUtils.damp in Three.js.

      lambda is the decay rate in 1/seconds — higher = snappier convergence. A rule of thumb: lambda = 5 reaches ≈ 99% of the target in one second; lambda = 10 reaches ≈ 99% in ~0.5 s.

      Use this — NOT lerp(current, target, alpha) per frame — whenever the smoothing should feel the same regardless of frame rate (camera follow, value tracking, input smoothing).

      Parameters

      • current: number

        the current value (carries forward across frames)

      • target: number

        the value to approach

      • lambda: number

        decay rate in 1/seconds. Higher = snappier.

      • dt: number

        delta time in seconds (note: melonJS engine dt is in ms; divide by 1000)

      Returns number

      the new value, one step closer to target

      // frame-rate independent smooth follow:
      cameraX = math.damp(cameraX, target.x, 5, dt / 1000);