Skip to content

Commit

Permalink
🐛 Fix issue with bounding box when x or y are non zero and expose bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcgregor committed Jul 18, 2016
1 parent c17b5c9 commit 99c6bf8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 47 deletions.
26 changes: 16 additions & 10 deletions dist/boid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/boid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "boid",
"version": "0.2.2",
"version": "0.2.4",
"description": "Bird-like behaviours",
"keywords": [
"boid",
Expand Down
23 changes: 15 additions & 8 deletions src/boid.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,18 @@ function Boid(options) {
};

var bounce = function() {
if (position.x > bounds.width) {
position.x = bounds.width;
var maxX = bounds.x + bounds.width;
if (position.x > maxX) {
position.x = maxX;
velocity.x *= -1;
} else if (position.x < bounds.x) {
position.x = bounds.x;
velocity.x *= -1;
}
if (position.y > bounds.height) {
position.y = bounds.height;

var maxY = bounds.y + bounds.height;
if (position.y > maxY) {
position.y = maxY;
velocity.y *= -1;
} else if (position.y < bounds.y) {
position.y = bounds.y;
Expand All @@ -102,15 +105,18 @@ function Boid(options) {
};

var wrap = function() {
if (position.x > bounds.width) {
var maxX = bounds.x + bounds.width;
if (position.x > maxX) {
position.x = bounds.x;
} else if (position.x < bounds.x) {
position.x = bounds.width;
position.x = maxX;
}
if (position.y > bounds.height) {

var maxY = bounds.y + bounds.height;
if (position.y > maxY) {
position.y = bounds.y;
} else if (position.y < bounds.y) {
position.y = bounds.height;
position.y = maxY;
}
};

Expand Down Expand Up @@ -329,6 +335,7 @@ function Boid(options) {

// methods
var boid = {
bounds: bounds,
setBounds: setBounds,
update: update,
pursue: pursue,
Expand Down
61 changes: 34 additions & 27 deletions test/boid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@ var Boid = require('../src/boid.js');

describe('boid', function() {

var boid = new Boid();

it ('should have created a boid instance', function() {
expect(boid).to.be.an('object');
expect(boid.setBounds).to.be.a('function');
expect(boid.update).to.be.a('function');
expect(boid.pursue).to.be.a('function');
expect(boid.evade).to.be.a('function');
expect(boid.wander).to.be.a('function');
expect(boid.avoid).to.be.a('function');
expect(boid.followPath).to.be.a('function');
expect(boid.flock).to.be.a('function');
expect(boid.arrive).to.be.a('function');
expect(boid.flee).to.be.a('function');

expect(boid.position).to.be.an('object');
expect(boid.velocity).to.be.an('object');
// expect(boid.edgeBehaviour).to.be.a('string');
expect(boid.mass).to.be.a('number');
expect(boid.maxSpeed).to.be.a('number');
expect(boid.maxForce).to.be.a('number');
});

it ('should have static members', function() {
expect(Boid.vec2).to.be.a('function');
expect(Boid.obstacle).to.be.a('function');
});
var boid = new Boid();

it('should have created a boid instance', function() {
expect(boid).to.be.an('object');
});

it('should have methods', function() {
expect(boid.setBounds).to.be.a('function');
expect(boid.update).to.be.a('function');
expect(boid.pursue).to.be.a('function');
expect(boid.evade).to.be.a('function');
expect(boid.wander).to.be.a('function');
expect(boid.avoid).to.be.a('function');
expect(boid.followPath).to.be.a('function');
expect(boid.flock).to.be.a('function');
expect(boid.arrive).to.be.a('function');
expect(boid.flee).to.be.a('function');
expect(boid.seek).to.be.a('function');
});

it('should have props', function() {
expect(boid.position).to.be.an('object');
expect(boid.velocity).to.be.an('object');
expect(boid.bounds).to.be.an('object');
// expect(boid.edgeBehaviour).to.be.a('string');
expect(boid.mass).to.be.a('number');
expect(boid.maxSpeed).to.be.a('number');
expect(boid.maxForce).to.be.a('number');
});

it('should have static members', function() {
expect(Boid.vec2).to.be.a('function');
expect(Boid.obstacle).to.be.a('function');
});

});

0 comments on commit 99c6bf8

Please sign in to comment.