diff --git a/README.md b/README.md index 6c10114..3853370 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/lib/aa-rectangle.js b/lib/aa-rectangle.js index 401f5f2..739654f 100644 --- a/lib/aa-rectangle.js +++ b/lib/aa-rectangle.js @@ -1,6 +1,6 @@ 'use strict'; -const {segmentsCollide} = require('./collision-helpers'); +const {segmentCollision} = require('./collision-helpers'); let AARectangleId = 0; @@ -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; } } diff --git a/lib/collision-helpers.js b/lib/collision-helpers.js index 366aa4f..fe64a6d 100644 --- a/lib/collision-helpers.js +++ b/lib/collision-helpers.js @@ -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; } diff --git a/test/aa-rectangle.js b/test/aa-rectangle.js index c4d20ed..ee240f1 100644 --- a/test/aa-rectangle.js +++ b/test/aa-rectangle.js @@ -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(); @@ -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(); @@ -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); + }); + + }); + + }); + }); diff --git a/test/collision-helpers.js b/test/collision-helpers.js index 5b83bcd..e10758e 100644 --- a/test/collision-helpers.js +++ b/test/collision-helpers.js @@ -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); }); });