1 /**
  2  * Constructs a new drag force with the specified constant.
  3  *
  4  * @class Implements a drag force, simulating friction. The drag force is
  5  * applied in the opposite direction of the particle's velocity. Since Position
  6  * Verlet integration does not track velocities explicitly, the error term with
  7  * this estimate of velocity is fairly high, so the drag force may be
  8  * inaccurate.
  9  *
 10  * @extends pv.Force
 11  * @param {number} k the drag constant.
 12  * @see #constant
 13  */
 14 pv.Force.drag = function(k) {
 15   var force = {};
 16 
 17   if (!arguments.length) k = .1; // default drag constant
 18 
 19   /**
 20    * Sets or gets the drag constant, in the range [0,1]. The default drag
 21    * constant is 0.1. The drag forces scales linearly with the particle's
 22    * velocity based on the given drag constant.
 23    *
 24    * @function
 25    * @name pv.Force.drag.prototype.constant
 26    * @param {number} x the new drag constant.
 27    * @returns {pv.Force.drag} this, or the current drag constant.
 28    */
 29   force.constant = function(x) {
 30     if (arguments.length) { k = x; return force; }
 31     return k;
 32   };
 33 
 34   /**
 35    * Applies this force to the specified particles.
 36    *
 37    * @function
 38    * @name pv.Force.drag.prototype.apply
 39    * @param {pv.Particle} particles particles to which to apply this force.
 40    */
 41   force.apply = function(particles) {
 42     if (k) for (var p = particles; p; p = p.next) {
 43       p.fx -= k * p.vx;
 44       p.fy -= k * p.vy;
 45     }
 46   };
 47 
 48   return force;
 49 };
 50