File: src/solver/Island.js
module.exports = Island;
/**
* An island of bodies connected with equations.
* @class Island
* @constructor
*/
function Island(){
/**
* Current equations in this island.
* @property equations
* @type {Array}
*/
this.equations = [];
/**
* Current bodies in this island.
* @property bodies
* @type {Array}
*/
this.bodies = [];
}
/**
* Clean this island from bodies and equations.
* @method reset
*/
Island.prototype.reset = function(){
this.equations.length = this.bodies.length = 0;
}
/**
* Get all unique bodies in this island.
* @method getBodies
* @return {Array} An array of Body
*/
Island.prototype.getBodies = function(){
var bodies = [],
bodyIds = [],
eqs = this.equations;
for(var i=0; i!==eqs.length; i++){
var eq = eqs[i];
if(bodyIds.indexOf(eq.bi.id)===-1){
bodies.push(eq.bi);
bodyIds.push(eq.bi.id);
}
if(bodyIds.indexOf(eq.bj.id)===-1){
bodies.push(eq.bj);
bodyIds.push(eq.bj.id);
}
}
return bodies;
};
/**
* Solves all constraints in the group of islands.
* @method solve
* @param {Number} dt
* @param {Solver} solver
*/
Island.prototype.solve = function(dt,solver){
var bodies = [];
solver.removeAllEquations();
// Add equations to solver
var numEquations = this.equations.length;
for(var j=0; j!==numEquations; j++){
solver.addEquation(this.equations[j]);
}
var islandBodies = this.getBodies();
var numBodies = islandBodies.length;
for(var j=0; j!==numBodies; j++){
bodies.push(islandBodies[j]);
}
// Solve
solver.solve(dt,{bodies:bodies});
};