Created
September 12, 2020 12:55
-
-
Save worldoptimizer/82e2bd5ad57918f14552672f0a210598 to your computer and use it in GitHub Desktop.
hypeDocument.queryIntersections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Query intersections between elements using Matter.js | |
* | |
* @param {Node} sourceElm The source element used in each check | |
* @param {Nodelist} targetElms The target elements to query against | |
* @return {Nodelist} Returns a list of targetElms that intersect with sourceElm | |
*/ | |
hypeDocument.queryIntersections = function(sourceElm, targetElms) { | |
var sourceBody = hypeDocument.getElementProperty(sourceElm, 'physics-body'); | |
if (sourceBody) { | |
var targetBodies = []; | |
targetElms.forEach(function(elm){ | |
var elmBody = hypeDocument.getElementProperty(elm, 'physics-body'); | |
if (elmBody) { | |
targetBodies.push(elmBody); | |
} else { | |
console.log('target '+elm.id+' must have static Physics enabled'); | |
} | |
}); | |
var collisions = Matter.Query.collides(sourceBody, targetBodies); | |
if (collisions.length) { | |
return collisions.map(function(collision){ | |
var isB = sourceElm.id == collision.bodyB.elementId; | |
return document.getElementById((isB? collision.bodyA : collision.bodyB).elementId); | |
}); | |
} | |
} else { | |
console.log('sourceElm must have static Physics enabled'); | |
} | |
return []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment