summaryrefslogtreecommitdiffstats
path: root/bounds2/particle.js
blob: f6058e31cc9ae9fd0f6813b30b3a95c5c6dee35c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
( function() {

function Particle( x, y ) {
  this.x = x;
  this.y = y;
  this.velocity = 0;
  this.accel = 0;
  this.friction = 0.15;
}

Particle.prototype.update = function() {
  this.velocity += this.accel;
  this.velocity *= ( 1 - this.friction );
  this.x += this.velocity;
  this.accel = 0;
};

Particle.prototype.applyForce = function( force ) {
  this.accel += force;
};


Particle.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.Particle = Particle;

})();