From 0df60ac9a21676926ca91a194961784696a83a95 Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Mon, 15 Jan 2018 13:29:28 -0500 Subject: [PATCH] Fix MBR narrow-phase collision logic The narrow phase collision check was always returning true for 'MBR' type collisions. --- src/spatial/collision.js | 2 +- tests/unit/spatial/collision.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/spatial/collision.js b/src/spatial/collision.js index 44570cd3..f8a9746a 100644 --- a/src/spatial/collision.js +++ b/src/spatial/collision.js @@ -532,7 +532,7 @@ Crafty.c("Collision", { SAT.obj = obj; SAT.type = "SAT"; } - } else if (Crafty.rectManager.overlap(area, this._cbr || this._mbr || this)){ + } else if (Crafty.rectManager.overlap(area, obj._cbr || obj._mbr || obj)){ results.push({ obj: obj, type: "MBR" diff --git a/tests/unit/spatial/collision.js b/tests/unit/spatial/collision.js index 9a503727..f4484ed9 100644 --- a/tests/unit/spatial/collision.js +++ b/tests/unit/spatial/collision.js @@ -103,6 +103,22 @@ _.ok('overlap' in results[0], "expected overlap value"); }); + test("hit -- collision vs. non collision tests", function(_){ + var e1 = Crafty.e("2D, Collision") + .attr({x: 0, y: 0, w: 2, h: 2}); + var e2 = Crafty.e("2D") + .attr({x: 0, y: 0, w: 2, h: 2}); + var results = e1.hit('2D'); + _.strictEqual(results.length, 1, "exactly one collision"); + _.strictEqual(results[0].type, "MBR", "expecdted MBR collision type"); + _.strictEqual(results[0].obj[0], e2[0], "Expected collision with e2"); + + // Move e2 such that it should be returned by the broadphase search, but should not be considered a hit + e2.x=3; + var newResults = e1.hit('2D'); + _.ok(!newResults, 0, "No collisions"); + }); + test("onHit", function(_) { var e = Crafty.e("2D, Collision") .attr({x: 0, y: 0, w: 25, h: 25});