summaryrefslogtreecommitdiffstats
path: root/attractors7/particle.js
blob: ed869f750acb94e00b67386ba4ea2d2603e4cb2a (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
46
47
48
49
50
51
( 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() {
  // get how many ticks until velocity is slow
  var restingVelo = 0.07;
  var ticks = getBaseLog( 1 - this.friction, restingVelo / Math.abs( this.velocity ) );
  // integrate to determine resting position
  /*
  var x = this.x;
  var velo = this.velocity;
  while ( ticks > 0 ) {
    velo *= 1 - this.friction;
    x += velo;
    ticks--;
  }
  */
  // http://mikestoolbox.com/powersum.html
  var damping = 1 - this.friction;
  var sum = ( Math.pow( damping, ticks + 1 ) - 1 ) / ( damping - 1 );
  return this.x + this.velocity * damping * sum;
}

function getBaseLog( a, b ) {
  return Math.log( b ) / Math.log( a );
}



window.Particle = Particle;

})();