Skip to content

Commit

Permalink
CollisionHelpers extraction and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Procopiou committed Mar 11, 2017
1 parent 09a5c5a commit e127669
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ rules:
no-whitespace-before-property: error
no-with: error
nonblock-statement-body-position: error
object-curly-newline: error
object-curly-newline: off
object-curly-spacing: error
object-property-newline: error
object-shorthand: error
Expand Down
37 changes: 10 additions & 27 deletions lib/aa-rectangle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const {segmentsCollide} = require('./collision-helpers');

let AARectangleId = 0;

class AARectangle {
Expand Down Expand Up @@ -50,14 +52,6 @@ class AARectangle {
return this._x;
}

// get x1 () {
// return this._x - (this._width / 2) + (this.translateX * this.orientationX);
// }

// get x2 () {
// return this._x + (this._width / 2) + (this.translateX * this.orientationX);
// }

set x (value) {
this._x = value || 0;
}
Expand All @@ -66,14 +60,6 @@ class AARectangle {
return this._y;
}

// get y1 () {
// return this._y - (this._height / 2) + (this.translateY * this.orientationY);
// }

// get y2 () {
// return this._y + (this._height / 2) + (this.translateY * this.orientationY);
// }

set y (value) {
this._y = value || 0;
}
Expand All @@ -95,10 +81,14 @@ class AARectangle {
}
this.globalX = globalX;
this.globalY = globalY;
this.globalX1 = globalX - (this._width / 2) + (this.translateX * this.orientationX);
this.globalX2 = globalX + (this._width / 2) + (this.translateX * this.orientationX);
this.globalY1 = globalY - (this._height / 2) + (this.translateY * this.orientationY);
this.globalY2 = globalY + (this._height / 2) + (this.translateY * this.orientationY);
const halfX = this._width / 2;
const extraX = this.translateX * this.orientationX;
this.globalX1 = globalX - halfX + extraX;
this.globalX2 = globalX + halfX + extraX;
const halfY = this._height / 2;
const extraY = this.translateY * this.orientationY;
this.globalY1 = globalY - halfY + extraY;
this.globalY2 = globalY + halfY + extraY;
}

add (box) {
Expand Down Expand Up @@ -141,13 +131,6 @@ class AARectangle {

}

// Helpers

function segmentsCollide (segment1_start, segment1_end, segment2_start, segment2_end) {
return segment1_end >= segment2_start &&
segment1_start <= segment2_end;
}

function parseDimensionValue (value) {
let newValue = value || 1;
if (newValue < 0) {
Expand Down
8 changes: 8 additions & 0 deletions lib/collision-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {segmentsCollide};

function segmentsCollide (segment1_start, segment1_end, segment2_start, segment2_end) {
return segment1_end >= segment2_start &&
segment1_start <= segment2_end;
}
32 changes: 32 additions & 0 deletions test/collision-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const {segmentsCollide} = require('../lib/collision-helpers');
const expect = require('chai').expect;

describe('CollisionHelpers', function () {

describe('.segmentsCollide', function () {

it('returns true if segments touch', function () {
expect(segmentsCollide(1, 2, 2, 3)).to.equal(true);
expect(segmentsCollide(2, 3, 1, 2)).to.equal(true);
});

it('returns true if segments intersect', function () {
expect(segmentsCollide(1, 2.5, 2, 3)).to.equal(true);
expect(segmentsCollide(1.9, 3, 1, 2)).to.equal(true);
});

it('returns true if one segment is inside the other', function () {
expect(segmentsCollide(1, 5, 2, 3)).to.equal(true);
expect(segmentsCollide(2, 3, 1, 5)).to.equal(true);
});

it('returns false if segments do not touch or intersect', function () {
expect(segmentsCollide(1, 2, 3, 4)).to.equal(false);
expect(segmentsCollide(3, 4, 1, 2)).to.equal(false);
});

});

});

0 comments on commit e127669

Please sign in to comment.