diff options
Diffstat (limited to 'bounds3/slider.js')
-rw-r--r-- | bounds3/slider.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/bounds3/slider.js b/bounds3/slider.js new file mode 100644 index 0000000..e245071 --- /dev/null +++ b/bounds3/slider.js @@ -0,0 +1,45 @@ +( function() { + +function Slider( x, y ) { + this.x = x; + this.y = y; + this.velocity = 0; + this.accel = 0; + this.friction = 0.15; +} + +Slider.prototype.update = function() { + this.velocity += this.accel; + this.velocity *= ( 1 - this.friction ); + this.x += this.velocity; + this.accel = 0; +}; + +Slider.prototype.applyForce = function( force ) { + this.accel += force; +}; + + +Slider.prototype.getRestingPosition = function() { + + var fFriction = 1 - this.friction; + var restingVelo = 0.07; + var ticks = getBaseLog( fFriction, restingVelo / Math.abs( this.velocity ) ); + var frictionSum = ( Math.pow( fFriction, ticks + 1 ) - 1 ) / ( fFriction - 1 ); + var restX = this.x + this.velocity * fFriction * frictionSum; + + return { + x: restX, + frictionSum: frictionSum + }; +}; + + +function getBaseLog( a, b ) { + return Math.log( b ) / Math.log( a ); +} + + +window.Slider = Slider; + +})(); |