boid
Bird-like behaviours (http://en.wikipedia.org/wiki/Boids)
Installation
npm:
npm install boid --save-dev
bower:
bower install boid --save-dev
Usage
var Boid = ; var canvas = document context = canvas flockers = flocker; while flockerslength < 40 flocker = ; flocker; flockerpositionx = canvaswidth * Math; flockerpositiony = canvasheight * Math; flockervelocityx = 20 * Math - 10; flockervelocityy = 20 * Math - 10; flockers; { window; context; flockers;};
Behaviours
// steer towards a target positionboid; // steer away from a target positionboid; // seek until within arriveThresholdboid; // steer towards a target boid predicting where it's velocity is taking itboid; // steer away from a target boid predicting where it's velocity is taking itboid; // wander around randomlyboid; // attempt to avoid an array of objects with x, y and radius propertiesboid; // follow a path made up of an array or vectors, optionally loopingboid; // flock - group of boids loosely move togetherboid; // update must be called after any behavioursboid;
Configuration
// position vectorboidpositionxboidpositiony// velocity vectorboidvelocityxboidvelocityy// empty object for any properties needed e.g. id or colorboiduserData // affects all behaviours: // define the area containing the boidboid;// how the boid reacts when hitting the bounds// can be Boid.EDGE_NONE, Boid.EDGE_WRAP or Boid.EDGE_BOUNCEboidedgeBehavior// mass - affects the steering forceboidmass// maximum speedboidmaxSpeed// maximum force to apply to steeringboidmaxForce // affects arrive behaviour: // threshold at which the boid reaches targetboidarriveThreshold // affects wander behaviour: // distance forward to go towardsboidwanderDistance// distance to wander from headingboidwanderRadius// range that angle is updated to (plus or minus)boidwanderRange // affects avoid behaviour: // distance to look aheadboidavoidDistance// buffer to avoid the obstacle byboidavoidBuffer // affects followPath behaviour: // the current index the boid has reached in the path arrayboidpathIndex// the threshold at which the boid reaches each waypointboidpathThreshold // affects flock behaviour: // distance within which boid has sight of the flockboidmaxDistance// distance within which boid is too close to another boidboidminDistance
Helpers
BoidEDGE_NONEBoidEDGE_BOUNCEBoidEDGE_WRAPBoidBoid
Dev setup
To install dependencies:
$ npm install
$ bower install
To run tests:
$ npm test