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 { namespace RealityReboot.jelycho.Player {
public class CameraSpring : MonoBehaviour { public class CameraSpring : MonoBehaviour {
@@ -18,12 +19,12 @@ namespace RealityReboot.jelycho.Player {
} }
public void UpdateSpring(float deltaTime, Vector3 up, Vector3 right, Vector3 forward) { public void UpdateSpring(float deltaTime, Vector3 up, Vector3 right, Vector3 forward) {
Spring(ref m_SpringPosition, MathUtility.Spring(ref m_SpringPosition,
ref m_SpringVelocity, ref m_SpringVelocity,
transform.position, transform.position,
m_HalfLife, m_HalfLife,
m_Frequency, m_Frequency,
deltaTime); deltaTime);
Vector3 localSpringPosition = m_SpringPosition - transform.position; Vector3 localSpringPosition = m_SpringPosition - transform.position;
float springHeight = Vector3.Dot(localSpringPosition, up); float springHeight = Vector3.Dot(localSpringPosition, up);
@@ -35,25 +36,5 @@ namespace RealityReboot.jelycho.Player {
0.0f, 0.0f,
springSideways * m_SidewaysAngularDisplacement); 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;
}
} }
} }

View File

@@ -52,7 +52,7 @@ namespace RealityReboot.jelycho.Player {
} }
Vector2 lookInput = SensitivityReader.Read(m_Config.lookActionReference.action); Vector2 lookInput = SensitivityReader.Read(m_Config.lookActionReference.action);
m_FPPActor.Look(lookInput * FPPConfig.s_MouseSensitivity.FloatValue); m_FPPActor.Look(lookInput * FPPConfig.MouseSensitivity.FloatValue);
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>(); Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
m_FPPActor.SetMoveInput(moveInput); m_FPPActor.SetMoveInput(moveInput);
@@ -74,7 +74,7 @@ namespace RealityReboot.jelycho.Player {
[Serializable] [Serializable]
public class Config { public class Config {
[FormerlySerializedAs("fppActorPrefab")] public AssetReferenceT<GameObject> playerActorPrefab; public AssetReferenceT<GameObject> playerActorPrefab;
public InputActionReference moveActionReference; public InputActionReference moveActionReference;
public InputActionReference lookActionReference; public InputActionReference lookActionReference;
@@ -87,9 +87,9 @@ namespace RealityReboot.jelycho.Player {
} }
public static class FPPConfig { public static class FPPConfig {
[ConfigVar("fpp.mouse.sens", 0.25f, "Mouse look sensitivity")] public static ConfigVar s_MouseSensitivity; [ConfigVar("fpp.mouse.sens", 0.25f, "Mouse look sensitivity")] public static ConfigVar MouseSensitivity;
[ConfigVar("fpp.mouse.invert.y", 0, "Invert mouse look")] public static ConfigVar s_MouseInvertY; [ConfigVar("fpp.mouse.invert.y", 0, "Invert mouse look")] public static ConfigVar MouseInvertY;
[ConfigVar("fpp.gamepad.sens", 4.0f, "Gamepad look sensitivity")] public static ConfigVar s_GamepadSensitivity; [ConfigVar("fpp.gamepad.sens", 4.0f, "Gamepad look sensitivity")] public static ConfigVar GamepadSensitivity;
} }
public static class SensitivityReader { public static class SensitivityReader {
@@ -103,11 +103,11 @@ namespace RealityReboot.jelycho.Player {
bool isGamepad = action.activeControl.device is Gamepad; bool isGamepad = action.activeControl.device is Gamepad;
if (isGamepad) { if (isGamepad) {
sensitivity *= FPPConfig.s_GamepadSensitivity.FloatValue; sensitivity *= FPPConfig.GamepadSensitivity.FloatValue;
} else { } else {
sensitivity *= FPPConfig.s_MouseSensitivity.FloatValue; sensitivity *= FPPConfig.MouseSensitivity.FloatValue;
if (FPPConfig.s_MouseInvertY.IndexValue == 1) { if (FPPConfig.MouseInvertY.IndexValue == 1) {
sensitivity.y *= -1; sensitivity.y *= -1;
} }
} }

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c47ef0a0dcfbe6d4aa3b23261a96f53f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,5 @@
{
"name": "controls_jelycho",
"maps": [],
"controlSchemes": []
}

View File

@@ -0,0 +1,14 @@
fileFormatVersion: 2
guid: bf6c2a363dbaef244afe38b9ff9fecc5
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3}
generateWrapperCode: 0
wrapperCodePath:
wrapperClassName:
wrapperCodeNamespace: