Skip to content

Instantly share code, notes, and snippets.

@thygrrr
Last active November 6, 2024 10:07
Show Gist options
  • Save thygrrr/b40dfe7993e2c6a89d91f718230af44d to your computer and use it in GitHub Desktop.
Save thygrrr/b40dfe7993e2c6a89d91f718230af44d to your computer and use it in GitHub Desktop.
C# Damping for Godot et al
//SPDX-License-Identifier: MIT
using System.Runtime.CompilerServices;
using Godot;
using Vector3 = System.Numerics.Vector3;
namespace Jovian.Math
{
public static class Damp
{
public static void Smooth(ref double x, ref double v, double x_goal, double v_goal, double lambda, double dt)
{
if (lambda <= 0)
{
x = x_goal;
v = v_goal;
return;
}
var g = x_goal;
var q = v_goal;
var d = lambda_to_damping(lambda);
var c = g + d * q / (d * d / 4.0);
var y = d / 2.0;
var j0 = x - c;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + c;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref float x, ref float v, float x_goal, float v_goal, float lambda, float dt)
{
if (lambda <= 0)
{
x = x_goal;
v = v_goal;
return;
}
var g = x_goal;
var q = v_goal;
var d = lambda_to_damping(lambda);
var c = g + d * q / (d * d / 4.0f);
var y = d / 2.0f;
var j0 = x - c;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + c;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Vector3 x, ref Vector3 v, Vector3 x_goal, Vector3 v_goal, float lambda, float dt)
{
if (lambda <= 0)
{
x = x_goal;
v = v_goal;
return;
}
var g = x_goal;
var q = v_goal;
var d = lambda_to_damping(lambda);
var c = g + d * q / (d * d / 4.0f);
var y = d / 2.0f;
var j0 = x - c;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + c;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Godot.Vector3 x, ref Godot.Vector3 v, Godot.Vector3 x_goal, Godot.Vector3 v_goal, float lambda, float dt)
{
if (lambda <= 0)
{
x = x_goal;
v = v_goal;
return;
}
var g = x_goal;
var q = v_goal;
var d = lambda_to_damping(lambda);
var c = g + d * q / (d * d / 4.0f);
var y = d / 2.0f;
var j0 = x - c;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + c;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Vector3D x, ref Vector3D v, Vector3D x_goal, Vector3D v_goal, double lambda, double dt)
{
if (lambda <= 0)
{
x = x_goal;
v = v_goal;
return;
}
var g = x_goal;
var q = v_goal;
var d = lambda_to_damping(lambda);
var c = g + q * d / (d * d / 4.0);
var y = d / 2.0;
var j0 = x - c;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + c;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref double x, ref double v, double x_goal, double lambda, double dt)
{
if (lambda <= 0)
{
x = x_goal;
v = 0;
return;
}
var y = lambda_to_damping(lambda) / 2.0;
var j0 = x - x_goal;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + x_goal;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref float x, ref float v, float x_goal, float lambda, float dt)
{
if (lambda <= 0)
{
x = x_goal;
v = 0;
return;
}
var y = lambda_to_damping(lambda) / 2.0f;
var j0 = x - x_goal;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + x_goal;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Vector3 x, ref Vector3 v, Vector3 x_goal, float lambda, float dt)
{
if (lambda <= 0)
{
x = x_goal;
v = Vector3.Zero;
return;
}
var y = lambda_to_damping(lambda) / 2.0f;
var j0 = x - x_goal;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + x_goal;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Godot.Vector3 x, ref Godot.Vector3 v, Godot.Vector3 x_goal, float lambda, float dt)
{
var y = lambda_to_damping(lambda) / 2.0f;
var j0 = x - x_goal;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + x_goal;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Vector3D x, ref Vector3D v, Vector3D x_goal, double lambda, double dt)
{
var y = lambda_to_damping(lambda) / 2.0;
var j0 = x - x_goal;
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = ey_dt * (j0 + j1 * dt) + x_goal;
v = ey_dt * (v - j1 * y * dt);
}
public static void Smooth(ref Godot.Quaternion x, ref Godot.Vector3 v, Quaternion x_goal, float lambda, float dt)
{
var y = lambda_to_damping(lambda) / 2.0f;
//Compensate for double cover of quaternions: q and -q represent the same rotation
var dot = x.Dot(x_goal);
var multi = dot > 0f ? 1f : -1f;
x_goal *= multi;
var j0 = Qx.ToScaledAngleAxis(x * x_goal.Inverse());
var j1 = v + j0 * y;
var ey_dt = fast_neg_exp(y * dt);
x = Qx.FromScaledAngleAxis(ey_dt * (j0 + j1 * dt)) * x_goal;
v = ey_dt * (v - j1 * y * dt);
}
private const double Ln2 = 0.6931471805599453094172321214581765680755001343602552541206800094d;
private const float Ln2F = 0.693147180559945f;
#region Helper Maths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float lambda_to_damping(float lambda)
{
return 4.0f * Ln2F / lambda;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double lambda_to_damping(double lambda)
{
return 4.0 * Ln2 / lambda;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float fast_neg_exp(float x)
{
return 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double fast_neg_exp(double x)
{
return 1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x);
// Better for x element [0.5..1]: (our case is most often x < 0.5, usually 0.15 to 0.05-ish)
// return 1.0 / (1.0 + 1.006722 * x + 0.453568 * x * x + 0.254559 * x * x * x);
}
#endregion
}
}
/*
MIT License
Copyright (c) 2023 tiger.blue
Modified and ported to Godot and System.Numerics, with float/double precision.
Changed core use case, which is SmoothDamp / Critical with and without goal velocity,
for float, double, Vector2, Vector3, Vector3D, and Quaternions
Copyright (c) 2021 Daniel Holden, Meta
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment