Skip to content

Commit

Permalink
Collision tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Procopiou committed Mar 12, 2017
1 parent 16d957b commit c1ae289
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Build Status](https://semaphoreci.com/api/v1/pauloddr/aa-rectangle-javascript/branches/master/shields_badge.svg)](https://semaphoreci.com/pauloddr/aa-rectangle-javascript)
[![Test Coverage](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript/badges/coverage.svg)](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript/coverage)
[![Code Climate](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript/badges/gpa.svg)](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript)
[![Issue Count](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript/badges/issue_count.svg)](https://lima.codeclimate.com/github/pauloddr/aa-rectangle-javascript)

```javascript
var box = new AARectangle(width, height, x, y);
Expand Down
16 changes: 9 additions & 7 deletions lib/aa-rectangle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

let AARectangleId = 0;

Expand Down Expand Up @@ -114,19 +114,21 @@ class AARectangle {
collision (box) {
this.update();
box.update();
return this.collisionX(box) && this.collisionY(box);
}

const collidesInX = segmentsCollide(
collisionX (box) {
return segmentCollision(
this.globalX1, this.globalX2,
box.globalX1, box.globalX2
);
if (!collidesInX) {
return false;
}
const collidesInY = segmentsCollide(
}

collisionY (box) {
return segmentCollision(
this.globalY1, this.globalY2,
box.globalY1, box.globalY2
);
return collidesInY;
}

}
Expand Down
8 changes: 4 additions & 4 deletions lib/collision-helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = {segmentsCollide};
module.exports = {segmentCollision};

function segmentsCollide (segment1_start, segment1_end, segment2_start, segment2_end) {
return segment1_end >= segment2_start &&
segment1_start <= segment2_end;
function segmentCollision (segment1Start, segment1End, segment2Start, segment2End) {
return segment1End >= segment2Start &&
segment1Start <= segment2End;
}
56 changes: 54 additions & 2 deletions test/aa-rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('AARectangle', function () {
box3.add(box4);
box4.add(box5);

it('runs fast!', function () {
it('runs fast', function () {
this.retries(10); // allows engine to optimize
box2.flipX();
box3.flipY();
Expand All @@ -69,7 +69,7 @@ describe('AARectangle', function () {
time1 = now();
box4.update();
time2 = now();
expect(time2 - time1).to.be.at.most(0.009); // 9 nanoseconds
expect(time2 - time1).to.be.at.most(0.009); // 9 nanoseconds ~ 111,111 op/sec
}
box2.unflipX();
box3.unflipY();
Expand Down Expand Up @@ -175,4 +175,56 @@ describe('AARectangle', function () {

});

describe('#collision', function () {

it('returns true if a box is completely inside another', function () {
let box1 = new AARectangle(100, 100, 1, 1);
let box2 = new AARectangle(2, 2, -40, -40);
expect(box1.collision(box2)).to.equal(true);
});

it('returns true if sides and bases touch', function () {
let box1 = new AARectangle(2, 2, 1, 1);
let box2 = new AARectangle(2, 2, -1, -1);
expect(box1.collision(box2)).to.equal(true);
});

it('returns false if bases touch but sides do not', function () {
let box1 = new AARectangle(2, 2, 3, 1);
let box2 = new AARectangle(2, 2, -1, -1);
expect(box1.collision(box2)).to.equal(false);
});

it('returns false if sides touch but bases do not', function () {
let box1 = new AARectangle(2, 2, 1, 3);
let box2 = new AARectangle(2, 2, -1, -1);
expect(box1.collision(box2)).to.equal(false);
});

describe('with parent', function () {

it('returns true if box is dragged towards another by parent position', function () {
let parent = new AARectangle(0, 0, 0, 2);
let box1 = new AARectangle(2, 2, 1, 3);
let box2 = new AARectangle(2, 2, -1, -1);
expect(box1.collision(box2)).to.equal(false);
parent.add(box2); // parent position will bring box2 closer to box1
expect(box1.collision(box2)).to.equal(true);
});

it('returns true if box is dragged towards another by parent translation', function () {
let parent = new AARectangle(0, 0, 0, 0);
let box1 = new AARectangle(2, 2, 1, 3);
let box2 = new AARectangle(2, 2, -1, -1);
expect(box1.collision(box2)).to.equal(false);
parent.add(box2);
expect(box1.collision(box2)).to.equal(false);
parent.translateY = 2; // parent translation will bring box2 closer to box1
expect(box1.collision(box2)).to.equal(true);
});

});

});

});
20 changes: 10 additions & 10 deletions test/collision-helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict';

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

describe('CollisionHelpers', function () {

describe('.segmentsCollide', function () {
describe('.segmentCollision', 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);
expect(segmentCollision(1, 2, 2, 3)).to.equal(true);
expect(segmentCollision(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);
expect(segmentCollision(1, 2.5, 2, 3)).to.equal(true);
expect(segmentCollision(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);
expect(segmentCollision(1, 5, 2, 3)).to.equal(true);
expect(segmentCollision(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);
expect(segmentCollision(1, 2, 3, 4)).to.equal(false);
expect(segmentCollision(3, 4, 1, 2)).to.equal(false);
});

});
Expand Down

0 comments on commit c1ae289

Please sign in to comment.