Skip to content

Commit

Permalink
Test and fix CircleCollider.overlap() with offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
islemaster committed May 4, 2017
1 parent 6f3d1d5 commit 5b28e03
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/p5.play.js
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,11 @@ function CircleCollider(pInst, _center, _radius, _offset) {
//square dist
var r = this.radius + other.radius;
r *= r;
var sqDist = pow(this.center.x - other.center.x, 2) + pow(this.center.y - other.center.y, 2);
var thisCenterX = this.center.x + this.offset.x;
var thisCenterY = this.center.y + this.offset.y;
var otherCenterX = other.center.x + other.offset.x;
var otherCenterY = other.center.y + other.offset.y;
var sqDist = pow(thisCenterX - otherCenterX, 2) + pow(thisCenterY - otherCenterY, 2);
return r > sqDist;
};

Expand Down
78 changes: 69 additions & 9 deletions test/unit/circle-collider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ describe('CircleCollider', function() {
);
}

// Create at a (0,0) with radius=1 to test use of offsets
function makeWithOffset(x, y) {
return new pInst.CircleCollider(
new p5.Vector(0, 0),
1,
new p5.Vector(x, y)
);
}

it('conforms to the collider interface', function() {
// Still figuring out what this is though;
var collider = new pInst.CircleCollider();
Expand Down Expand Up @@ -61,8 +70,8 @@ describe('CircleCollider', function() {

it('true when tangent along 45deg line', function() {
var a = makeAt(2, 2);
var b = makeAt(2 + 2*Math.cos(Math.PI / 4),
2 + 2*Math.sin(Math.PI / 4));
var b = makeAt(2 + 2 * Math.cos(Math.PI / 4),
2 + 2 * Math.sin(Math.PI / 4));
expect(a.overlap(b)).to.be.true;
expect(b.overlap(a)).to.be.true;
});
Expand All @@ -84,6 +93,57 @@ describe('CircleCollider', function() {
expect(a.overlap(b)).to.be.false;
expect(b.overlap(a)).to.be.false;
});

it('true when exactly overlapped by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(2, 2);
expect(a.overlap(b)).to.be.true;
expect(b.overlap(a)).to.be.true;
});

it('true when partially overlapped by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(2, 3);
expect(a.overlap(b)).to.be.true;
expect(b.overlap(a)).to.be.true;
});

it('false when tangent along axes by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(4, 2); // Separated by 2 on the x-axis
expect(a.overlap(b)).to.be.false;
expect(b.overlap(a)).to.be.false;

var c = makeWithOffset(2, 4); // Separated by 2 on the y-axis
expect(a.overlap(c)).to.be.false;
expect(c.overlap(a)).to.be.false;
});

it('true when tangent along 45deg line by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(2 + 2 * Math.cos(Math.PI / 4),
2 + 2 * Math.sin(Math.PI / 4));
expect(a.overlap(b)).to.be.true;
expect(b.overlap(a)).to.be.true;
});

it('false when distant along axes by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(5, 2); // Separated by 3 on the x-axis
expect(a.overlap(b)).to.be.false;
expect(b.overlap(a)).to.be.false;

var c = makeWithOffset(2, 5); // Separated by 3 on the y-axis
expect(a.overlap(c)).to.be.false;
expect(c.overlap(a)).to.be.false;
});

it('false when distant along 45deg line by offset', function() {
var a = makeAt(2, 2);
var b = makeWithOffset(4, 4); // Separated by 2 on both x and y axes
expect(a.overlap(b)).to.be.false;
expect(b.overlap(a)).to.be.false;
});
});
});

Expand Down Expand Up @@ -120,7 +180,7 @@ describe('CircleCollider', function() {
expect(horizontalDisplacementB.y).to.closeTo(0, 0.001);
});

it('projects at an angle when overlapped at an angle', function () {
it('projects at an angle when overlapped at an angle', function() {
var a = makeAt(2, 2);

// At 45deg
Expand Down Expand Up @@ -174,7 +234,7 @@ describe('CircleCollider', function() {
describe('size()', function() {
var collider, radius;

beforeEach(function (){
beforeEach(function() {
radius = Math.floor(100 * Math.random());
collider = new pInst.CircleCollider(
new p5.Vector(0, 0),
Expand All @@ -184,13 +244,13 @@ describe('CircleCollider', function() {
});

it('returns a p5.Vector', function() {
expect(collider.size()).to.be.an.instanceOf(p5.Vector);
expect(collider.size()).to.be.an.instanceOf(p5.Vector);
});

it('is twice the circle radius in each direction', function () {
it('is twice the circle radius in each direction', function() {
var size = collider.size();
expect(size.x).to.equal(2*radius);
expect(size.y).to.equal(2*radius);
})
expect(size.x).to.equal(2 * radius);
expect(size.y).to.equal(2 * radius);
});
});
});

0 comments on commit 5b28e03

Please sign in to comment.