Skip to content

Instantly share code, notes, and snippets.

@joonatansir
Last active December 20, 2020 20:12
Show Gist options
  • Save joonatansir/844b50c0dbf42077690939f0a7430cf8 to your computer and use it in GitHub Desktop.
Save joonatansir/844b50c0dbf42077690939f0a7430cf8 to your computer and use it in GitHub Desktop.
using UnityEngine;
public static class Utils
{
/*
* x - value
* v - velocity
* xt - target value
* zeta - damping ratio
* omega - oscillation frequency
* h - timestep
*/
public static void Spring(ref float x, ref float v, float xt, float zeta, float omega, float h)
{
var f = 1.0f + 2.0f * h * zeta * omega;
var oo = omega * omega;
var hhoo = h * h * oo;
var d = 1.0f / (f + hhoo);
var numX = f * x + h * v + hhoo * xt;
var numV = v + h * oo * (xt - x);
x = numX * d;
v = numV * d;
}
public static void Spring(ref Vector2 x, ref Vector2 v, Vector2 xt, float zeta, float omega, float h)
{
var f = 1.0f + 2.0f * h * zeta * omega;
var oo = omega * omega;
var hhoo = h * h * oo;
var d = 1.0f / (f + hhoo);
var numX = f * x + h * v + hhoo * xt;
var numV = v + h * oo * (xt - x);
x = numX * d;
v = numV * d;
}
public static void Spring(ref Vector3 x, ref Vector3 v, Vector3 xt, float zeta, float omega, float h)
{
var f = 1.0f + 2.0f * h * zeta * omega;
var oo = omega * omega;
var hhoo = h * h * oo;
var d = 1.0f / (f + hhoo);
var numX = f * x + h * v + hhoo * xt;
var numV = v + h * oo * (xt - x);
x = numX * d;
v = numV * d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment