added spring math

This commit is contained in:
2025-06-07 14:42:35 +02:00
parent e534ea7596
commit f8fbe10c56
2 changed files with 47 additions and 0 deletions

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 487254dcb0ff429b8a065dcbc91b614a
timeCreated: 1749299515