text: /**
   * Performs forces integration, using given time step. Uses Euler method to solve
   * differential equation (http://en.wikipedia.org/wiki/Euler_method ).
   *
   * @returns {Number} squared distance of total position updates.
   */
  
  module.exports = integrate;
  
  function integrate(bodies, timeStep) {
    var dx = 0, tx = 0,
        dy = 0, ty = 0,
        i,
        max = bodies.length;
  
    if (max === 0) {
      return 0;
    }
  
    for (i = 0; i < max; ++i) {
      var body = bodies[i],
          coeff = timeStep / body.mass;
  
      body.velocity.x += coeff * body.force.x;
      body.velocity.y += coeff * body.force.y;
      var vx = body.velocity.x,
          vy = body.velocity.y,
          v = Math.sqrt(vx * vx + vy * vy);
  
      if (v > 1) {
        // We normalize it so that we move within timeStep range. 
        // for the case when v <= 1 - we let velocity to fade out.
        body.velocity.x = vx / v;
        body.velocity.y = vy / v;
      }
  
      dx = timeStep * body.velocity.x;
      dy = timeStep * body.velocity.y;
  
      body.pos.x += dx;
      body.pos.y += dy;
  
      tx += Math.abs(dx); ty += Math.abs(dy);
    }
  
    return (tx * tx + ty * ty)/max;
  }
  