Skip to content

Commit 2829889

Browse files
authored
feat: [Math] - Exponential damping (#2461)
* Exponential damping * ExpDecay double precision overload * Flip arguments
1 parent 763a529 commit 2829889

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

sources/core/Stride.Core.Mathematics/MathUtil.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,5 +662,37 @@ public static float Mod(float value, float divisor)
662662
{
663663
return ((value % divisor) + divisor) % divisor;
664664
}
665+
666+
/// <summary>
667+
/// Exponential damping.
668+
/// Alternative to
669+
/// <code>a = lerp(a, b, damping * dt)</code>
670+
/// using the exponential function flipped around the Y axis: e^(-t)
671+
/// </summary>
672+
/// <param name="a">Initial value</param>
673+
/// <param name="b">Plateau value</param>
674+
/// <param name="lambda">Damping</param>
675+
/// <param name="dt">Discrete time unit, delta time.</param>
676+
/// <returns>A value interpolated from a to b depending on lambda and dt.</returns>
677+
public static float ExpDecay(float a, float b, float lambda, float dt)
678+
{
679+
return b + (a - b) * MathF.Exp(-lambda * dt);
680+
}
681+
682+
/// <summary>
683+
/// Exponential damping.
684+
/// Alternative to
685+
/// <code>a = lerp(a, b, damping * dt)</code>
686+
/// using the exponential function flipped around the Y axis: e^(-t)
687+
/// </summary>
688+
/// <param name="a">Initial value</param>
689+
/// <param name="b">Plateau value</param>
690+
/// <param name="lambda">Damping</param>
691+
/// <param name="dt">Discrete time unit, delta time.</param>
692+
/// <returns>A value interpolated from a to b depending on lambda and dt.</returns>
693+
public static double ExpDecay(double a, double b, double lambda, double dt)
694+
{
695+
return b + (a - b) * Math.Exp(-lambda * dt);
696+
}
665697
}
666698
}

sources/engine/Stride.Rendering/Rendering/Utils/Math.sdsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,10 @@ shader Math
114114
vecProjected.xyz /= vecProjected.w;
115115
return vecProjected;
116116
}
117+
118+
//Exponential damping
119+
float ExpDecay(float a, float b, float lambda, float dt)
120+
{
121+
return b + (a - b) * exp(-lambda * dt);
122+
}
117123
};

0 commit comments

Comments
 (0)