Rewritten camera to follow cursor

This commit is contained in:
Marco 2025-05-07 11:36:03 +02:00
commit 869a3b4c06
7 changed files with 232 additions and 9 deletions

View file

@ -1,4 +1,5 @@
using Godot;
using System;
using Godot;
namespace Cirno.Scripts.Utils;
@ -29,4 +30,35 @@ public static class MathFunctions
return targetPos + targetVel * t;
}
// Critically damped spring, based on Game Programming Gems 4 Chapter 1.10. https://archive.org/details/game-programming-gems-4/page/95/mode/2up
// Returns a 2-tuple of [next_position, next_velocity].
public static Tuple<float, float> SmoothDamp(float current, float target, float currentVelocity, float smoothTime, float maxSpeed, float delta)
{
smoothTime = MathF.Max(smoothTime, 0.0001f);
var omega = 2.0f / smoothTime;
var x = omega * delta;
var xExp = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);
var change = current - target;
var originalTarget = target;
// Clamp max speed
var maxChange = maxSpeed * smoothTime;
change = Math.Clamp(change, -maxChange, maxChange);
target = current - change;
var temp = (currentVelocity + omega * change) * delta;
currentVelocity = (currentVelocity - omega * temp) * xExp;
var output = target + (change + temp) * xExp;
// Prevent Overshooting
if ((originalTarget - current > 0.0) == (output > originalTarget))
{
output = originalTarget;
currentVelocity = (output - originalTarget) / delta;
}
return new Tuple<float, float>(output, currentVelocity);
}
}