From 71f3b37f149c96370cee1271c0143e802441e9b6 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Tue, 2 Jul 2019 16:50:11 +0200 Subject: [PATCH] optimized collision detection by only looking at the alpha channel data --- HISTORY.md | 4 ++++ snap.html | 2 +- src/morphic.js | 15 +++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 542e64c7..37d422f4 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,11 +4,15 @@ * **New Features:** * **Notable Changes:** * gliding animation when inserting blocks using the keyboard + * optimized collision detection * **Notable Fixes:** * list watchers occasionally didn't show cells after reassigning a changed list to a variable * FOREACH over a linked list failed for scripts mutating it * **Translation Updates:** +### 2019-07-02 +* morphic: optimized collision detection by only looking at the alpha channel data + ### 2019-07-01 * new dev version * lists: fixed #2446 diff --git a/snap.html b/snap.html index 4d4916d3..53393354 100755 --- a/snap.html +++ b/snap.html @@ -4,7 +4,7 @@ Snap! Build Your Own Blocks 5.0.1 - dev - - + diff --git a/src/morphic.js b/src/morphic.js index 83fec678..5b599dba 100644 --- a/src/morphic.js +++ b/src/morphic.js @@ -1162,7 +1162,7 @@ /*global window, HTMLCanvasElement, FileReader, Audio, FileList, Map*/ -var morphicVersion = '2019-July-01'; +var morphicVersion = '2019-July-02'; var modules = {}; // keep track of additional loaded modules var useBlurredShadows = getBlurredShadowSupport(); // check for Chrome-bug @@ -4454,19 +4454,18 @@ Morph.prototype.evaluateString = function (code) { Morph.prototype.isTouching = function (otherMorph) { var oImg = this.overlappingImage(otherMorph), - data; + data, len, i; if (!oImg.width || !oImg.height) { return false; } data = oImg.getContext('2d') .getImageData(1, 1, oImg.width, oImg.height) .data; - return detect( - data, - function (each) { - return each !== 0; - } - ) !== null; + len = data.length; + for(i = 3; i < len; i += 4) { + if (data[i] !== 0) {return true; } + } + return false; }; Morph.prototype.overlappingImage = function (otherMorph) {