Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created September 24, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/7bf12b4f913c85b565143c6a7d95e1dd to your computer and use it in GitHub Desktop.
Save MattRix/7bf12b4f913c85b565143c6a7d95e1dd to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackwardEulerController : MonoBehaviour
{
public float gain = 200; //200-700;
public float damp = 20; //20-100;
public Transform target = null;
Vector3 velocity = Vector3.zero;
void Update()
{
transform.position = Integrate(target.position,transform.position, ref velocity,gain,damp, Time.deltaTime);
}
static public Vector3 Integrate(Vector3 target, Vector3 current, ref Vector3 velocity, float gain, float damp, float dt)
{
float g = damp * dt + gain * dt * dt;
g = 1.0f / (1.0f + g);
float _gain = gain * g;
float _damp = (damp + gain * dt) * g;
Vector3 diff = target - current;
Vector3 force = diff * _gain - velocity * _damp;
velocity += velocity + force * dt;
return current + velocity * dt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment