diff --git a/Runtime/Engine/Code/Foundation/MathUtility.cs b/Runtime/Engine/Code/Foundation/MathUtility.cs new file mode 100644 index 0000000..9cabd84 --- /dev/null +++ b/Runtime/Engine/Code/Foundation/MathUtility.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +namespace RebootKit.Engine.Foundation { + public static class MathUtility { + + // Spring math + // Source: https://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/ + public static void Spring(ref float current, + ref float velocity, + float target, + float halfLife, + float frequency, + float timeStep) { + float dampingRatio = -Mathf.Log(0.5f) / (frequency * halfLife); + float f = 1.0f + 2.0f * timeStep * dampingRatio * frequency; + float oo = frequency * frequency; + float hoo = timeStep * oo; + float hhoo = timeStep * hoo; + float detInv = 1.0f / (f + hhoo); + float detX = f * current + timeStep * velocity + hhoo * target; + float detV = velocity + hoo * (target - current); + current = detX * detInv; + velocity = detV * detInv; + } + + public static void Spring(ref Vector3 current, + ref Vector3 velocity, + Vector3 target, + float halfLife, + float frequency, + float timeStep) { + float dampingRatio = -Mathf.Log(0.5f) / (frequency * halfLife); + float f = 1.0f + 2.0f * timeStep * dampingRatio * frequency; + float oo = frequency * frequency; + float hoo = timeStep * oo; + float hhoo = timeStep * hoo; + float detInv = 1.0f / (f + hhoo); + Vector3 detX = f * current + timeStep * velocity + hhoo * target; + Vector3 detV = velocity + hoo * (target - current); + current = detX * detInv; + velocity = detV * detInv; + } + } +} \ No newline at end of file diff --git a/Runtime/Engine/Code/Foundation/MathUtility.cs.meta b/Runtime/Engine/Code/Foundation/MathUtility.cs.meta new file mode 100644 index 0000000..6643a45 --- /dev/null +++ b/Runtime/Engine/Code/Foundation/MathUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 487254dcb0ff429b8a065dcbc91b614a +timeCreated: 1749299515 \ No newline at end of file