moved spring math to rebootkit

This commit is contained in:
2025-06-07 14:42:50 +02:00
parent f258d19a31
commit a8cdd143ca
6 changed files with 44 additions and 36 deletions

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using RebootKit.Engine.Foundation;
using UnityEngine;
namespace RealityReboot.jelycho.Player {
public class CameraSpring : MonoBehaviour {
@@ -18,12 +19,12 @@ namespace RealityReboot.jelycho.Player {
}
public void UpdateSpring(float deltaTime, Vector3 up, Vector3 right, Vector3 forward) {
Spring(ref m_SpringPosition,
ref m_SpringVelocity,
transform.position,
m_HalfLife,
m_Frequency,
deltaTime);
MathUtility.Spring(ref m_SpringPosition,
ref m_SpringVelocity,
transform.position,
m_HalfLife,
m_Frequency,
deltaTime);
Vector3 localSpringPosition = m_SpringPosition - transform.position;
float springHeight = Vector3.Dot(localSpringPosition, up);
@@ -35,25 +36,5 @@ namespace RealityReboot.jelycho.Player {
0.0f,
springSideways * m_SidewaysAngularDisplacement);
}
// Source: https://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/
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;
}
}
}