Last active
April 26, 2019 16:58
-
-
Save sketchpunk/e3d70095962795f25286321de58a307c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/ | |
//https://jsfiddle.net/bkanber/pDngH/ | |
//https://github.com/pqml/spring | |
//http://lolengine.net/blog/2015/05/03/damping-with-delta-time | |
//https://github.com/keijiro/SmoothingTest | |
//https://gafferongames.com/post/spring_physics/ | |
//https://stackoverflow.com/questions/5100811/algorithm-to-control-acceleration-until-a-position-is-reached | |
// | |
//http://lolengine.net/blog/2015/05/03/damping-with-delta-time | |
/* | |
http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ | |
Damping from A to B. | |
Smoothing rate dictates the proportion of source remaining after one second | |
public static float Damp(float source, float target, float smoothing, float dt) | |
{ | |
return Mathf.Lerp(source, target, 1 - Mathf.Pow(smoothing, dt)) | |
} | |
Both functions should produce about the same value. | |
public static float Damp(float a, float b, float lambda, float dt) | |
{ | |
return Mathf.Lerp(a, b, 1 - Mathf.Exp(-lambda * dt)) | |
} | |
This damps v value to zero | |
public static float Damp(float source, float smoothing, float dt) | |
{ | |
return source * Mathf.Pow(smoothing, dt); | |
} | |
*/ | |
class SpringFloat{ | |
constructor( stiff=0.1, damp=0.3 ){ | |
this.to = 0; | |
this.at = 0; | |
this.vel = 0; | |
this.stiffness = stiff; //tension | |
this.damping = damp; | |
this.rate = 10 / 16.67; // 60 FPS | |
//this.damping = Math.pow( 1-damp, 10 / 16 ); | |
} | |
set( to, at=null ){ | |
this.to = to; | |
if( at != null ) this.at = at; | |
return this; | |
} | |
dif(){ return this.at - this.to; } | |
setAt( v ){ this.at = v; return this; } | |
update( dt ){ | |
//let accel = ( -this.stiffness * ( this.at - this.to ) ) + (-this.damping * this.vel); | |
//this.vel += accel * this.rate; | |
//this.at += this.vel * this.rate; | |
this.stiffness = 0.1; //tension | |
this.damping = 0.35; | |
//GOOD, very springy and fast | |
//let accel = -this.stiffness * ( this.at - this.to ); | |
//this.vel = ( this.vel + accel * this.rate ) * Math.pow( 1 - this.damping, this.rate ); | |
//this.at = this.at + this.vel * this.rate; | |
dt *= 40; | |
//let accel = -this.stiffness * ( this.at - this.to ); | |
//this.vel = ( this.vel + accel * dt ) * Math.pow( 1 - this.damping, dt ); | |
//this.at = this.at + this.vel * dt; | |
//http://lolengine.net/blog/2015/05/03/damping-with-delta-time | |
let frameRate = 60; | |
let accel = -this.stiffness * ( this.at - this.to ); | |
this.vel = ( this.vel + accel * dt ) * Math.pow( 1 - this.damping / frameRate, frameRate * dt ); | |
this.at = this.at + this.vel * dt; | |
//GOOD, Slow accell and decell. | |
//let accel = -this.stiffness * ( this.at - this.to ); // Accell = Force / Mass, but not using mass. | |
//this.vel += ( accel - this.damping * this.vel ) * dt; | |
//this.at = this.at + this.vel * dt; | |
//if( Math.abs( this.at - this.to ) < 0.0001 ) console.log( "done" ); | |
// | |
//test for Velocity and Distance to be sure we're done. | |
//if( Math.abs( this.vel ) < 0.00005 ){ | |
//console.log( "done" ); | |
//this.at = this.to; | |
//} | |
//console.log( this.at, this.to ); | |
//console.log("accel", accel, "velocity", this.vel, "dif", ( this.at - this.to ), -this.stiffness * ( this.at - this.to ) ); | |
return this.at; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment