require('./Constraint');
lib.DistanceConstraint = DistanceConstraint;
function DistanceConstraint(distance, a, b) {
  var size = a.length || arguments.length - 1;
  var min = distance.length ? distance[0] : distance;
  var max = distance.length ? distance[1] : distance;

  lib.Constraint.call(this, size, 2);
  this.setDistance(min, max);
  this.setIndices(a, b);
}

DistanceConstraint.create = lib.ctor(DistanceConstraint);
DistanceConstraint.prototype = Object.create(lib.Constraint.prototype);
DistanceConstraint.prototype.constructor = DistanceConstraint;

DistanceConstraint.prototype.setDistance = function (min, max) {
  this.setMin(min);
  this.setMax(max != null ? max : min);
};

DistanceConstraint.prototype.setMin = function (min) {
  this._min2 = min * min;
};

DistanceConstraint.prototype.setMax = function (max) {
  this._max2 = max * max;
};

DistanceConstraint.prototype.applyConstraint = function (index, p0, p1) {
  var ii = this.indices;
  var ai = ii[index], bi = ii[index + 1];

  var ax = ai * 3, ay = ax + 1, az = ax + 2;
  var bx = bi * 3, by = bx + 1, bz = bx + 2;

  var dx = p0[bx] - p0[ax];
  var dy = p0[by] - p0[ay];
  var dz = p0[bz] - p0[az];

  if (!(dx || dy || dz)) {
    dx = dy = dz = 0.1;
  }

  var dist2 = dx * dx + dy * dy + dz * dz;
  var min2 = this._min2;
  var max2 = this._max2;

  if (dist2 < max2 && dist2 > min2) { return; }

  var target2 = dist2 < min2 ? min2 : max2;
  var diff = target2 / (dist2 + target2);
  var aDiff = diff - 0.5;
  var bDiff = diff - 0.5;

  p0[ax] -= dx * aDiff;
  p0[ay] -= dy * aDiff;
  p0[az] -= dz * aDiff;

  p0[bx] += dx * bDiff;
  p0[by] += dy * bDiff;
  p0[bz] += dz * bDiff;
};

@@ -1,73 +1,994 @@
-require('./Constraint');%0Alib.DistanceConstraint = DistanceConstraint;
+// ..................................................%0A// DistanceConstraint%0A// ..................................................%0A%0Arequire('./Constraint');%0Alib.DistanceConstraint = DistanceConstraint;%0A%0A/**%0A  Defines one or many relationships between sets of two particles.%0A%0A  %60%60%60javascript%0A  var a = 0, b = 1, c = 2;%0A  var single = DistanceConstraint.create(10, a, b);%0A  var many = DistanceConstraint.create(10, %5Ba, b, a, c%5D);%0A  %60%60%60%0A%0A  Particles are constrained by a fixed distance or a distance range.%0A%0A  %60%60%60javascript%0A  var min = 0.5, max = 2.5;%0A  var fixed = DistanceConstraint.create(max, 0, 1);%0A  var range = DistanceConstraint.create(%5Bmin, max%5D, 0, 1);%0A  %60%60%60%0A%0A  @class DistanceConstraint%0A  @extends Constraint%0A  @constructor%0A  @param %7BFloat%7CArray%7D  distance  Distance or distance range %60%5Bmin, max%5D%60 between particles%0A  @param %7BInt%7CArray%7D    a         Particle index or list of many constraint sets%0A  @param %7BInt%7D         %5Bb%5D        Particle index (only used if %60a%60 is not an array)%0A*/
 %0Afun
@@ -1282,16 +1282,101 @@
  b);%0A%7D%0A%0A
+/**%0A  Create instance, accepts constructor arguments.%0A%0A  @method create%0A  @static%0A*/%0A
 Distance
@@ -1561,16 +1561,106 @@
 raint;%0A%0A
+/**%0A  Set distance%0A%0A  @method setDistance%0A  @param %7BFloat%7D  min%0A  @param %7BFloat%7D %5Bmax%5D%0A*/%0A
 Distance
@@ -1776,24 +1776,93 @@
 : min);%0A%7D;%0A%0A
+/**%0A  Set minimum distance%0A%0A  @method setMin%0A  @param %7BFloat%7D min%0A*/%0A
 DistanceCons
@@ -1938,90 +1938,435 @@
 %7D;%0A%0A
-DistanceConstraint.prototype.setMax = function (max) %7B%0A  this._max2 = max * max;%0A%7D
+/**%0A  Cached value of minimum distance squared%0A%0A  @property _min2%0A  @type Float%0A  @private%0A*/%0ADistanceConstraint.prototype._min2 = null;%0A%0A/**%0A  Set maximum distance%0A%0A  @method setMax%0A  @param %7BFloat%7D max%0A*/%0ADistanceConstraint.prototype.setMax = function (max) %7B%0A  this._max2 = max * max;%0A%7D;%0A%0A/**%0A  Cached value of maximum distance squared%0A%0A  @property _max2%0A  @type Float%0A  @private%0A*/%0ADistanceConstraint.prototype._max2 = null
 ;%0A%0AD

Add DistanceConstraint docs